Windows 上使用 ffmpeg 正确生成中文字幕的解决方案。
在 Windows 上使用 ffmpeg 的 drawtext、 subtitles、 ass 滤镜处理中文字幕时,会遇到以下问题:
错误码原因返回码 -22 (EINVAL)字体路径转义问题字幕不显示中文编码被截断乱码字符集不匹配
核心思路:用 Pillow 在图片上绘制字幕,ffmpeg 只负责图片转视频。
方案状态原因drawtext=text='中文'❌ 失败命令行参数截断subtitles='中文.srt'❌ 失败路径编码问题ass='中文.ass'❌ 失败同上Pillow 绘制 + ffmpeg✅ 成功Python 原生支持 Unicode
python
from ffmpeg_subtitle import add_subtitle_to_image
# 在图片上添加字幕
add_subtitle_to_image(
image_path="input.png",
subtitle_text="这是中文字幕",
output_path="output.png",
font_size=24,
y_offset=50
)
参数默认值说明image_path-输入图片路径subtitle_text-字幕文本output_path-输出图片路径font_size24字体大小y_offset50距底部的偏移量(像素)font_color(255,255,255)字体颜色 RGBshadow_color(0,0,0)阴影颜色 RGB
text
Pillow>=10.0.0
默认使用 Windows 系统字体:
C:/Windows/Fonts/msyh.ttc(微软雅黑)C:/Windows/Fonts/simhei.ttf(黑体)ffmpeg字幕、 中文字幕、 视频字幕、 字幕烧录
text
ffmpeg-chinese-subtitle/
├── SKILL.md # 技能说明文档
├── README.md # 详细使用指南
├── ffmpeg_subtitle.py # 核心模块
├── example.py # 完整示例
└── package.json # 包信息
MIT
systiger