ffmpeg xfade 不能很好地与 crossfade 配合使用
Posted
技术标签:
【中文标题】ffmpeg xfade 不能很好地与 crossfade 配合使用【英文标题】:ffmpeg xfade not working well with acrossfade 【发布时间】:2021-04-15 09:47:15 【问题描述】:我在使用 xfade 和 crossfade 时遇到问题。
我有两个视频 t1.mp4 和 t2.mp4 具有相同的 fps、分辨率等。两个视频都是 15 秒长。
现在我想要在两者之间进行一秒钟的淡入淡出过渡。
没有交叉淡入,
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "xfade=transition=fade:duration=1:offset=14,format=yuv420p" output.mp4
检查所有帧后,过渡看起来不错。有一些重叠的帧具有褪色效果。 请看下面两个连续的帧。
但结果的第二部分没有音频,这就是我决定使用 acrossfade 的原因。
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "xfade=transition=fade:duration=1:offset=14,format=yuv420p;acrossfade=d=1" output.mp4
现在看来过渡效果不完整,有淡入淡出效果的重叠帧较少,过渡锐利。请看下面两个连续的帧,与上面相同的帧数。
请问我在这里错过了什么?
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "xfade=transition=fade:duration=1:offset=14,format=yuv420p;acrossfade=d=1" output.mp4
ffmpeg version 4.3.1-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 8 (Debian 8.3.0-6)
configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 't1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:15.02, start: 0.000000, bitrate: 2447 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2315 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 't2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:15.02, start: 0.000000, bitrate: 1643 kb/s
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 1509 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 (h264) -> xfade:main
Stream #0:1 (aac) -> acrossfade:crossfade0
Stream #1:0 (h264) -> xfade:xfade
Stream #1:1 (aac) -> acrossfade:crossfade1
format -> Stream #0:0 (libx264)
acrossfade -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
[libx264 @ 0x5963dc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x5963dc0] profile High, level 4.0, 4:2:0, 8-bit
[libx264 @ 0x5963dc0] 264 - core 161 r3018 db0d417 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - 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=34 lookahead_threads=5 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=25 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, mp4, to 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080, q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
Metadata:
encoder : Lavc58.91.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
encoder : Lavc58.91.100 aac
frame= 726 fps= 69 q=-1.0 Lsize= 7114kB time=00:00:29.00 bitrate=2009.4kbits/s dup=1 drop=0 speed=2.76x
video:6633kB audio:456kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.351093%
[libx264 @ 0x5963dc0] frame I:8 Avg QP:16.38 size: 96958
[libx264 @ 0x5963dc0] frame P:319 Avg QP:19.49 size: 14630
[libx264 @ 0x5963dc0] frame B:399 Avg QP:23.99 size: 3379
[libx264 @ 0x5963dc0] consecutive B-frames: 4.5% 63.6% 8.7% 23.1%
[libx264 @ 0x5963dc0] mb I I16..4: 16.4% 61.0% 22.6%
[libx264 @ 0x5963dc0] mb P I16..4: 3.5% 9.8% 0.7% P16..4: 26.6% 4.9% 1.9% 0.0% 0.0% skip:52.5%
[libx264 @ 0x5963dc0] mb B I16..4: 0.3% 0.6% 0.2% B16..8: 13.8% 0.9% 0.1% direct: 0.9% skip:83.1% L0:44.1% L1:52.3% BI: 3.6%
[libx264 @ 0x5963dc0] 8x8 transform intra:67.2% inter:81.6%
[libx264 @ 0x5963dc0] coded y,uvDC,uvAC intra: 26.3% 38.2% 8.9% inter: 3.2% 5.8% 0.1%
[libx264 @ 0x5963dc0] i16 v,h,dc,p: 43% 25% 15% 17%
[libx264 @ 0x5963dc0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 34% 17% 37% 2% 2% 2% 2% 3% 2%
[libx264 @ 0x5963dc0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 19% 14% 6% 7% 7% 6% 6% 5%
[libx264 @ 0x5963dc0] i8c dc,h,v,p: 60% 19% 18% 3%
[libx264 @ 0x5963dc0] Weighted P-Frames: Y:0.9% UV:0.9%
[libx264 @ 0x5963dc0] ref P L0: 69.4% 7.6% 17.5% 5.5% 0.0%
[libx264 @ 0x5963dc0] ref B L0: 85.1% 12.9% 2.1%
[libx264 @ 0x5963dc0] ref B L1: 99.0% 1.0%
[libx264 @ 0x5963dc0] kb/s:1870.80
[aac @ 0x58bb580] Qavg: 174.733
【问题讨论】:
有哪位ffmpeg大侠能帮帮我吗? 当过渡的持续时间和偏移发生在您的第一个视频结束之后,就会发生急剧过渡。您是否尝试过使用偏移量,更低? 【参考方案1】:通过分离视频和音频轨道来修复。
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fadeblack:duration=1.000:offset=8.960,format=yuv420p[video];[0:a][1:a]acrossfade=d=1.000:c1=tri:c2=tri[audio]" -map [video] -map [audio] -movflags +faststart output.mp4
【讨论】:
【参考方案2】:我偶然发现了一个类似问题的open bug report,其中分离视频和音频并不能解决问题。
在这里,唯一可靠的解决方法是在另一个 ffmpeg 命令中将音频与视频完全分开处理,然后通过流复制将其合并,例如:
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fadeblack:duration=1.000:offset=8.960,format=yuv420p[video]" -an video.mp4
ffmpeg -i t1.mp4 -i t2.mp4 -filter_complex "[0:a][1:a]acrossfade=d=1.000:c1=tri:c2=tri[audio]" -vn audio.m4a
ffmpeg -i video.mp4 -i audio.m4a -c copy -movflags +faststart output.mp4
【讨论】:
或者再次输入输入并将该输入用作音频过滤器输入。以上是关于ffmpeg xfade 不能很好地与 crossfade 配合使用的主要内容,如果未能解决你的问题,请参考以下文章
当 sleep() 不能很好地与警报一起工作时,我还能做啥“睡眠”?
python 子进程不能很好地与 gsutil 复制/移动命令一起使用