[FFmpeg-user] How to split and then concatenate a video file?

Žygimantas Bruzgys un1x0nly at gmail.com
Wed Feb 20 15:17:28 CET 2013


On Wed, Feb 20, 2013 at 2:16 PM, Stefano Sabatini <stefasab at gmail.com> wrote:
>> zybru at hp-6530b-bs:~/Desktop$ ffmpeg -i small.mp4 -f segment -vcodec
>> h264 -acodec mp3 -segment_time 2 -map 0 out%03d.mp4
>
> Did you try with -reset_timestamps 1?

Let me try it now! (just fyi, I've recompiled ffmpeg today from git head)

Output:

zybru at hp-6530b-bs:~/Desktop$ ffmpeg -i small.mp4 -f segment -vcodec
h264 -acodec mp3 -segment_time 2 -map 0 -reset_timestamps 0
out%03d.mp4
ffmpeg version N-50129-g951d39f Copyright (c) 2000-2013 the FFmpeg
developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'small.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.61.100
  Duration: 00:01:00.00, start: 0.000000, bitrate: 1361 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 1239 kb/s, 24 fps, 24 tbr, 16k tbn, 48
tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz,
stereo, fltp, 128 kb/s
    Metadata:
      handler_name    : SoundHandler
[libx264 @ 0x278df80] using SAR=1/1
[libx264 @ 0x278df80] using cpu capabilities: MMX2 SSE2Fast SSSE3
FastShuffle SSE4.1 Cache64
[libx264 @ 0x278df80] profile High, level 4.0
[libx264 @ 0x278df80] 264 - core 120 r2151 a3f4407 - H.264/MPEG-4 AVC
codec - Copyleft 2003-2011 - 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=3 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=250
keyint_min=24 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf
mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40
aq=1:1.00
Output #0, segment, to 'out%03d.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.63.100
    Stream #0:0(und): Video: h264, yuv420p, 1920x818 [SAR 1:1 DAR
960:409], q=-1--1, 90k tbn, 24 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: mp3, 44100 Hz, stereo, fltp
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
  Stream #0:1 -> #0:1 (aac -> libmp3lame)
Press [q] to stop, [?] for help
frame= 1440 fps= 11 q=28.0 Lsize=N/A time=00:01:00.00 bitrate=N/A
dup=16 drop=0
video:11561kB audio:938kB subtitle:0 global headers:0kB muxing
overhead -100.000172%
[libx264 @ 0x278df80] frame I:16    Avg QP:15.08  size: 37096
[libx264 @ 0x278df80] frame P:1016  Avg QP:20.03  size:  9881
[libx264 @ 0x278df80] frame B:408   Avg QP:23.28  size:  2954
[libx264 @ 0x278df80] consecutive B-frames: 59.2%  5.7%  9.8% 25.3%
[libx264 @ 0x278df80] mb I  I16..4: 42.5% 47.1% 10.4%
[libx264 @ 0x278df80] mb P  I16..4:  7.7% 10.0%  0.3%  P16..4: 23.7%
4.5%  1.2%  0.0%  0.0%    skip:52.5%
[libx264 @ 0x278df80] mb B  I16..4:  0.7%  0.7%  0.0%  B16..8: 26.8%
1.1%  0.1%  direct: 0.4%  skip:70.3%  L0:51.2% L1:47.9% BI: 0.9%
[libx264 @ 0x278df80] 8x8 transform intra:54.6% inter:86.7%
[libx264 @ 0x278df80] coded y,uvDC,uvAC intra: 22.8% 29.1% 2.1% inter:
4.7% 6.6% 0.0%
[libx264 @ 0x278df80] i16 v,h,dc,p: 32% 30% 16% 22%
[libx264 @ 0x278df80] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 22% 34%  3%
3%  3%  4%  3%  3%
[libx264 @ 0x278df80] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 21% 19%  5%
6%  6%  5%  4%  3%
[libx264 @ 0x278df80] i8c dc,h,v,p: 72% 14% 13%  2%
[libx264 @ 0x278df80] Weighted P-Frames: Y:0.4% UV:0.2%
[libx264 @ 0x278df80] ref P L0: 78.4%  8.7%  9.7%  3.2%  0.0%
[libx264 @ 0x278df80] ref B L0: 90.6%  8.1%  1.2%
[libx264 @ 0x278df80] ref B L1: 95.0%  5.0%
[libx264 @ 0x278df80] kb/s:1578.34

zybru at hp-6530b-bs:~/Desktop$ head -n 2 concat.lst; tail -n 2 concat.lst
file 'out000.mp4'
file 'out001.mp4'
file 'out012.mp4'
file 'out013.mp4'

zybru at hp-6530b-bs:~/Desktop$ ffmpeg -f concat -i concat.lst -c copy joined.mp4
ffmpeg version N-50129-g951d39f Copyright (c) 2000-2013 the FFmpeg developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
[concat @ 0x1e06fc0] Estimating duration from bitrate, this may be inaccurate
Input #0, concat, from 'concat.lst':
  Duration: 00:00:00.03, start: 0.000000, bitrate: 76 kb/s
    Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 1371 kb/s, 21.52 fps, 24 tbr, 12288
tbn, 48 tbc
    Stream #0:1: Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo,
