FFMPEG: Overlaying scaled video over image

7,507

Use

ffmpeg -loop 1 -i background.jpg \
       -vf "movie=overlay.mp4,scale=128:96[inner];[in][inner]overlay=70:70:shortest=1[out]" \
       -y output.mp4

The image needs to be looped, but that will create an unending stream, so the shortest argument in the overlay filter stops the filter when the movie ends.


With the overlay's audio included

ffmpeg -loop 1 -i background.jpg -i overlay.mp4 \
       -filter_complex "[1]scale=128:96[inner];[0][inner]overlay=70:70:shortest=1[out]" \
       -map "[out]" -map 1:a -c:a copy -y output.mp4
Share:
7,507

Related videos on Youtube

cincplug
Author by

cincplug

Updated on September 18, 2022

Comments

  • cincplug
    cincplug almost 2 years

    I am trying to produce video containing static image and smaller video which would be overlaid on top of it. Dimensions and position of overlay video are dynamically sent to the server script which runs FFMPEG, so I need to scale video before overlaying it. So, numeric values I used are just for test. Original video has dimensions 72:48px. I tried command like this:

    ffmpeg -i background.jpg -vf "movie=overlay.mp4, scale=128:96 [inner]; [in][inner] overlay=70:70 [out]" -y output.mp4
    

    However, it just instantly produces an empty video. If, on the other hand, I try inserting -loop 1 right after ffmpeg command, the processing of video seems to take endless time and it never finishes.

    What I am trying to achieve is to avoid creating scaled video first. Is it possible and what am I doing wrong?

  • cincplug
    cincplug about 8 years
    Thanks, this works perfect except that there is no audio in output file. I need the output video to use audio stream from overlay.mp4. If I try to append -c:a copy after image path, or after video path as well, it throws an error. What is proper way?
  • cincplug
    cincplug about 8 years
    This should perhaps be another question, but since it inherits the code, I put it here first: how do I concatenate another video (using own audio stream) after output.mp4 which is produced as described above? I managed to do that separately like: ffmpeg -f concat -i concatlist.txt -c copy finaloutput.mp4, but I need to remain in scope of one ffmpeg directive, because it runs on remote server and second directive starts before the first finishes. Concatenated video has same size, codec, and frame rate as output.mp4. I struggle with concat filter all day with no good results :)