ffmpeg drawtext 文本位置及时间戳

我们在树莓派推流的时候,有时候需要加上时间戳,那么drawtext滤镜可以实现这个效果。

ffmpeg drawtext Positions

Top left
x=0:y=0 (with 10 pixel padding x=10:y=10)
Top center
x=(w-text_w)/2:y=0 (with 10 px padding x=(w-text_w)/2:y=10)
Top right
x=w-tw:y=0 (with 10 px padding: x=w-tw-10:y=10)
Centered
x=(w-text_w)/2:y=(h-text_h)/2
Bottom left
x=0:y=h-th (with 10 px padding: x=10:y=h-th-10)
Bottom center
x=(w-text_w)/2:y=h-th (with 10 px padding: x=(w-text_w)/2:y=h-th-10)
Bottom right
x=w-tw:y=h-th (with 10 px padding: x=w-tw-10:y=h-th-10)

完整Centered文本案例:

ffmpeg -i input.mp4 -vf "drawtext=text='Super User':x=(w-text_w)/2:y=(h-text_h)/2:fontsize=24:fontcolor=white" -c:a copy output.mp4

参数含义:

line_h, lh
the height of each text line

main_h, h, H
the input height

main_w, w, W
the input width

max_glyph_a, ascent
the maximum distance from the baseline to the highest/upper grid coordinate used to place a glyph outline point, for all the rendered glyphs. It is a positive value, due to the grid’s orientation with the Y axis upwards.

max_glyph_d, descent
the maximum distance from the baseline to the lowest grid coordinate used to place a glyph outline point, for all the rendered glyphs. This is a negative value, due to the grid’s orientation, with the Y axis upwards.

max_glyph_h
maximum glyph height, that is the maximum height for all the glyphs contained in the rendered text, it is equivalent to ascent - descent.

max_glyph_w
maximum glyph width, that is the maximum width for all the glyphs contained in the rendered text

n
the number of input frame, starting from 0

rand(min, max)
return a random number included between min and max

sar
The input sample aspect ratio.

t
timestamp expressed in seconds, NAN if the input timestamp is unknown

text_h, th
the height of the rendered text

text_w, tw
the width of the rendered text

添加时间戳的几种方式:

duration
ffmpeg -i input -vf "drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf: text='%{pts\:hms}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" image%03d.png
current time
ffmpeg -i input -vf "drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf: text='%{localtime}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" image%03d.png
timecode
ffmpeg -i input -vf "drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf: timecode='00\:00\:00\:00': r=25: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" image%03d.png

更多一些高级用法可以参考
《ffmpeg basics》中文版 – 10.在视频上添加文本

参考文献:

  1. FFmpeg Filters Documentation
  2. ffmpeg - How to position drawtext text - Super User
  3. Can FFMPEG extract images from a video with timestamps? - Super User
  4. BurntinTimecode - FilteringGuide – FFmpeg
  5. FFmpeg wiki
  6. 《ffmpeg basics》中文版 – 10.在视频上添加文本

你可能感兴趣的:(#,树莓派,视频,ffmpeg,drawtext,时间戳)