python - ffmpeg 恢复为默认字幕字体
问题描述
我正在使用ffmpeg-python将 SRT 刻录到视频文件中。我的代码看起来像这样:
caption_file = "captions.srt"
style = "FontName=Roboto-Regular,FontSize=8"
fonts_dir = "fonts-main/apache"
(
ffmpeg
.concat(video.filter("subtitles", caption_file, fontsdir=fonts_dir, force_style=style), audio, v=1, a=1)
.output("my_video_w_subs.mp4")
.run()
)
当我运行代码时,SRT 确实被烧毁了,但不是以指定的字体(Roboto-Regular)。
以下是输出日志:
[Parsed_subtitles_0 @ 0x55adfd490e80] Loading font file 'fonts-main/apache/Roboto-Regular.ttf'
[Parsed_subtitles_0 @ 0x55adfd490e80] Using font provider fontconfig
[Parsed_subtitles_0 @ 0x55adfd490e80] fontselect: (Roboto-Regular, 400, 0) -> /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf, 0, DejaVuSans
似乎找到并加载了所需的字体,所以我不确定为什么不使用它。
解决方案
看起来fonts_dir
应该包括字体文件名。
而不是fonts_dir = "fonts-main/apache"
,尝试:
fonts_dir = "fonts-main/apache/Roboto-Regular.ttf"
注意:fonts-main/apache/Roboto-Regular.ttf
使用相对路径。
您可以尝试使用完整路径,例如:/fonts-main/apache/Roboto-Regular.ttf
.
对于调试添加参数.global_args('-report')
,并检查日志文件。
这是一个完整的代码示例:
import ffmpeg
caption_file = "captions.srt"
style = "FontName=Roboto-Regular,FontSize=8"
fonts_dir = "/fonts-main/apache/Roboto-Regular.ttf"
input = ffmpeg.input('1.avi')
video = input.video
audio = input.audio
(
ffmpeg
.concat(video.filter("subtitles", filename=caption_file, fontsdir=fonts_dir, force_style=style), audio, v=1, a=1)
.output("my_video_w_subs.mp4")
.global_args('-report')
.overwrite_output()
.run()
)
使用fonts_dir = "/fonts-main/apache"
时出现错误ass_read_file(/fonts-main/apache/����): fopen failed
笔记:
- 我正在使用 Windows,但我不确定该
fonts_dir
值是否是真正的问题(我没有收到Loading font file
消息)。
推荐阅读
- excel - 如何在使用 VBA 匹配的范围内找到最后一行单元格?
- php - Symfony 错误:试图调用类的名为 […] 的未定义方法
- blazor - 如何从可绑定组件继承?
- rust - 虚假网络错误(剩余 2 次尝试):[7] 无法连接到服务器
- php - ajax成功方法回退类型不起作用
- wordpress - WordPress插件,显示注册页面,点击按钮
- c# - 使用 C# 中的 PowerShell 主机如何获取输出?
- flutter - 任务“:app:processDebugGoogleServices”执行失败。> 找不到与包名称“com.example.shopapp_tut”匹配的客户端
- android - 如何在颤动中动态地进行带有文本叠加(实时变化)的 Youtube 直播?
- git - 当它说它已经是最新的时,为什么我的 git 分支与主分支不匹配