s16p, 126 kb/s
Output #0, mp4, to 'joined.mp4':
  Metadata:
    encoder         : Lavf54.63.100
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], q=2-31, 1371 kb/s, 21.52 fps, 12288
tbn, 12288 tbc
    Stream #0:1: Audio: mp3 (i[0][0][0] / 0x0069), 44100 Hz, stereo, 126 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 1440 fps=0.0 q=-1.0 Lsize=   12560kB time=00:01:03.50
bitrate=1620.2kbits/s
video:11561kB audio:938kB subtitle:0 global headers:0kB muxing
overhead 0.490097%
zybru at hp-6530b-bs:~/Desktop$ ffprobe joined.mp4
ffprobe version N-50129-g951d39f Copyright (c) 2007-2013 the FFmpeg developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'joined.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.63.100
  Duration: 00:01:03.51, start: 0.000000, bitrate: 1620 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 1491 kb/s, 22.69 fps, 24 tbr, 12288
tbn, 48 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz,
stereo, s16p, 128 kb/s
    Metadata:
      handler_name    : SoundHandler

The output duration is longer, which means that there are glitches in
between split points (I've checked that by playing the video).

I think it's worth to notice my observations from today. small.mp4
adio starts a couple of milliseconds earlier than video. It is
probably because I actually cut that piece from the middle of longer
clip using ffmpeg with -ss and -t flags.

So, I've realised that it would be a good idea to do the same test on
the whole original clip. Here is the output:

zybru at hp-6530b-bs:~/Desktop$ ffmpeg -i
Sintel-Third-Open-Movie-by-Blender-Foundation-1080p.mp4 -f segment
-segment_time 2 -map 0 -c copy out%03d.mp4
ffmpeg version N-50129-g951d39f Copyright (c) 2000-2013 the FFmpeg developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from
'Sintel-Third-Open-Movie-by-Blender-Foundation-1080p.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2010-09-29 12:52:38
  Duration: 00:14:48.00, start: 0.000000, bitrate: 3075 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 2941 kb/s, 24 fps, 24 tbr, 1k tbn, 48
tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz,
stereo, fltp, 127 kb/s
    Metadata:
      creation_time   : 2010-09-29 12:53:05
      handler_name    : (C) 2007 Google Inc. v08.13.2007.
Output #0, segment, to 'out%03d.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf54.63.100
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], q=2-31, 2941 kb/s, 24 fps, 90k tbn, 24
tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, 127 kb/s
    Metadata:
      creation_time   : 2010-09-29 12:53:05
      handler_name    : (C) 2007 Google Inc. v08.13.2007.
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=21312 fps=1425 q=-1.0 Lsize=N/A time=00:14:47.99 bitrate=N/A
video:318907kB audio:13874kB subtitle:0 global headers:0kB muxing
overhead -100.000006%

zybru at hp-6530b-bs:~/Desktop$ for f in `ls out*`; do echo "file '$f'"
>> concat.lst; done
zybru at hp-6530b-bs:~/Desktop$ head -n 2 concat.lst; tail -n 2 concat.lst
file 'out000.mp4'
file 'out001.mp4'
file 'out442.mp4'
file 'out443.mp4'
zybru at hp-6530b-bs:~/Desktop$ ffmpeg -y -f concat -i concat.lst -c copy
joined.mp4
ffmpeg version N-50129-g951d39f Copyright (c) 2000-2013 the FFmpeg developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
[concat @ 0x258ffc0] Estimating duration from bitrate, this may be inaccurate
Input #0, concat, from 'concat.lst':
  Duration: 00:00:00.52, start: 0.000000, bitrate: 122 kb/s
    Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 10 kb/s, 21.19 fps, 24 tbr, 12288 tbn,
48 tbc
    Stream #0:1: Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo,
fltp, 111 kb/s
Output #0, mp4, to 'joined.mp4':
  Metadata:
    encoder         : Lavf54.63.100
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], q=2-31, 10 kb/s, 21.19 fps, 12288 tbn,
12288 tbc
    Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, 111 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=21312 fps=316 q=-1.0 Lsize=  333633kB time=00:16:45.30
bitrate=2718.7kbits/s
video:318907kB audio:13874kB subtitle:0 global headers:0kB muxing
overhead 0.255866%

zybru at hp-6530b-bs:~/Desktop$ ffprobe joined.mp4
ffprobe version N-50129-g951d39f Copyright (c) 2007-2013 the FFmpeg developers
  built on Feb 20 2013 14:47:49 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-gpl --enable-libfaac --enable-libmp3lame
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-nonfree --enable-version3 --enable-x11grab
--disable-stripping --enable-debug=3 --extra-cflags=-gstabs+
--disable-optimizations
  libavutil      52. 17.102 / 52. 17.102
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.100 / 54. 63.100
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 38.103 /  3. 38.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2e0c420] Invalid SampleDelta in STTS -10895766
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2e0c420] Invalid SampleDelta in STTS -39114675
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'joined.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.63.100
  Duration: 00:01:58.61, start: 0.000000, bitrate: 23043 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1920x818 [SAR 1:1 DAR 960:409], 2598 kb/s, 21.20 fps, 24 tbr, 12288
tbn, 48 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz,
stereo, fltp, 113 kb/s
    Metadata:
      handler_name    : SoundHandler

If you play the movie using VLC, the audio runs smooth, but video gets
'stuck' every couple of seconds for couple of milliseconds.

Žygimantas


More information about the ffmpeg-user mailing list