ffmpeg draw a line from point to point

Solution 1:

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.