Opened 10 years ago

Closed 5 years ago

#3621 closed defect (fixed)

"pts < dts" causing remuxing failure for h264 from mkv to mkv

Reported by: Aman Owned by:
Priority: important Component: avformat
Version: git-master Keywords: av_interleaved_write_frame mkv h264 regression
Cc:, Blocked By:
Blocking: Reproduced by developer: yes
Analyzed by developer: no


I have some h264/mkv files that ffmpeg blows up on while remuxing, due to non-monotonic pts timestamps.

$ ffmpeg -nostats -i test.mkv -c:v copy -c:a copy out.mkv
ffmpeg version 2.2.1 Copyright (c) 2000-2014 the FFmpeg developers
  built on May  7 2014 00:59:53 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  configuration: --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
  libavutil      52. 66.100 / 52. 66.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  2.100 /  4.  2.100
  libavresample   1.  2.  0 /  1.  2.  0
  libswscale      2.  5.102 /  2.  5.102
  libswresample   0. 18.100 /  0. 18.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, matroska,webm, from 'test.mkv':
    encoder         : libebml v1.3.0 + libmatroska v1.4.0
    creation_time   : 2014-05-06 09:28:53
  Duration: 00:44:21.01, start: 0.000000, bitrate: 0 kb/s
    Stream #0:0(eng): Video: h264 (High), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 2k tbc (default)
    Stream #0:1(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s (default)
    Stream #0:2(eng): Subtitle: subrip (default)
Output #0, matroska, to 'out.mkv':
    encoder         : Lavf55.33.100
    Stream #0:0(eng): Video: h264 (H264 / 0x34363248), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 1k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), 384 kb/s (default)
    Stream #0:2(eng): Subtitle: ssa (default)
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
  Stream #0:2 -> #0:2 (subrip -> ssa)
Press [q] to stop, [?] for help
[matroska @ 0x7f9c6283de00] pts (375) < dts (418) in stream 0
av_interleaved_write_frame(): Invalid argument
frame=   11 fps=0.0 q=-1.0 Lsize=     217kB time=00:00:00.59 bitrate=2983.2kbits/s
video:189kB audio:27kB subtitle:0 data:0 global headers:0kB muxing overhead 0.492406%

Here are the video packets from -debug_ts on one of the files:

muxer <- type:video pkt_pts:0 pkt_pts_time:0 pkt_dts:0 pkt_dts_time:0 size:32998
muxer <- type:video pkt_pts:42 pkt_pts_time:0.042 pkt_dts:42 pkt_dts_time:0.042 size:31878
muxer <- type:video pkt_pts:83 pkt_pts_time:0.083 pkt_dts:83 pkt_dts_time:0.083 size:34437
muxer <- type:video pkt_pts:125 pkt_pts_time:0.125 pkt_dts:125 pkt_dts_time:0.125 size:26936
muxer <- type:video pkt_pts:167 pkt_pts_time:0.167 pkt_dts:167 pkt_dts_time:0.167 size:22856
muxer <- type:video pkt_pts:209 pkt_pts_time:0.209 pkt_dts:209 pkt_dts_time:0.209 size:22355
muxer <- type:video pkt_pts:250 pkt_pts_time:0.25 pkt_dts:250 pkt_dts_time:0.25 size:14882
muxer <- type:video pkt_pts:292 pkt_pts_time:0.292 pkt_dts:292 pkt_dts_time:0.292 size:3548
muxer <- type:video pkt_pts:334 pkt_pts_time:0.334 pkt_dts:334 pkt_dts_time:0.334 size:2954
muxer <- type:video pkt_pts:417 pkt_pts_time:0.417 pkt_dts:417 pkt_dts_time:0.417 size:138
muxer <- type:video pkt_pts:375 pkt_pts_time:0.375 pkt_dts:418 pkt_dts_time:0.418 size:140
[matroska @ 0x7fb88c0eaa00] pts (375) < dts (418) in stream 0
av_interleaved_write_frame(): Invalid argument

I believe the error can be ignored, as ffmpeg will attempt to fix timestamps in the output file.

--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -486,7 +486,6 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
     if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
         av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
                av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
-        return AVERROR(EINVAL);

     av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",

With the change above, ffmpeg on master warns me but is able to process my entire input file without crashing:

[matroska @ 0x7fabdc034600] pts (375) < dts (418) in stream 0
[matroska @ 0x7fabdc034600] Non-monotonous DTS in output stream 0:0; previous: 418, current: 375; changing to 418. This may result in incorrect timestamps in the output file.
frame=  152 fps=0.0 q=-1.0 Lsize=    4475kB time=00:00:06.48 bitrate=5654.2kbits/s
video:4168kB audio:303kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.089263%

Attachments (1)

test.mkv (256.0 KB ) - added by Aman 10 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by Carl Eugen Hoyos, 10 years ago

Is there a crash or do you want to report that the remuxing does not succeed?

comment:2 by Aman, 10 years ago

It does not succeed, i.e. exit status 1. Sorry for the ambiguous wording.

comment:3 by Carl Eugen Hoyos, 10 years ago

Keywords: av_interleaved_write_frame added
Summary: "pts < dts" causing crash in av_interleaved_write_frame(): Invalid argument"pts < dts" causing remuxing failure

Please upload the input sample.

by Aman, 10 years ago

Attachment: test.mkv added

comment:4 by Carl Eugen Hoyos, 10 years ago

Keywords: mkv h264 added
Status: newopen

comment:5 by Carl Eugen Hoyos, 10 years ago

Keywords: regression added
Priority: normalimportant
Reproduced by developer: set
Summary: "pts < dts" causing remuxing failure"pts < dts" causing remuxing failure for h264 from mkv to mkv

Regression since 8b73a3f6

comment:6 by ramitb, 10 years ago

My 2 cents, if the original video has an error in then it's okay for the remuxed video to also contain that error. So this code while not perfect is along the right track, just that it shouldnn't return an error, rather just log the warning on the screen if the video is being remuxed

comment:7 by Carl Eugen Hoyos, 10 years ago

Why do you think that the original video has an error?

comment:8 by ramitb, 10 years ago

Cc: added

comment:9 by Jacob, 6 years ago

Cc: added

comment:10 by Elon Musk, 5 years ago

Resolution: fixed
Status: openclosed
Note: See TracTickets for help on using tickets.