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

Nicholas Robbins nickrobbins at yahoo.com
Fri Oct 17 20:30:24 CEST 2014

> On Thursday, October 16, 2014 11:59 AM, Moritz Barsnick <barsnick at gmx.net> wrote:

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

Ok, it seems like this should be easier. If we had a filter that did the same thing as vf_perspective but with inverse options this would be easy. So instead telling it what points you want to send to the corners, you tell it where you want the corners sent. 

I ran the math and the formulas to map between the two different ways of specifying the projective transformation are a bit messy. For instance, if you want to send the corners to  x0:y0:x1:y1:x2:y2:x3:y3 the first option for the vf_perspective should be 

(W (x2 y0 - x0 y2) (x3 (y0 - y1) + x0 (y1 - y3) +
    x1 (-y0 + y3)) (x3 (y1 - y2) + x1 (y2 - y3) +
    x2 (-y1 + y3)))/(-x0 (x3^2 (y1 - y2) (-2 y1 y2 + y0 (y1 + y2)) -
      2 x2 x3 y1 (y1 - y2) (y0 - y3) + x2^2 y0 (y1 - y3)^2) +
  x2 x3 (y0 - y1)^2 (-x3 y2 + x2 y3) +
  x1 (x3^2 y1 (y0 - y2)^2 + 2 x0 x3 (y1 - y2) y2 (y0 - y3) -
      x0^2 y1 (y2 - y3)^2 - 2 x2 (y1 - y2) (y0 - y3) (x3 y0 + x0 y3) +
      x2^2 (y0 - y3) (y0 (y1 - 2 y3) + y1 y3)) +
  x0^2 (x2 y2 (y1 - y3)^2 - x3 (y1 - y2) (2 y1 y2 - y1 y3 - y2 y3)) +
    x1^2 (x0 y0 (y2 - y3)^2 - x3 (y0 - y2)^2 y3 -
      x2 (y0 - y3) (y0 (y2 - 2 y3) + y2 y3)))

not something you can put on the command line or even want to work out by hand. However, it would be totally doable to calculate in filter initialization. 

So is it possible to make a filter that is basically just a wrapper around another filter? This hypothetical filter (call it vf_keystone) would just call vf_perspective with crazy options. Clearly I could just copy the vf_perspective filter and then insert these changes, but than any future changes to vf_perspective wouldn't filter down to vf_keystone.

Is this possible?


More information about the ffmpeg-devel mailing list