简体   繁体   中英

FFmpeg complex filter and -vf to pad and scale images to video

I am trying to overlay a video with multiple images at different points in time. The issue here is that the images might be bigger than the video, but they should be scaled down (keeping the aspect ratio) and the missing spaces should be filled with black.

Here's what I have so far:

-i 'input_video.mp4' 
-i 'input_image_1.jpg' 
-i 'input_image_2.jpg' 
-i 'input_image3.jpg' 
-i 'input_image4.jpg' 
-filter_complex "[0][1] overlay=0:0:enable='between(t,0.0,2.0)'[s1];
[s1][2] overlay=0:0:enable='between(t,7.0,9.0)'[s2];
[s2][3] overlay=0:0:enable='between(t,18.0,20.0)'[s3];
[s3][4] overlay=0:0:enable='between(t,20.0,22.0)'[s4];" 
-vf 'scale=min(iw*540/ih,960):min(540,ih*960/iw), 
     pad=960:540:(960-iw)/2:(540-ih)/2' 
-pix_fmt yuv420p -c:a copy
'output_image.mp4'

The values in scale and pad are derived dynamically and correspond to the video height and video width.

However with this commando FFmpeg complains that -filter_complex and -vf can not be used in the same commando. I would like to refrain from first scaling down the images or creating video files from them and then overlay these videos.

Any pointers are appreciated.

Use

-i 'input_video.mp4' 
-i 'input_image_1.jpg' 
-i 'input_image_2.jpg' 
-i 'input_image3.jpg' 
-i 'input_image4.jpg' 
-filter_complex
"[1]scale=min(iw*540/ih,960):min(540,ih*960/iw),
    pad=960:540:(960-iw)/2:(540-ih)/2[img1];
 [2]scale=min(iw*540/ih,960):min(540,ih*960/iw),
    pad=960:540:(960-iw)/2:(540-ih)/2[img2];
 [3]scale=min(iw*540/ih,960):min(540,ih*960/iw),
    pad=960:540:(960-iw)/2:(540-ih)/2[img3];
 [4]scale=min(iw*540/ih,960):min(540,ih*960/iw),
    pad=960:540:(960-iw)/2:(540-ih)/2[img4];
[0][img1] overlay=0:0:enable='between(t,0.0,2.0)'[s1];
[s1][img2] overlay=0:0:enable='between(t,7.0,9.0)'[s2];
[s2][img3] overlay=0:0:enable='between(t,18.0,20.0)'[s3];
[s3][img4] overlay=0:0:enable='between(t,20.0,22.0)'[s4];" 
-c:a copy   'output_image.mp4'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM