I have a need to burn in a time code to a video and am wondering if this is something that ffmpeg is capable of?
The drawtext filter mentioned in @stib's answer is the key to insert the time. Using the timecode
option, however, does not match the wall clock time. If you get the r
(timecode_rate) parameter wrong, then your the time will not match your playback time.
Other options exist, for instance the text='%{prt}'
option allows you to display the elapsed time with microsecond precision. Command:
ffmpeg -i video.mp4 -vf "drawtext=text='%{prt}'" output.mp4
To get a clock instead, I had to use the deprecated strftime
option. This has an undocumented basetime
option that can be used to set the start time in microseconds. An example where I set the start time to 12:00 PM on 1 December 2013 (the $(...)
part is shell-expansion done by the shell) and have only the time displayed (see the strftime manual for possible formats):
ffmpeg -i video.mp4 -vf "drawtext=expansion=strftime: \
basetime=$(date +%s -d'2013-12-01 12:00:00')000000: \
text='%H\\:%S\\:%S'" output.mp4
\\:
is used to escape the :
which would otherwise get the meaning of an option separator.
Another example: a command to insert the date + time within a black box, some pixels away from the top-left corner and "some padding" (actually, two spaces and newlines on the edges):
newline=$'\r'
ffmpeg -i video.mp4 -vf "drawtext=x=8:y=8:box=1:fontcolor=white:boxcolor=black: \
expansion=strftime:basetime=$(date +%s -d'2013-12-01 12:00:00')000000: \
text='$newline %Y-%m-%d %H\\:%M\\:%S $newline'" output.mp4
Another example to get the microseconds below the clock:
newline=$'\r'
ffmpeg -i video.mp4 -vf "drawtext=expansion=strftime: \
basetime=$(date +%s -d'2013-12-01 12:00:00')000000: \
text='$newline %H\\:%M\\:%S $newline':fontcolor=white:box=1:boxcolor=black, \
drawtext=text='$newline %{pts} $newline': \
y=2*th/3:box=1:fontcolor=white:boxcolor=black:" output.mp4
This uses the fact that the text is actually three lines long and that both text have a newline (carriage return, ^M
) prepended and appended. (Without this newline, the space gets stripped)
Other hints:
-vf
and -filter:v
are equal.-vf drawtext=text=1 -vf drawtext=text=2
will only draw the second text. You can combine filters with a comma as I showed above.