I'm using ffmpeg combined with tee and mplayer to have a simple video livestream and recorder over SSH. Now, I'd love to embed the current (server) time in the stream. The format doesn't matter much, ideally would be YYYY-MM-DD HH:MM:SS.

I've found this how-to suggesting the following command:

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 \
-vf "drawtext=fontfile=/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: \
text='%{localtime\:%T}': fontcolor=white@0.8: x=7: y=700" -vcodec libx264 \
-preset veryfast -f mp4 -pix_fmt yuv420p -y output.mp4

But using the -vf portion of this results in the following error for me:

[NULL @ 0x55e4ef96b2a0] Unable to find a suitable output format for 'text=%{localtime:%T}:'
text=%{localtime:%T}:: Invalid argument

I've also tried this command (the third code block in the Q), just to test it, but it failed aswell:

[Parsed_drawtext_0 @ 0x55fb2862a940] Both text and text file provided. Please provide only one
[AVFilterGraph @ 0x55fb28629fc0] Error initializing filter 'drawtext' with args 'text=%{pts:hms:1553359336.166336638}'

Finally, I also found this question, and tried it, slightly modified like this (this is showing my full command, I've only adjusted the -vf options in the past two examples):

ffmpeg  -r 20 -s 640x480 -f video4linux2 -i /dev/video0 -vf "drawtext=fontsize=90:fontcolor=white: fontfile=/usr/share/fonts/TTF/DejaVuSans.ttf: text='%{localtime\:%X}'" -f avi -

This presents me with the following error:

[NULL @ 0x55ae61d051a0] Unable to find a suitable output format for 'fontfile=/usr/share/fonts/TTF/DejaVuSans.ttf:'
fontfile=/usr/share/fonts/TTF/DejaVuSans.ttf:: Invalid argument

Modifying what I've found here a bit to:

-vf drawtext="fontfile='/usr/share/fonts/cantarell/Cantarell-Light.otf':fontsize=14:fontcolor=white:shadowcolor=black:shadowx=2:shadowy=1:text='%H-%M-%S':x=8:y=8"

Presents me with:

[Parsed_drawtext_0 @ 0x55e36db2aa40] Stray % near 'H-%M-%S'

Note that using this comment with just text (e.g. text='test') works fine..
Adding 1-4 \s in front of the %s shows no change in error or effect.

My ffmpeg version: ffmpeg version 3.2.12-1~deb9u1 on a debian machine.
It has been compiled with the drawtext library and it is present in ffmpeg -filters.


Okay, I've found the solution in the FFmpeg filter documentation.

10.52.2 Text expansion

If expansion is set to strftime, the filter recognizes strftime() sequences in the provided text and expands them accordingly. Check the documentation of strftime(). This feature is deprecated.

Though it says This feature is deprecated., this works just fine for me. Here is my final -vf:

-vf drawtext="expansion=strftime:fontfile='/usr/share/fonts/cantarell/Cantarell-Light.otf':fontsize=14:fontcolor=white:shadowcolor=black:shadowx=2:shadowy=1:text='%Y-%m-%d\ %H\\\\:%M\\\\:%S':x=8:y=8"

Please note that whyever, the four \\\\ are neccessary to escape :. To escape the space, a single \ is sufficient. The result will look like this:



The first option, text='%{localtime\\:T}' works fine. The OP had his quotes in the wrong place, he had 'text=...'. You'll note that in subsequent examples, he has the single quotes in the right place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.