1

I want to concat a few videos from my phone camera:

ffmpeg -f concat -i /storage/emulated/0/tmp.sc -codec copy -threads 3 -strict -2 -y /storage/emulated/0/final.mp4

Basically it works good.

But I would like also to process each video before concat:

ffmpeg -i /storage/emulated/0/original0.mp4 -filter_complex scale=1137:640, crop=640:640:248:0, transpose=1 -threads 3 -preset ultrafast -strict -2 -y /storage/emulated/0/processed0.mp4

it also works as it should. However, when I try to concat the processed videos - the final video is always broken and sometimes I get an error non-monotonous DTS in output stream.

tmp.sc:

file '/storage/emulated/0/processed0.mp4'
file '/storage/emulated/0/processed1.mp4'

filtering file1:

ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
built on Sep  6 2015 00:45:52 with gcc 4.8 (GCC)
configuration: --target-os=linux --cross-prefix=/home/mike/Downloads/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/mike/Downloads/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/mike/Downloads/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/mike/Downloads/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/mike/Downloads/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/mike/Downloads/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
libavutil      54.  7.100 / 54.  7.100
libavcodec     56.  1.100 / 56.  1.100
libavformat    56.  4.101 / 56.  4.101
libavdevice    56.  0.100 / 56.  0.100
libavfilter     5.  1.100 /  5.  1.100
libswscale      3.  0.100 /  3.  0.100
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  0.100 / 53.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/original0.mp4':
Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: isommp42
creation_time   : 2015-09-16 21:55:26
Duration: 00:00:02.39, start: 0.000000, bitrate: 67424 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 65770 kb/s, SAR 65536:65536 DAR 16:9, 30.01 fps, 30 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time   : 2015-09-16 21:55:26
handler_name    : VideoHandle
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 319 kb/s (default)
Metadata:
creation_time   : 2015-09-16 21:55:26
handler_name    : SoundHandle
[libx264 @ 0xb5829000] using SAR=1462/1463
[libx264 @ 0xb5829000] using cpu capabilities: none!
[libx264 @ 0xb5829000] profile Constrained Baseline, level 3.1
[libx264 @ 0xb5829000] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to '/storage/emulated/0/processed0.mp4':
Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: isommp42
encoder         : Lavf56.4.101
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x640 [SAR 10233:10240 DAR 10233:10240], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
encoder         : Lavc56.1.100 libx264
Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time   : 2015-09-16 21:55:26
handler_name    : SoundHandle
encoder         : Lavc56.1.100 aac
Stream mapping:
Stream #0:0 (h264) -> scale (graph 0)
transpose (graph 0) -> Stream #0:0 (libx264)
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
frame=    6 fps=0.0 q=25.0 size=      20kB time=00:00:01.00 bitrate= 160.3kbits/s
frame=   16 fps= 15 q=26.0 size=     157kB time=00:00:01.00 bitrate=1275.0kbits/s
frame=   27 fps= 16 q=26.0 size=     313kB time=00:00:01.00 bitrate=2542.3kbits/s
frame=   29 fps= 13 q=26.0 size=     339kB time=00:00:01.84 bitrate=1509.4kbits/s
frame=   38 fps= 14 q=26.0 size=     466kB time=00:00:02.01 bitrate=1899.6kbits/s
frame=   49 fps= 15 q=26.0 size=     627kB time=00:00:02.01 bitrate=2552.8kbits/s
frame=   60 fps= 16 q=26.0 size=     785kB time=00:00:02.01 bitrate=3198.9kbits/s
frame=   69 fps= 16 q=26.0 size=     915kB time=00:00:02.35 bitrate=3185.4kbits/s
frame=   70 fps= 15 q=-1.0 Lsize=     990kB time=00:00:02.39 bitrate=3387.6kbits/s
video:954kB audio:33kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.333486%
[libx264 @ 0xb5829000] frame I:1     Avg QP:20.00  size: 15444
[libx264 @ 0xb5829000] frame P:69    Avg QP:25.90  size: 13926
[libx264 @ 0xb5829000] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0xb5829000] mb P  I16..4: 72.5%  0.0%  0.0%  P16..4: 24.5%  0.0%  0.0%  0.0%  0.0%    skip: 3.0%
[libx264 @ 0xb5829000] coded y,uvDC,uvAC intra: 85.2% 7.9% 0.0% inter: 52.6% 14.6% 0.0%
[libx264 @ 0xb5829000] i16 v,h,dc,p: 13% 11% 65% 11%
[libx264 @ 0xb5829000] i8c dc,h,v,p: 62% 18% 16%  4%
[libx264 @ 0xb5829000] kb/s:3347.43

filtering file2:

ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
built on Sep  6 2015 00:45:52 with gcc 4.8 (GCC)
configuration: --target-os=linux --cross-prefix=/home/mike/Downloads/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/mike/Downloads/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/mike/Downloads/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/mike/Downloads/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/mike/Downloads/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/mike/Downloads/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
libavutil      54.  7.100 / 54.  7.100
libavcodec     56.  1.100 / 56.  1.100
libavformat    56.  4.101 / 56.  4.101
libavdevice    56.  0.100 / 56.  0.100
libavfilter     5.  1.100 /  5.  1.100
libswscale      3.  0.100 /  3.  0.100
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  0.100 / 53.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/original1.mp4':
Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: isommp42
creation_time   : 2015-09-16 21:56:23
Duration: 00:00:02.41, start: 0.000000, bitrate: 71677 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 64782 kb/s, SAR 65536:65536 DAR 16:9, 29.93 fps, 30 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time   : 2015-09-16 21:56:23
handler_name    : VideoHandle
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 320 kb/s (default)
Metadata:
creation_time   : 2015-09-16 21:56:23
handler_name    : SoundHandle
[libx264 @ 0xb5829000] using SAR=1462/1463
[libx264 @ 0xb5829000] using cpu capabilities: none!
[libx264 @ 0xb5829000] profile Constrained Baseline, level 3.1
[libx264 @ 0xb5829000] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to '/storage/emulated/0/processed1.mp4':
Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: isommp42
encoder         : Lavf56.4.101
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x640 [SAR 10233:10240 DAR 10233:10240], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
encoder         : Lavc56.1.100 libx264
Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time   : 2015-09-16 21:56:23
handler_name    : SoundHandle
encoder         : Lavc56.1.100 aac
Stream mapping:
Stream #0:0 (h264) -> scale (graph 0)
transpose (graph 0) -> Stream #0:0 (libx264)
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
frame=    3 fps=0.0 q=0.0 size=       0kB time=00:00:01.00 bitrate=   0.4kbits/s
frame=   13 fps= 12 q=26.0 size=     146kB time=00:00:01.00 bitrate=1190.2kbits/s
frame=   23 fps= 14 q=26.0 size=     269kB time=00:00:01.00 bitrate=2197.7kbits/s
frame=   29 fps= 14 q=26.0 size=     343kB time=00:00:01.34 bitrate=2088.9kbits/s
frame=   34 fps= 13 q=26.0 size=     402kB time=00:00:02.00 bitrate=1643.0kbits/s
frame=   44 fps= 14 q=26.0 size=     529kB time=00:00:02.00 bitrate=2162.5kbits/s
frame=   55 fps= 15 q=26.0 size=     664kB time=00:00:02.00 bitrate=2714.4kbits/s
frame=   66 fps= 15 q=26.0 size=     777kB time=00:00:02.06 bitrate=3081.1kbits/s
frame=   72 fps= 15 q=-1.0 Lsize=     923kB time=00:00:02.41 bitrate=3135.8kbits/s
video:886kB audio:34kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.360588%
[libx264 @ 0xb5829000] frame I:1     Avg QP:20.00  size: 46850
[libx264 @ 0xb5829000] frame P:71    Avg QP:26.00  size: 12109
[libx264 @ 0xb5829000] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0xb5829000] mb P  I16..4: 49.9%  0.0%  0.0%  P16..4: 38.4%  0.0%  0.0%  0.0%  0.0%    skip:11.8%
[libx264 @ 0xb5829000] coded y,uvDC,uvAC intra: 70.3% 10.2% 0.0% inter: 44.6% 12.7% 0.0%
[libx264 @ 0xb5829000] i16 v,h,dc,p: 27% 16% 45% 13%
[libx264 @ 0xb5829000] i8c dc,h,v,p: 49% 17% 25% 10%
[libx264 @ 0xb5829000] kb/s:3021.96

