连接过滤器

连接过滤器

我想从我的手机相机中连接一些视频:

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

基本上,它运作良好。

但我还想在连接之前处理每个视频:

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

它也正常工作。但是,当我尝试连接处理过的视频时,最终的视频总是损坏,有时还会出错non-monotonous DTS in output stream

tmp.sc:

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

过滤文件1:

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

过滤文件2:

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

将它们连接起来:

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%

更新:所以我猜问题出在过滤上——使用它之后,输出视频不再统一,无法正确连接。我更新了日志。

更新2:完整输出和实际命令

更新3:以下是示例视频:

答案1

连接过滤器

既然你无论如何都要过滤,只需使用连接过滤器用一个命令完成所有操作:

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
  • 为了使此过滤器正常工作,所有段必须从时间戳 0 开始(如上setpts所示)。

  • 所有对应流必须在所有片段中具有相同的参数;过滤系统将自动为视频流选择通用像素格式,为音频流选择通用采样格式、采样率和通道布局。其他设置(例如帧大小)必须由用户明确转换。

连接解复用器

如果你只是想连接而不重新编码,你可以尝试连接解复用器

  1. 创建一个文本文件,列出您的输入:

    file '/path/to/input0'
    file '/path/to/input1'
    file '/path/to/input2'
    
  2. 连接ffmpeg

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

所有文件必须具有相同的流(相同的编解码器、相同的时间基准等)。

相关内容