Search code examples
ffmpegdrawtext

FFmpeg drawtext filter - is it possible to use variables with live data for x,y coordinates?


I'd like to use variables for FFmpeg's drawtext filter's x,y coordinates so I can feed them with real-time data. The below solution with sendcmd works but I have to add relative timecodes at the beginnings so FFmpeg can link coordinates to time positions. Can it be done without timecodes with only the actual coordinates and tell FFmpeg that it should update these coordinates every 100ms?

It would look like this in my case:

cmd.txt

drawtext reinit 'x=960:y=540'; (coordinates change when there's a new position from the live source and FFmpeg updates these via sendcmd regularly).

Thanks!


Solution

  • Using sendcmd did the trick, this is how it can be used (the below example is using vaapi hw acceleration and a loopback device as input):

    ffmpeg -vaapi_device /dev/dri/renderD128 -f v4l2 -i /dev/video0 -vf "sendcmd=f=cmd.txt,drawtext=fontfile=abc.ttf:fontsize=60:fontcolor=white@0.8:bordercolor=black@0.1:borderw=1:r=25:text='O',format=nv12,hwupload" -c:v h264_vaapi out.mp4
    

    Format for cmd.txt:

    0   drawtext     reinit  'x=800:y=440';
    0.1 drawtext     reinit  'x=900:y=580';
    0.2 drawtext     reinit  'x=560:y=740';
    0.3 drawtext     reinit  'x=580:y=640';
    0.4 drawtext     reinit  'x=960:y=540';
    0.5 drawtext     reinit  'x=800:y=440';
    0.6 drawtext     reinit  'x=900:y=580';
    0.7 drawtext     reinit  'x=560:y=740';
    0.8 drawtext     reinit  'x=580:y=640';
    0.9 drawtext     reinit  'x=960:y=540';
    1   drawtext     reinit  'x=800:y=440';