Sunday, December 14, 2014

How to overlay a png with fade in and fade out over a specific duration on the main video

First, let's create the overlay **like a movie** in this way:
-loop 1 -i watermark.png

Then use a fade filter to fade in for 4 seconds (that is 100 frames):
fade=in:0:100



Let us then delay it by 10 seconds to start later in this way:
setpts=PTS-STARTPTS+10/TB
Your command then becomes:
ffmpeg -i video.mp4 -loop 1 -i watermark.png -filter_complex "[1:v]fade=in:0:100[v1]; [0:v]setpts=PTS-STARTPTS[v0];[v1]setpts=PTS-STARTPTS+10/TB[v3];[v0][v3]overlay=eof_action=pass[out1]" -map [out1] <other parameters> overlaidoutput.mp4
The overlay will continue to the end. And of course you can use many fine-tuning parameters within the "other parameters" section.
If you need to end the overlay at a specific time point, you can also use the enable parameter:
ffmpeg -i video.mp4 -loop 1 -i watermark.png -filter_complex "[1:v]fade=in:0:100[v1]; [0:v]setpts=PTS-STARTPTS[v0];  [v1]setpts=PTS-STARTPTS+10/TB[v3];  [v0][v3]overlay=enable='between(t,5,12)':eof_action=pass[out1]"  -map [out1] <other parameters> overlaidoutput.mp4 
This ends the overlay at 12 seconds.

What if we need fade out as well? We'll need to fade out the alpha channel. But you will not need the enable parameter any more.

ffmpeg -i video.mp4 -loop 1 -i watermark.png -filter_complex "[1:v]fade=in:0:100:out=:175:25:alpha=1[v1]; [0:v]setpts=PTS-STARTPTS[v0];  [v1]setpts=PTS-STARTPTS+5/TB[v3];  [v0][v3]overlay=eof_action=pass[out1]"  -map [out1] <other parameters> overlaidoutput.mp4 
And finally, for my usual 2 pass mp4 encoding, using all these parameters the script becomes:
for file in *.mov
do
ffmpeg -i $file -y -pass 1 -s 1280x720 -an -vcodec libx264 -pix_fmt yuv420p -threads 4 -b 1024k -flags +loop -cmp chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 1 -trellis 0 -refs 1 -bf 0 -coder 0 -me_range 16 -g 25 -keyint_min 15 -sc_threshold 40 -i_qfactor 0.71 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -f mp4 /dev/null
ffmpeg -i $file -loop 1 -i wm_iphone.png -filter_complex "[1:v]fade=in:0:100, fade=out:175:25:alpha=1[v3]; [0:v]setpts=PTS-STARTPTS[v0];[v3]setpts=PTS-STARTPTS+5/TB[v1];[v0][v1]overlay=eof_action=pass[out1]" -map [out1] -y -s 1280x720 -strict -2 -acodec aac -ab 128k -pass 2 -vcodec libx264 -pix_fmt yuv420p -threads 4 -b 1024k -flags +loop -cmp chroma -partitions +parti4x4+partp8x8+partb8x8 -mixed-refs 1 -subq 6 -trellis 1 -refs 5 -bf 0 -coder 0 -me_range 16 -g 25 -keyint_min 15 -sc_threshold 40 -i_qfactor 0.71 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 30 -qdiff 4 -movflags +faststart ${file%%.mov}.mp4
done

No comments:

Post a Comment