diff --git a/ffmpeg.c b/ffmpeg.c index 111551fe76..b5af415d42 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -610,6 +610,25 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) bsfc = bsfc->next; } + if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) && + ost->last_mux_dts != AV_NOPTS_VALUE && + pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) { + av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream " + "%d:%d; previous: %"PRId64", current: %"PRId64"; ", + ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); + if (exit_on_error) { + av_log(NULL, AV_LOG_FATAL, "aborting.\n"); + exit(1); + } + av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result " + "in incorrect timestamps in the output file.\n", + ost->last_mux_dts + 1); + pkt->dts = ost->last_mux_dts + 1; + if (pkt->pts != AV_NOPTS_VALUE) + pkt->pts = FFMAX(pkt->pts, pkt->dts); + } + ost->last_mux_dts = pkt->dts; + pkt->stream_index = ost->index; if (debug_ts) { diff --git a/ffmpeg.h b/ffmpeg.h index 3641a626b4..558c996789 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -320,6 +320,8 @@ typedef struct OutputStream { /* pts of the first frame encoded for this stream, used for limiting * recording time */ int64_t first_pts; + /* dts of the last packet sent to the muxer */ + int64_t last_mux_dts; AVBitStreamFilterContext *bitstream_filters; AVCodec *enc; int64_t max_frames; diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c index b27a2c3e2c..ff80c8ab44 100644 --- a/ffmpeg_opt.c +++ b/ffmpeg_opt.c @@ -1089,6 +1089,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e input_streams[source_index]->discard = 0; input_streams[source_index]->st->discard = AVDISCARD_NONE; } + ost->last_mux_dts = AV_NOPTS_VALUE; return ost; }