[FFmpeg-user] How to create trapezoid videos with ffmpeg?

Moritz Barsnick barsnick at gmx.net
Thu Oct 16 17:59:32 CEST 2014


Hi Zenny,

On Wed, Oct 15, 2014 at 10:55:40 +0200, Zenny wrote:
> New to ffmpeg. How can one create a trapezoid shaped video as a
> overlay to the background as seen at
> http://www.youtube.com/watch?v=_380o5B9MrA (starts at 00:02:25) using
> ffmpeg command?

I can't seem to find a filter which does that directly, but the
"perspective" does a similar transformation, in an inverse way. I
believe it "virtually pulls the given viewpoints to the corners." Using
it with values pointing at viewpoints outside of the frame gives the
approximate effect of what you're trying to achieve, see my example
below. Interestingly, that filter smears the edges colors of the input
across the rest of the output frame. (I'm not sure whether that's a bug
or a feature.)

Here's an example, using a test source with an overlayed outline and
grid lines, to may the effect more clearly visible. It uses the SDL
display driver as output to screen, but you can output to a file
alternatively:

ffmpeg -re -f lavfi -i "testsrc,drawbox=x=1:y=1:w=iw-2:h=ih-2:t=1:c=white,drawgrid=w=iw/10:h=ih/10:t=1:c=white at 0.5" -filter_complex "[0:v]perspective=x0=-100:y0=-100:x2=-100:y2=H+100[ovl1]; [0:v]pad=w=iw*2[ovl0]; [ovl0][ovl1]overlay=x=W/2[vidout]" -map "[vidout]" -pix_fmt yuv420p -f sdl -

I use the "-filter_complex" chain to combine the original and the
transformed output side-by-side. The relevant "perspective" filter
section is:

"perspective=x0=-100:y0=-100:x2=-100:y2=H+100"

Note the reference points _outside_ of the frame to achieve a trapezoid
size reduction. The filter you are looking for should probably accept
actual corner values. That said, such a filter could probably be
derived from the "perspective" filter's code. (I failed at finding an
easy way to do this though.)

Moritz


More information about the ffmpeg-user mailing list