'Crossfade Animation with multiple Images using Ffmpeg

I'm creating video with Multiple images using Ffmpeg and i'm using given link -

(Remember most important thing is All the images have different sizes)

https://superuser.com/a/834035/1243959

Now First of all i'm creating video with fade in/out animation and for that i'm using 3rd code with title For input images with varying or arbitrary sizes and it works well -

ffmpeg \
-loop 1 -t 5 -i input0.png \
-loop 1 -t 5 -i input1.png \
-loop 1 -t 5 -i input2.png \
-loop 1 -t 5 -i input3.png \
-loop 1 -t 5 -i input4.png \
-filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=out:st=4:d=1[v0]; \
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; \
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
 [3:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
 [4:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v4]; \
 [v0][v1][v2][v3][v4]concat=n=5:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4

but when i want to create Cross Fade animation using given command with the title MP4 output in Cross fade section,in output video,portrait images are fits into video,but square and landscape images are getting cropped (@llogan has specified that if images have different sizes than we have to use same command which is used for fade in/out) -

ffmpeg \
-loop 1 -t 5 -i 1.png \
-loop 1 -t 5 -i 2.png \
-loop 1 -t 5 -i 3.png \
-loop 1 -t 5 -i 4.png \
-loop 1 -t 5 -i 5.png \
-filter_complex \
"[1]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
 [3]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3]; \
 [bg3][f3]overlay,format=yuv420p[v]" -map "[v]" -movflags +faststart out.mp4

But I don't know exactly what to do and I think I have to first fit the images in a certain ratio like 1280 * 720, by which all the images will be of same size and then the animation should be applied to those images and may be different solution can work too but right now I'm confused about what to do because I've tried a lot of solutions like scaling images and some other attempts and not a single solution worked (maybe I didn't even try right)..

if SAR and DAR can help than they are as below (I don't know difference between these variables):

SAR 1:1 / DAR 197:295
SAR 1:1 / DAR 2:3
SAR 96:96 / DAR 16:9
SAR 1:1 DAR / 540:961
SAR 1:1 DAR / 2567:3113

Any help will be appreciated..Thank you!



Solution 1:[1]

variant 2, using fade+overlay:

#!/bin/bash
ffmpeg \
-loop 1 -t 5 -i 1.png \
-loop 1 -t 5 -i 2.png \
-loop 1 -t 5 -i 3.png \
-loop 1 -t 5 -i 4.png \
-loop 1 -t 5 -i 5.png \-filter_complex "
[0]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=d=1:t=in:alpha=1,fade=t=out:st=4:d=1,setpts=PTS-STARTPTS+4/TB[f0];
[1]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=d=1:t=in:alpha=1,fade=t=out:st=4:d=1,setpts=PTS-STARTPTS+8/TB[f1];
[2]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=d=1:t=in:alpha=1,fade=t=out:st=4:d=1,setpts=PTS-STARTPTS+12/TB[f2];
[3]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=d=1:t=in:alpha=1,fade=t=out:st=4:d=1,setpts=PTS-STARTPTS+16/TB[f3];
[4]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=d=1:t=in:alpha=1,fade=t=out:st=4:d=1,setpts=PTS-STARTPTS+20/TB[f4];
[f0][f1]overlay[bg1];
[bg1][f2]overlay[bg2];
[bg2][f3]overlay[bg3];
[bg3][f4]overlay,format=yuv420p[v]
" -map [v] -movflags +faststart -y out.mp4

Solution 2:[2]

xfade

#!/bin/bash
ffmpeg \
-loop 1 -t 5 -i 1.png \
-loop 1 -t 5 -i 2.png \
-loop 1 -t 5 -i 3.png \
-loop 1 -t 5 -i 4.png \
-filter_complex "
[0]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1[0p];
[1]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1[1p];
[2]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1[2p];
[3]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1[3p];
[0p][1p]xfade=duration=1:offset=4[1x];
[1x][2p]xfade=duration=1:offset=8[2x];
[2x][3p]xfade=duration=1:offset=12[3x]
" -map [3x] -c:v libx264 -crf 17 /tmp/output.mp4

generated by script:

#!/bin/bash
LST=($(ls -1 *.png))
TOT=${#LST[*]}
f="${LST[0]}"
INP=("-loop" "1" "-t" "5" "-i" "$f")
echo $f
FLS="[0]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1[0p]"
PDX="[0p]"
OFS=0
for (( i=1; i<=$(( $TOT -1 )); i++ )); do
  f="${LST[$i]}"
  INP+=("-loop" "1" "-t" "5" "-i" "$f")
  ((OFS += 4))

  PDS="[${i}p]"
  FLS+=";[${i}]scale=-2:720,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1${PDS}"
  FLX+=";${PDX}${PDS}xfade=duration=1:offset=${OFS}"
  PDX="[${i}x]"
  FLX+="${PDX}"

  echo $OFS $f
done

echo $FLS
echo ""
echo $FLX
echo ""
rm /tmp/output.mp4
ffmpeg "${INP[@]}" -filter_complex "$FLS $FLX" -map $PDX -c:v h264_nvenc -cq 20 -y /tmp/output.mp4 -hide_banner

ffplay -autoexit /tmp/output.mp4

[updated] code optimization

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ???? ???????????
Solution 2