[FFmpeg-user] Reset Timestamps for mp4 Video Segments

Jeremy Whitaker jeremywhitaker91 at gmail.com
Wed Jan 16 23:29:12 CET 2013


Hello, I've been having some trouble lately with the -reset_timestamps
feature in creating video segments. My goal is basically to take an mp4
video file as input, and segment it into smaller mp4 videos which can then
be played one after another in VLC. The problem is that whenever I try to
play the segmented mp4 video files in VLC, there seems to be a pause
between each of the video files by some factor of the segment length. For
example, if I'm segmenting a 30 second mp4 video file into 6 5-second long
mp4 video segments, VLC will pause for 5 seconds after playing the first
video segment, for 10 seconds after playing the second video segment, for
15 after the third, and so on. I looked into any options that are supported
by FFmpeg to resolve the issue, and it seems like the option
"-reset_timestamps" was added in to address this exact issue. The
description of the "-reset_timestamps" option is:

Reset timestamps at the begin of each segment, so that each segment will
start with near-zero timestamps. It is meant to ease the playback of the
generated segments. May not work with some combinations of muxers/codecs.
It is set to 0 by default.

Unfortunately, when I try this option I get an error saying that the PTS is
less than ( < ) the DTS. I think this issue has been discussed before, but
I was wondering if it's even possible to make it so that there's no delay
between the mp4 video segments when playing them back in VLC. I suspect
that it has something to do with the individual segments' metadata, since
VLC also has a 5 second delay when I try to play only the 2nd mp4 video
segment from the example above. I'm beginning to think that this issue
might actually be pretty complicated, so any help would be greatly
appreciated. Here's an example ffmpeg command that I use and it's terminal
output:

ffmpeg -i webcam.mp4 -vcodec libx264 -b:v 500k -r 29.97 -g 12 -keyint_min
12 -map 0 -f segment -segment_time 5 -reset_timestamps 1 output%01d.mp4
ffmpeg version git-Mon Jan 14 22:59:05 2013 +0100-30981a9 Copyright (c)
2000-2013 the FFmpeg developers
  built on Jan 14 2013 15:32:55 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libass --enable-libfaac
--enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb
--enable-libopencore-amrwb --enable-librtmp --enable-libtheora
--enable-libvorbis --enable-libvpx --enable-x11grab --enable-libx264
--enable-nonfree --enable-version3
  libavutil      52. 14.100 / 52. 14.100
  libavcodec     54. 89.100 / 54. 89.100
  libavformat    54. 59.107 / 54. 59.107
  libavdevice    54.  3.102 / 54.  3.102
  libavfilter     3. 32.100 /  3. 32.100
  libswscale      2.  1.103 /  2.  1.103
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'webcam.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.22.100
  Duration: 00:00:30.00, start: 0.000000, bitrate: 1180 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
640x480, 1177 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc
    Metadata:
      handler_name    : VideoHandler
[libx264 @ 0x304e280] using cpu capabilities: MMX2 SSE2Fast SSSE3
FastShuffle SSE4.2
[libx264 @ 0x304e280] profile High, level 3.0
[libx264 @ 0x304e280] 264 - core 129 r2 1cffe9f - H.264/MPEG-4 AVC codec -
Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1
ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00
mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11
fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1
sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0
constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1
weightb=1 open_gop=0 weightp=2 keyint=12 keyint_min=7 scenecut=40
intra_refresh=0 rc_lookahead=12 rc=abr mbtree=1 bitrate=500 ratetol=1.0
qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, segment, to 'output%01d.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.59.107
    Stream #0:0(und): Video: h264, yuv420p, 640x480, q=-1--1, 500 kb/s, 90k
tbn, 29.97 tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
Press [q] to stop, [?] for help
pts (400) < dts (1200) in stream 0A time=00:00:03.93 bitrate=N/A
av_interleaved_write_frame(): Invalid argument



Here's another command I tried without the "-reset_timestamps" option. This
one doesn't give me an error, but there's a delay between the segments when
playing them back in VLC:



ffmpeg -i webcam.mp4 -vcodec libx264 -b:v 500k -r 29.97 -g 12 -keyint_min
12 -map 0 -f segment -segment_time 5 -y output%01d.mp4
ffmpeg version git-Mon Jan 14 22:59:05 2013 +0100-30981a9 Copyright (c)
2000-2013 the FFmpeg developers
  built on Jan 14 2013 15:32:55 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libass --enable-libfaac
--enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb
--enable-libopencore-amrwb --enable-librtmp --enable-libtheora
--enable-libvorbis --enable-libvpx --enable-x11grab --enable-libx264
--enable-nonfree --enable-version3
  libavutil      52. 14.100 / 52. 14.100
  libavcodec     54. 89.100 / 54. 89.100
  libavformat    54. 59.107 / 54. 59.107
  libavdevice    54.  3.102 / 54.  3.102
  libavfilter     3. 32.100 /  3. 32.100
  libswscale      2.  1.103 /  2.  1.103
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'webcam.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.22.100
  Duration: 00:00:30.00, start: 0.000000, bitrate: 1180 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
640x480, 1177 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc
    Metadata:
      handler_name    : VideoHandler
[libx264 @ 0x2b7e1e0] using cpu capabilities: MMX2 SSE2Fast SSSE3
FastShuffle SSE4.2
[libx264 @ 0x2b7e1e0] profile High, level 3.0
[libx264 @ 0x2b7e1e0] 264 - core 129 r2 1cffe9f - H.264/MPEG-4 AVC codec -
Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1
ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00
mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11
fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1
sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0
constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1
weightb=1 open_gop=0 weightp=2 keyint=12 keyint_min=7 scenecut=40
intra_refresh=0 rc_lookahead=12 rc=abr mbtree=1 bitrate=500 ratetol=1.0
qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, segment, to 'output%01d.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.59.107
    Stream #0:0(und): Video: h264, yuv420p, 640x480, q=-1--1, 500 kb/s, 90k
tbn, 29.97 tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
Press [q] to stop, [?] for help
frame=  900 fps=100 q=32766.0 Lsize=N/A time=00:00:29.96 bitrate=N/A
video:1915kB audio:0kB subtitle:0 global headers:0kB muxing overhead
-100.001122%
[libx264 @ 0x2b7e1e0] frame I:75    Avg QP:24.78  size: 16586
[libx264 @ 0x2b7e1e0] frame P:273   Avg QP:27.98  size:  1839
[libx264 @ 0x2b7e1e0] frame B:552   Avg QP:29.90  size:   388
[libx264 @ 0x2b7e1e0] consecutive B-frames: 13.7%  6.0% 23.0% 57.3%
[libx264 @ 0x2b7e1e0] mb I  I16..4: 12.8% 62.6% 24.7%
[libx264 @ 0x2b7e1e0] mb P  I16..4:  1.5%  1.1%  0.1%  P16..4: 38.9%  4.3%
 3.0%  0.0%  0.0%    skip:51.1%
[libx264 @ 0x2b7e1e0] mb B  I16..4:  0.1%  0.0%  0.0%  B16..8: 21.1%  0.3%
 0.0%  direct: 0.4%  skip:78.0%  L0:34.7% L1:64.5% BI: 0.8%
[libx264 @ 0x2b7e1e0] final ratefactor: 25.70
[libx264 @ 0x2b7e1e0] 8x8 transform intra:60.3% inter:84.3%
[libx264 @ 0x2b7e1e0] coded y,uvDC,uvAC intra: 60.7% 89.3% 47.7% inter:
2.2% 11.3% 0.3%
[libx264 @ 0x2b7e1e0] i16 v,h,dc,p: 26% 26% 12% 36%
[libx264 @ 0x2b7e1e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 18% 15%  7%  7%
 9%  6%  9%  6%
[libx264 @ 0x2b7e1e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 24% 11%  6% 10%
 9%  6%  6%  5%
[libx264 @ 0x2b7e1e0] i8c dc,h,v,p: 52% 22% 19%  7%
[libx264 @ 0x2b7e1e0] Weighted P-Frames: Y:7.7% UV:6.2%
[libx264 @ 0x2b7e1e0] ref P L0: 74.0%  8.0% 14.2%  3.7%  0.0%
[libx264 @ 0x2b7e1e0] ref B L0: 88.9%  9.9%  1.2%
[libx264 @ 0x2b7e1e0] ref B L1: 96.0%  4.0%
[libx264 @ 0x2b7e1e0] kb/s:522.17


More information about the ffmpeg-user mailing list