I would like to concatenate multiple videos with differents aspect ratio, framerates, etc. The working solution is scale2ref option with black background, no re-scale and concat.
If I use my command line with movie1.mp4 (30fps 1920x1080 2sec) and movie2.mp4 (25fps 800x600 3sec) I except a final output at 25fps 1920x1080 5sec. This part works well.
Then if I use my command line with movie1.mp4 (30fps 1920x1080 2sec) and movie2.mp4 (30fps 1920x1080 3sec) I except a final output at 30fps 1920x1080 5sec. But for an unknown reason my final output is 25fps 1920x1080 5 sec.
I tried with a copy of movie1.mp4 and rename it to movie2.mp4 to ensure they are both perfectly identical and got the same issue.
Input #0, lavfi, from 'color':
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from './1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:02.47, start: 0.000000, bitrate: 6737 kb/s
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 6567 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #1:1(und): Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from './2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:02.47, start: 0.000000, bitrate: 6737 kb/s
Stream #2:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 6567 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #2:1(und): Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
handler_name : SoundHandler
Output file :
[libx264 @ 000001a689d44180] using SAR=1/1
[libx264 @ 000001a689d44180] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 000001a689d44180] profile High, level 4.0, 4:2:0, 8-bit
[libx264 @ 000001a689d44180] 264 - core 160 - 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=24 lookahead_threads=4 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 './out.mp4':
Metadata:
encoder : Lavf58.29.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
Metadata:
encoder : Lavc58.54.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.54.100 aac
frame= 124 fps= 72 q=-1.0 Lsize= 3842kB time=00:00:04.84 bitrate=6502.6kbits/s speed=2.79x
Here is my command line :
ffmpeg -y -f lavfi -i color -i ./1.mp4 -i ./2.mp4 -filter_complex_script ./3.txt ./out.mp4
The content of filter comple script :
[0][1]scale2ref[canvas][vid1];[canvas][2]scale2ref='max(iw,main_w)':'max(ih,main_h)'[canvas][vid2];[canvas]split=2[canvas1][canvas2];[canvas1][vid1]overlay=x='(W-w)/2':y='(H-h)/2':shortest=1[vid1];[canvas2][vid2]overlay=x='(W-w)/2':y='(H-h)/2':shortest=1[vid2];[vid1][vid2]concat=n=2:v=1,setsar=1
I´m quite sure it´s related to the lavfi input but I don´t know how to enforce it to be the best framerate available.
Is it possible to enforce the "best" framerate available ? Something like :
My ffmpeg commands are programmaticaly generated. Any automatic solution is prefered.
overlay filter will use the frame rate of the first input. So set color filter frame rate (for example color=r=30
) to whatever rate you need it to be.
There is no option to automatically select the "best" frame rate. It just does what you tell it to do.
You can automate it by using ffprobe
to get the frame rate of each input and use your script to decide what to do. Note that VFR inputs may complicate this approach and the easiest method is to just force a consistent frame rate. ffmpeg
will then drop or duplicate frames to accommodate your desired frame rate.