Merge two videos with transparency in ffmpeg

7

1

I have two videos merged into one file using below command. Unfortunately second video covers first video and it is not visible. How to make second video transparent (eg. 50%)?

ffmpeg
    -i in1.mp4 -i in2.mp4
    -filter_complex "nullsrc=size=480x360 [base];
        [0:v] setpts=PTS-STARTPTS, scale=480x360 [top];
        [1:v] setpts=PTS-STARTPTS, scale=480x360 [bottom];
        [base][top] overlay=shortest=1 [temp];
        [temp][bottom] overlay=shortest=1"
    -acodec libvo_aacenc -vcodec libx264 out.pm4

tiimowad

Posted 2016-01-13T14:07:23.390

Reputation: 125

Unrelated to your question, but libvo_aacenc is a crappy AAC encoder. Use -c:a aac instead. If ffmpeg provides an error mentioning that aac is experimental then upgrade your ffmpeg because aac is no longer experimental. – llogan – 2016-01-13T18:57:59.917

Do both inputs contain audio? If yes, how do you want to deal with the audio? With your command ffmpeg will choose audio from one input. You should always include the complete console output from your command when asking for ffmpeg help. – llogan – 2016-01-13T19:04:00.997

Answers

6

Use

ffmpeg \
    -i in1.mp4 -i in2.mp4 \
    -filter_complex " \
        [0:v]setpts=PTS-STARTPTS, scale=480x360[top]; \
        [1:v]setpts=PTS-STARTPTS, scale=480x360, \
             format=yuva420p,colorchannelmixer=aa=0.5[bottom]; \
        [top][bottom]overlay=shortest=1" \
    -acodec libvo_aacenc -vcodec libx264 out.mp4

Set aa to the opacity value needed.

Gyan

Posted 2016-01-13T14:07:23.390

Reputation: 21 016

This almost works for me except the main video (in1.mp4) stops showing video after in2.mp4 has finished, however the audio from in1.mp4 does continue for the duration of the video, just the video is "paused" – Titan – 2016-10-09T21:19:21.733

can you also explain the top and bottom my overlay sits left aligned and I'd like it centered, even if I have to specify the number of pixels it needs to move over – Titan – 2016-10-09T21:22:51.857

@Mulvya this works good, any way to repeat the shortest video? – Janis S. – 2017-11-29T21:57:25.190

If you know which input is shortest, its filterchain should be scale=480x360,loop=-1:99999:0,setpts=PTS-STARTPTS. where 99999 is a number equal or greater than its frame count. – Gyan – 2017-11-30T04:42:05.627