(FFMPEG) Make areas transparent before overlaying

2019-07-28 18:45发布

问题:

I'm trying to add perspective to a small video then overlay it on top of an other.

So far I can make the video small, add perspective to it and overlay it to an other video. but after applying the perspective filter, the excess areas don't turn transparent, they just stretch out the pixels.

the perspective filter doesn't have much documentation but as far as i could find out there was no way of setting the extra pixels to transparent.

So I'm guessing ill need to apply some kind of alpha mask, to the desired area?

this is the command I'm using for perspective:

ffmpeg -i /synced_folder/testvid.mp4 -vf perspective=0:0:W:H/4:0:H:W:3*H/4:0:1:0 /synced_folder/output5.mp4

Don't think it matters but I'm running ffmpeg on a VM with centOS.

This is a SS of how the result looks like:

回答1:

This requires an unconventional approach. The perspective filter was meant to correct certain types of distortion during recording, and not for DVE-type overlays. The pixels at the edges will be extended to fill the canvas.

The trick here is to add a transparent padding to the video, and then add perspective. The pixels which get extended will be the transparent pixels, which become invisible upon overlay.

ffmpeg -i base.mp4 -i overlay.mp4
filter_complex" [1]pad=iw+4:ih+4:2:2:black@0,perspective=0:0:W:H/4:0:H:W:3*H/4:0:1:0[p];
                [0][p]overlay=-2:-2"    output.mp4

I have padded with a border of 2 pixels, chosen because the usual pixel format is 4:2:0. The border color is black, but with alpha of 0.

Strictly speaking, the perspective values should be corrected to account for the border, but it's a small change. The overlay is also offset to align the visible video to the intended destination.



回答2:

I got the desired effect by using a png with alpha 0 in the areas I wanted, then using the alphaextract and alphamerge commands.

It works but if someone knows how to get a transparent background straight from the perspective command, I would appreciate the help.

ffmpeg -y -i video.mp4 -loop 1 -i alphamask.png -filter_complex "[1:v]alphaextract[alf];[0:v][alf]alphamerge" -c:v qtrle -an /synced_folder/output.mov

(Note that if you play the output, it might look like it didn't work. To see the results you have to overlay it with something in the background)