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

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

问题描述

我想创建一个包含一些文本的视频.视频将只有0.5秒长.背景应该只是某种颜色,我可以用照片创建这样的视频,但是在任何地方都找不到没有照片的方法,只能使用文本来创建这样的视频.

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.

你能帮我吗?预先感谢

推荐答案

drawtext vs字幕

可以使用 drawtext drawtext 比较简单,但是 subtitles 具有更好的样式和计时选项. 字幕可以自动换行,而 drawtext 不能自动换行.

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

使用颜色过滤器作为背景,并使用

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

  • duration=10将输出10秒的持续时间.
  • 请参阅所支持的颜色名称列表以及如何使用十六进制代码设置颜色.
    • The duration=10 will make a 10 second duration output.
    • See the list of supported color names and how to use a hex code to set color.
    • 与上述相同,但是视频将通过删除duration=10并添加-shortest来自动匹配音频持续时间:

      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
      

      在文本后面带有透明框

      另一个示例是文本后面有一个白框,不透明度为25%,填充为5像素:

      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
      

      多行


      更容易将两条线与多个drawtext实例居中对齐(左图).正确的图像来自外部文件和换行符示例.

      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.

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

      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.

      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
      

      方法2:外部文本文件

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

      Stack
      Overflow
      

      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" output.mp4
      

      方法3:命令中的换行符

      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
      


      带音频的字幕过滤器示例

      使用颜色过滤器作为背景,并使用


      subtitles filter example with audio

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

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

      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
      

      然后运行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
      

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