1

I'm needing to draw a line from point to point in ffmpeg. I don't see a drawline filter in ffmpeg, so I'd assume drawbox would need to be used (see command below). How could I adapt this to draw a diagonal line from, say, 10,10 to 500,500?

I've used pythagorean theorem to calculate how 'wide' the line needs to be, but that is as far as I've gotten:

ffmpeg -i input.mp4 -vf drawbox=x=10:y=10:w=692:h=1:color=red output.mp4

Thank you

1 Answer 1

5

In this specific case, since the line is at 45%, we can use the method given below.

ffmpeg -i in.mp4 -filter_complex 
      "color=red:s=490x490,geq=lum='p(X,Y))':a='if(eq(X,Y),255,0)'[c];
       [0][c]overlay=10:10:shortest=1"
out.mp4

The GEQ filters allows one to manipulate individual pixels using expressions. If a line is at 45 deg, that means all points are on the line X = Y or X = -Y. The latter case is irrelevant here.

So, first a blank canvas is created. Its size is the coverage needed to draw the entire line (W = 500-10; H = 500-10). Then the GEQ sets all pixels with X = Y to opaque but all others to transparent. (The lum expression is needed due to a quirk of the filter design; all it does is retain the existing value of the three planes - luma & two chroma).

Then this output is overlaid with an offset of (10,10). The shortest is needed because the color/geq input never terminates.


For the general case of a line at an arbitrary degree, you would draw a straight line i.e. keep alpha of 255 for a single row i.e. 'if(eq(Y,100),255,0)', then use the rotate filter to get it to the correct angle. (The rotate padding should be fillcolor=anycolor@0). Then overlay that.

1
  • This works but seems to cause ffmpeg to burn a lot of CPU. Is there a more efficient way? I assume this is so slow because the geq expression is being evaluated for every pixel for every frame. That seems particularly inefficient, especially in this case when there's no time element. drawtext or drawbox take up almost no CPU in comparison. Any ideas?
    – Chris
    Sep 7, 2020 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .