Can ffmpeg burn in time code?

后端 未结 7 1640
天涯浪人
天涯浪人 2020-12-07 21:16

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?

7条回答
  •  感情败类
    2020-12-07 22:09

    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.
    • You cannot specify filters multiple times, e.g. -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.

提交回复
热议问题