How to overlay two videos with blend filter in ffmpeg

Carlos Angulo picture Carlos Angulo · Apr 16, 2016 · Viewed 9.5k times · Source

I need to do a lot of videos with the next specifications:

  • A background video (bg.mp4)
  • Overlay a sequence of png images img1.png to img300.png (img%d.png) with a rate of 30 fps
  • Overlay a video with dust effects using a blend-lighten filter (dust.mp4)
  • Scale all the inputs to 1600x900 and if the input have not the aspect ratio, then crop them.
  • Specify the duration of the output-video to 10 sec (is the duration of image sequence at 30fps).

I've being doing a lot of test with different commands but always shows error.

Answer

Carlos Angulo picture Carlos Angulo · Apr 17, 2016

Well, I think I got it in the next command:

ffmpeg -ss 00:00:18.300 -i music.mp3 -loop 1 -i bg.mp4 -i ac%d.png -i dust.mp4 -filter_complex "[1:0]scale=1600:ih*1200/iw, crop=1600:900[a];[a][2:0] overlay=0:0[b]; [3:0] scale=1600:ih*1600/iw, crop=1600:900,setsar=1[c]; [b][c] blend=all_mode='overlay':all_opacity=0.2" -shortest -y output.mp4

I'm going to explain in order to share what I've found:

  • Declaring the inputs:

ffmpeg -ss 00:00:18.300 -i music.mp3 -loop 1 -i bg.mp4 -i ac%d.png -i dust.mp4

  • Adding the filter complex. First part: [1,0] is the second element of the inputs (bg.mp4) and scaling to get the max values, and then cropping with the size I need, the result of this opperation, is in the [a] element.

[1:0]scale=1600:ih*1600/iw, crop=1600:900, setsar=1[a];

  • Second Part: Put the PNGs sequence over the resized video (bg.mp4, now [a]) and saving the resunt in the [b] element.

[a][2:0] overlay=0:0[b];

  • Scaling and cropping the fourth input (overlay.mp4) and saving in the [c] element.

[3:0]scale=1600:ih*1600/iw, crop=1600:900,setsar=1[c];

  • Mixing the first result with the overlay video with an "overlay" blending mode, and with an opacity of 0.1 because the video has gray tones and makes the result so dark.

[b][c] blend=all_mode='overlay':all_opacity=0.1

That's all. If anyone can explay how this scaling filter works, I would thank a lot!