How to draw text on image? (FFMPEG drawtext command)

  • I want to read a local image file, draw text on it as an overlay, and write it back locally using ffmpeg in a shell command (overlay timestamp sourced from filename)

  • I’m trying to use the ffmpeg drawtext filter

  • My image is in www and I can read/write it (copy/move/rename etc)

I’ve tried:

  • Lots of keyword searches for relevant home assiantant threads.

  • a squillion combinations of syntax variants I’ve found online

Apparently accessing the font file is the most common point of failure. ffmpeg is working fine for a bunch of other commands.
Setup is hassio on raspberry pie.

Threads I’ve looked at:

Here’s the kind of thing I’ve tried. I swear I’ve tried a gazillion variants of this over a squillion hours. Especially paths, escaping, and ", ': fiddling.

shell_command:
  drawtext: 'ffmpeg -i /config/www/motioneye_files/GiraffeCam1/all_timelapse/lastestGiraffe.jpg -vf drawtext=fontfile="arial.ttf":text=Last60min -y /config/www/motioneye_files/GiraffeCam1/all_timelapse/drawtext_lastestGiraffe.jpg'
  drawtext2: 'ffmpeg -i /config/www/motioneye_files/GiraffeCam1/all_timelapse/lastestGiraffe.jpg -vf drawtext=fontfile=arial.ttf:text=Last60min -y /config/www/motioneye_files/GiraffeCam1/all_timelapse/drawtext_lastestGiraffe.jpg'
  drawtext3: 'ffmpeg -i /config/www/motioneye_files/GiraffeCam1/all_timelapse/lastestGiraffe.jpg -vf drawtext=text=Last60min -y /config/www/motioneye_files/GiraffeCam1/all_timelapse/drawtext_lastestGiraffe.jpg'

If anyone could confirm they have a ffmpeg drawtext filter working and how, that would be really great!!!

Updating this page for anyone else struggling…after searching google and finding similar posts.

I think your issue was just in not assiging the fontfile to the correct directory.

I ran into a similiar issue after using ffmpeg to crop a security camera image, I needed to add in a timestamp since the cropped image didn’t include the original camera timestamp.

This was my crop shell command:

crop_image: ffmpeg -y -i ‘/config/www/images/snapshots/image_snapshot1.jpg’ -vf “crop=576:324:2733:175” ‘/config/www/images/snapshots/image_cropped.jpg’

After testing the drawtext option with basic commands, I ran into the same errors. Using developer tools to run the action, I could see the result. Luckily the developer tools response DID show that HA is running the required : --enable-libfreetype and --enable-libharfbuzz and the optional: --enable-libfontconfig and --enable-libfribidi so I didn’t need to make any changes to the default config (although based on research, this may have been added later and not available years ago.

However, without setting any default font or font file, the action response showed the following:

[Parsed_drawtext_0 @ 0x7f8afe03c0] Cannot find a valid font for the family
  Sans

I first tested assinging the FONT option to try different ‘standard’ font names, but ran into similiar errors.
After some research, I slowly discovered the solution. Create a custom fonts folder in the WWW directory. Then assign the fontfile to that ttf. (You can download all sorts of TTF from the google fonts respository.)

So on my 4th version, I finally got the following to work without errors:.

SOLUTION:

text_test_four: ffmpeg -y -i ‘/config/www/images/snapshots/image_cropped.jpg’ -vf drawtext=fontfile=‘/config/www/fonts/opensans-bold.ttf’:text=‘text’:x=5:y=5:fontsize=24:fontcolor=white ‘/config/www/images/snapshots/image_text.jpg’

(I have changed the names of my custom image files)

I’m wondering if you can combine the crop and drawtext command into a single shell command? I haven’t tested that yet. And now I can just start playing with the text to add in a timestamp.

1 Like