concating them:

ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
built on Sep  6 2015 00:45:52 with gcc 4.8 (GCC)
configuration: --target-os=linux --cross-prefix=/home/mike/Downloads/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/mike/Downloads/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/mike/Downloads/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/mike/Downloads/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/mike/Downloads/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/mike/Downloads/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
libavutil      54.  7.100 / 54.  7.100
libavcodec     56.  1.100 / 56.  1.100
libavformat    56.  4.101 / 56.  4.101
libavdevice    56.  0.100 / 56.  0.100
libavfilter     5.  1.100 /  5.  1.100
libswscale      3.  0.100 /  3.  0.100
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  0.100 / 53.  0.100
Input #0, concat, from '/storage/emulated/0/tmp.sc':
Duration: N/A, start: 0.000000, bitrate: 3461 kb/s
Stream #0:0: Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x640 [SAR 1462:1463 DAR 1462:1463], 3349 kb/s, SAR 10233:10240 DAR 10233:10240, 30 fps, 30 tbr, 15360 tbn, 60 tbc
Stream #0:1: Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 112 kb/s
Output #0, mp4, to '/storage/emulated/0/final.mp4':
Metadata:
encoder         : Lavf56.4.101
Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 640x640 [SAR 10233:10240 DAR 10233:10240], q=2-31, 3349 kb/s, 30 fps, 15360 tbn, 15360 tbc
Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 112 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  142 fps=0.0 q=-1.0 Lsize=    1912kB time=00:00:04.84 bitrate=3230.1kbits/s
video:1840kB audio:67kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.281813%

UPDATE: so I guess the problem is with the filtering - after using it the output videos are not uniform anymore and can't be concated properly. I updated the logs.

UPDATE2: full output and actual commands

UPDATE3: here're sample videos:

  • Could you please give me a hint on direct audio stream? Can't find anything about it. So the final video is too long (24 sec instead of 3.5) has only few frames and it looks like timestamps are broken because I can't navigate using VLC. And yes that's the whole line, as I said it works perfectly with the original videos. – user1049280 Sep 13 '15 at 22:39
3
+100

concat filter

Since you're filtering anyway, just use the concat filter and do everything in one command:

ffmpeg -i input0 -i input1 -i input2 -filter_complex \
"[0:v]scale,crop,transpose,setpts=PTS-STARTPTS[v0]; \
 [1:v]scale,crop,transpose,setpts=PTS-STARTPTS[v1]; \
 [2:v]scale,crop,transpose,setpts=PTS-STARTPTS[v2]; \
 [v0][0:a][v1][1:a][v2][2:a]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -strict experimental output.mp4
  • For this filter to work correctly, all segments must start at timestamp 0 (such as with setpts as shown above).

  • All corresponding streams must have the same parameters in all segments; the filtering system will automatically select a common pixel format for video streams, and a common sample format, sample rate, and channel layout for audio streams. Other settings, such as frame size, must be converted explicitly by the user.

concat demuxer

If you just want to concatenate without re-encoding you can try the concat demuxer.

  1. Make a text file listing your inputs:

    file '/path/to/input0'
    file '/path/to/input1'
    file '/path/to/input2'
    
  2. Concatenate with ffmpeg:

    ffmpeg -f concat -i input.txt -c copy output.mp4
    

All files must have the same streams (same codecs, same time base, etc.).

  • thanks it works! However my task is kinda specific and I need to combine these videos in diferrent combinations so it would take less time if I just concat them instead of re-encode every time. I believe that there's a problem with my filtering thing. Probably I should set some additional encdoing params so the processed videos could be concated as well. – user1049280 Sep 14 '15 at 7:13
  • 1
    You can't use any filtering if you're going to concat them without reencoding. It's not possible to apply a scale/crop without reencoding, sorry. – Ely Sep 16 '15 at 16:40
  • Thanks for your attention, I understand that videos must be re-encoded to perform scale/rotate/crop. But I want to be able to concat them after it without re-encoding. – user1049280 Sep 16 '15 at 18:42
  • 1
    @user1049280 I don't completely understand your desired workflow, but I added info on the concat demuxer which may provide whatever it is you need. – llogan Sep 16 '15 at 18:55
  • I'm using concat demuxer right now :) The problem is that if I use concat demuxer to concat original videos - everything is fine, but if I use it to concat cropped/scaled/rotated videos - it doesn't work. Concat filter is a good workaround but I need to combine videos in different sequences like 1-2-3, 1-3-2, 3-2-1 etc. and re-encoding the whole list everytime is just wasting of time. – user1049280 Sep 16 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.