ffmpeg 用文本视频创建空白屏幕 [英] ffmpeg create blank screen with text video

查看:90
本文介绍了ffmpeg 用文本视频创建空白屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个包含一些文字的视频.视频只有 0.5 秒长.背景应该只是某种颜色,我可以从照片中创建这样的视频,但是找不到任何地方可以在没有照片的情况下完成此操作,只能使用文本来创建这样的视频.

你能帮我吗?提前致谢

解决方案

drawtext vs 字幕

这可以使用

使用

ffmpeg -f lavfi -i color=c=red:s=320x240:d=10 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:box=1:boxborderw=5:boxcolor=white@0.25:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack Overflow'"输出.mp4

多行


使用多个 drawtext 实例(左图)将两条线居中更容易.右图来自外部文件和换行示例.

有 3 种方法.您可以将两个 drawtext 过滤器链接在一起,或者使用 textfile 选项引用外部文本文件,或者在命令中添加换行符.

或者,请参阅下面使用字幕过滤器的示例.此过滤器将自动换行长文本.

方法一:多个drawtext实例

ffmpeg -f lavfi -i color=c=green:s=320x240:d=10 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h-text_h)/2:text='Stack',drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h+text_h)/2:text='溢出''输出.mp4

方法二:外部文本文件

文本文件text.txt的内容如下所示:

堆栈溢出

ffmpeg 命令:

ffmpeg -f lavfi -i color=c=green:s=320x240:d=0.5 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:textfile=text.txt"输出.mp4

方法三:命令换行

ffmpeg -f lavfi -i color=c=green:s=320x240:d=0.5 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack溢出''输出.mp4

职位

  • 左上角:x=0:y=0(10 像素填充 x=10:y=10)
  • 顶部中心:x=(w-text_w)/2:y=0(带 10 px 填充 x=(w-text_w)/2:y=10>)
  • 右上角:x=w-tw:y=0(10 像素填充:x=w-tw-10:y=10)
  • 居中:x=(w-text_w)/2:y=(h-text_h)/2
  • 左下角:x=0:y=h-th(填充 10 像素:x=10:y=h-th-10)
  • 底部中心:x=(w-text_w)/2:y=h-th(10 像素填充:x=(w-text_w)/2:y=h-th-10)
  • 右下角:x=w-tw:y=h-th(填充 10 像素:x=w-tw-10:y=h-th-10代码>)

带音频的字幕过滤器示例

使用 color 过滤器作为背景和 字幕 文本过滤器.

首先,创建 SRT (SubRip) 文件.本示例将持续时间设置为 10 小时.持续时间并不重要:它只需要比音频文件的持续时间长.

100:00:00,000 -->10:00:00,000堆栈溢出

然后运行ffmpeg:

ffmpeg -f lavfi -i color=size=256x144:rate=15:color=blue -i "Audio File.m4a";-vf "subtitles=subtitles.srt:force_style='Alignment=10,Fontsize=30'";-c:a copy -movflags +faststart -shortest output.mp4

  • 或者,使用 Aegisub 创建 ASS(高级 SubStation Alpha)字幕,您将不必使用 force_style.缺点是 ASS 的格式比 SRT 复杂得多.

  • 如果您想使用 force_style,请参阅 ASS 文件中的 FormatStyle 行以了解如何使用使用更多 force_style 选项,并参阅 Aegisub 手册 - 屁股标签 用于高级格式.


输出图像而不是视频

如果您想要图像输出,请用 -frames:v 1 output.png 替换 output.mp4.

I would like to create a video consisting of some text. The video will only be 0.5 seconds long. The background should just be some colour, I am able to create such video from a photo, but cant find anywhere how this could be done without a photo, just using text to create such video.

Can you help me ? Thanks in advance

解决方案

drawtext vs subtitles

This can be done using the drawtext or subtitles filters.

drawtext is simpler, but subtitles has much better styling and timing options. subtitles can automatically wrap the text while drawtext cannot.


drawtext filter examples

Add text to solid color background

Using the color filter for the background and the drawtext filter for the text. Example for a 320x240, 10 second, 25 fps, blue background. Text is size 30, colored white, and centered.

ffmpeg -f lavfi -i color=size=320x240:duration=10:rate=25:color=blue -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack Overflow'" output.mp4

With audio file

Same as above, but video will automatically match audio duration by removing duration=10 and adding -shortest:

ffmpeg -f lavfi -i color=size=320x240:rate=25:color=blue -i audio.m4a -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack Overflow'" -c:a copy -shortest output.mp4

With a transparent box behind text

Another example with a white box behind the text at 25% opacity with 5 pixel padding:

ffmpeg -f lavfi -i color=c=red:s=320x240:d=10 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:box=1:boxborderw=5:boxcolor=white@0.25:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack Overflow'" output.mp4

Multiple lines


It's easier to center the two lines with multiple drawtext instances (left image). Right image is from the external file and line break examples.

There are 3 methods. You can chain together two drawtext filters, or reference an external text file with the textfile option, or add the line break in the command.

Alternatively, see the example below using the subtitles filter. This filter will automatically wrap long text.

Method 1: Multiple drawtext instances

ffmpeg -f lavfi -i color=c=green:s=320x240:d=10 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h-text_h)/2:text='Stack',drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h+text_h)/2:text='Overflow'" output.mp4

Method 2: External text file

The contents of the text file, text.txt, looks like this:

Stack
Overflow

The ffmpeg command:

ffmpeg -f lavfi -i color=c=green:s=320x240:d=0.5 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:textfile=text.txt" output.mp4

Method 3: Line break in command

ffmpeg -f lavfi -i color=c=green:s=320x240:d=0.5 -vf "drawtext=fontfile=/path/to/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Stack
Overflow'" output.mp4

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)

subtitles filter example with audio

Using the color filter for the background and the subtitles filter for the text.

First, create SRT (SubRip) file. This example sets the duration to 10 hours. The duration doesn't really matter: it just needs to be longer than the audio file duration.

1
00:00:00,000 --> 10:00:00,000
Stack Overflow

Then run ffmpeg:

ffmpeg -f lavfi -i color=size=256x144:rate=15:color=blue -i "Audio File.m4a" -vf "subtitles=subtitles.srt:force_style='Alignment=10,Fontsize=30'" -c:a copy -movflags +faststart -shortest output.mp4

  • Alternatively, use Aegisub to create ASS (Advanced SubStation Alpha) subtitles and you won't have to use force_style. Downside is that ASS is a much more complicated format than SRT.

  • If you want to use force_style refer to the Format and Style lines in an ASS file to get an idea of how to use more force_style options, and also see Aegisub Manual - Ass Tags for advanced formatting.


Output image instead of video

If you want an image output instead replace output.mp4 with -frames:v 1 output.png.

这篇关于ffmpeg 用文本视频创建空白屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