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.在視頻上添加文本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章