You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FFmpeg字幕滤镜未加载指定字体,默认 fallback 至 Helvetica的问题

FFmpeg字幕自定义字体失效问题

我尝试用FFmpeg为视频字幕设置自定义字体,但无论怎么操作,字体始终默认使用Helvetica。以下是我使用的代码:

import ffmpeg
import os
import tempfile

def test_subtitle_font(font_path):

    # 生成测试用的视频和音频流
    video = ffmpeg.input('color=color=black:size=640x360:duration=5', f='lavfi')
    audio = ffmpeg.input('anullsrc=channel_layout=stereo:duration=5', f='lavfi')

    # 创建临时字幕文件
    with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.srt') as temp_subtitle_file:
        temp_subtitle_file.write("1\n00:00:00,000 --> 00:00:05,000\nThis is a test subtitle.")
    
    subtitle_file = temp_subtitle_file.name

    # 强制设置字幕样式(指定字体)
    style = f"FontName=KOMIKAX_,FontFile={font_path},FontSize=24,Alignment=10"

    output_video_path = "subtitle_font_test.mp4"
    (
        ffmpeg
        .concat(
            video.filter('subtitles', filename=subtitle_file, force_style=style),
            audio,
            v=1, a=1
        )
        .output(output_video_path)
        .global_args('-report')
        .overwrite_output()
        .run()
    )

    # 删除临时字幕文件
    os.remove(subtitle_file)

# 字体文件路径
font_file_path = "./KOMIKAX_.ttf"

# 执行测试
test_subtitle_font(font_file_path)

尽管在force_style参数中指定了正确的字体路径,调试输出显示FFmpeg默认使用Helvetica:

[Parsed_subtitles_0 @ 0x11df1c860] fontselect: (KOMIKAX_, 400, 0) -> /System/Library/Fonts/Helvetica.ttc, -1, Helvetica 

我已经尝试过以下解决方法:

  • 指定FontFile为字体的完整路径
  • 确认字体文件存在且可读取
  • 在force_style中使用不同的参数设置(如在FontName中添加.ttf后缀、使用fontsdir等)

我确认该字体是有效的,因为运行同一目录下的测试代码时,字体可以正常显示:

def test_font(font_path, text):
 
    video = ffmpeg.input('color=color=black:size=640x360:duration=5', f='lavfi')
    audio = ffmpeg.input('anullsrc=channel_layout=stereo:duration=5', f='lavfi')
    
    video = video.drawtext(text=text, fontfile=font_path, fontsize=24, fontcolor='white', box=1, boxcolor='black@0.5', boxborderw=5, x='(w-text_w)/2', y='(h-text_h)/2')
    
    output_file = 'font_test_output.mp4'
    stream = ffmpeg.output(audio, video, output_file)
    ffmpeg.run(stream)


font_file_path = './KOMIKAX_.ttf'

# 执行字体测试
test_font(font_file_path, test_text)

内容的提问来源于stack exchange,提问作者Vatsal Joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:47:02