视频 ffmpeg 上的文字 [英] Text on video ffmpeg
问题描述
如何在 ffmpeg 中的视频上添加文字叠加?
即给定视频video1.flv",如何在整个视频中添加StackOverflow"文本,位于屏幕中间,带有白色文本和边框?
使用
将Stack Overflow
以白色文本打印到视频中心,黑色背景框不透明度为 50%:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2"-codec:a copy output.mp4
- 本示例中的音频是流式复制的(类似于复制和粘贴).
@0.5
控制背景框不透明度.0.5 是 50%.如果您不想要任何透明度,请删除@0.5
.- 请参阅 drawtext 过滤器文档,了解完整的选项列表和说明.莉>
预览
您可以使用 ffplay
预览您的文本,而无需等待文件编码:
ffplay -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2"输入.mp4
或者,您可以使用 mpv
但语法略有不同:
mpv --vf="lavfi=[drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2]"输入.mp4
多个文本
您可以链接多个 drawtext 过滤器:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2,drawtext=fontfile=/path/to/font.ttf:text='右下文字':fontcolor=black:fontsize=14:x=w-tw-10:y=h-th-10"-codec:a copy output.mp4
职位
x
和 y
确定文本位置:
位置 | x :y | 使用 10 px 填充 |
---|---|---|
左上 | x=0:y=0 | x=10:y=10 |
顶部中心 | x=(w-text_w)/2:y=0 | x=(w-text_w)/2:y=10 |
右上角 | x=w-tw:y=0 | x=w-tw-10:y=10 |
居中 | x=(w-text_w)/2:y=(h-text_h)/2 | |
左下 | x=0:y=h-th | x=10:y=h-th-10 |
底部中心 | x=(w-text_w)/2:y=h-th | x=(w-text_w)/2:y=h-th-10 |
右下 | x=w-tw:y=h-th | x=w-tw-10:y=h-th-10 |
随机 | 见这个答案 |
按需重新定位文本
- sendcmd 如果您有预定的位置和时间.请参阅 ffmpeg 中的 Sendcmd 和 FFmpeg drawtext 过滤器- 是否可以将变量与 x、y 坐标的实时数据一起使用?
- zmq 用于实时、即时定位.请参阅需要用户输入的 ffmpeg cli 过滤器.
移动/动画/循环/滚动文本
见:
时间
使用 enable
选项 来控制何时文本出现.
显示 5-10 秒之间的文本:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='between(t,5,10)'";-codec:a copy output.mp4
3 秒后显示文本:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='gte(t,3)'";-codec:a copy output.mp4
闪烁的文字.每 10 秒显示 5 秒文本:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='lt(mod(t,10),5)'"-codec:a copy output.mp4
每 30 秒随机位置:
更改/更新文本
为 drawtext 添加 textfile
和 reload
选项:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:textfile=text.txt:reload=1:fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2"-codec:a copy output.mp4
- 每次您想要更改文本时更新
text.txt
. - 重要提示:您必须以原子方式更新文本文件,否则可能会失败.您可以在 Linux 或 macOS 上使用
mv
命令执行此操作. - 如果您有很多文本更改,例如制作字幕,则制作字幕文件(例如通过 Aegisub 制作
.ass
文件)并使用 字幕过滤器.
字体系列代替字体文件
您可以声明字体系列,例如 Times New Roman,而不必指向字体文件.请参阅如何在不使用 fontfile 选项的情况下在 FFMPEG 命令中包含字体?
要求
drawtext 过滤器需要使用 --enable-libfreetype
编译 ffmpeg
.如果你得到 No such filter: 'drawtext'
它缺少 --enable-libfreetype
.大多数可用的 ffmpeg
静态构建都支持这一点:请参阅 FFmpeg 下载 页面了解链接.
How can I add text overlay on my video in ffmpeg?
i.e. given a video "video1.flv", how can I add "StackOverflow" text during the whole video, positioned in the middle of the screen, with white text and a border?
Use the drawtext filter for simple text on video. If you need more complex timing, formatting, or dynamic text see the subtitles filter. This answer focuses on the drawtext filter.
Example
Print Stack Overflow
in white text onto center of video, with black background box of 50% opacity:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4
- The audio is stream copied in this example (like a copy and paste).
@0.5
controls background box opacity. 0.5 is 50%. Remove@0.5
if you do not want any transparency.- See the drawtext filter documentation for a complete list and explanations of options.
Preview
You can use ffplay
to preview your text without having to wait for a file to encode:
ffplay -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" input.mp4
Alternatively you can use mpv
but the syntax is slightly different:
mpv --vf="lavfi=[drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2]" input.mp4
Multiple texts
You can chain multiple drawtext filters:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2,drawtext=fontfile=/path/to/font.ttf:text='Bottom right text':fontcolor=black:fontsize=14:x=w-tw-10:y=h-th-10" -codec:a copy output.mp4
Position
x
and y
determine text position:
Position | x :y |
With 10 px padding |
---|---|---|
Top left | x=0:y=0 |
x=10:y=10 |
Top center | x=(w-text_w)/2:y=0 |
x=(w-text_w)/2:y=10 |
Top right | x=w-tw:y=0 |
x=w-tw-10:y=10 |
Centered | x=(w-text_w)/2:y=(h-text_h)/2 |
|
Bottom left | x=0:y=h-th |
x=10:y=h-th-10 |
Bottom center | x=(w-text_w)/2:y=h-th |
x=(w-text_w)/2:y=h-th-10 |
Bottom right | x=w-tw:y=h-th |
x=w-tw-10:y=h-th-10 |
Random | See this answer |
Repositioning text on demand
You can reposition the text with the sendcmd and zmq filters:
- sendcmd if you have predetermined positions and timing. See Sendcmd in ffmpeg and FFmpeg drawtext filter - is it possible to use variables with live data for x,y coordinates?
- zmq for live, on-the-fly positioning. See ffmpeg cli filter that require user input.
Moving / animated / looping / scrolling text
See:
- ffmpeg moving text drawtext
- Loop text that wipes left to right using FFMPEG drawtext filter
- Scrolling from RIGHT to LEFT in ffmpeg / drawtext
Timing
Use the enable
option to control when the text appears.
Show text between 5-10 seconds:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='between(t,5,10)'" -codec:a copy output.mp4
Show text after 3 seconds:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='gte(t,3)'" -codec:a copy output.mp4
Blinking text. For every 10 seconds show text for 5 seconds:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:enable='lt(mod(t,10),5)'" -codec:a copy output.mp4
Random position every 30 seconds:
See ffmpeg - Dynamic letters and random position watermark to video?
Changing / updating text
Add the textfile
and reload
options for drawtext:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:textfile=text.txt:reload=1:fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4
- Update
text.txt
every time you want the text to change. - Important: You must update the text file atomically or it may fail. You can do this with the
mv
command on Linux or macOS. - If you have many text changes, such as making subtitles, it is easier to make a subtitle file (such as an
.ass
file via Aegisub) and using the subtitles filter.
Font family instead of font file
You can declare the font family, such as Times New Roman, instead of having to point to a font file. See How to include font in FFMPEG command without using the fontfile option?
Requirements
The drawtext filter requires ffmpeg
to be compiled with --enable-libfreetype
. If you get No such filter: 'drawtext'
it is missing --enable-libfreetype
. Most of the ffmpeg
static builds available support this: see the FFmpeg Download page for links.
这篇关于视频 ffmpeg 上的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!