python - ffmpeg 恢复为默认字幕字体

标签 python ffmpeg moviepy

我正在使用 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 消息)。
  • 关于python - ffmpeg 恢复为默认字幕字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69699369/

    相关文章:

    ffmpeg - 如何在 ffmpeg 中使用 QTRLE 编解码器创建视频?

    python - 剪掉一部分视频 - python

    python - 写入文件时 ffmpeg osx 错误 : "Unrecognized option ' preset'. 分割参数列表时出错:未找到选项

    python - 从 Python 将 wav 转换为 wav ulaw

    python - Moviepy 滑入和滑出过渡

    python - 迭代字典

    python - python 中是否可以执行 "from module.foo import * as bar"?

    python - Apache Beam - 如何在 Python 中通过键将 PCollection 分成 2 个

    python - Python 中的循环问题

    ffmpeg - ffmpeg 文件输出格式中的特殊字符是什么意思?