Search code examples
videoffmpegoverlaytransparencyvideo-editing

(FFMPEG) Make areas transparent before overlaying a vid with perspective


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:


Solution

  • 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.