4 篇文章带有标签 “ffmpeg”

audio2sub — 音频转字幕工具

基于 OpenAI Whisper 的命令行工具,将音频文件批量转写为 VTT / SRT 格式字幕。

依赖 说明
Python ≥ 3.8
PyTorch Whisper 的运行时依赖,自动安装
openai-whisper 语音识别引擎
ffmpeg 音频解码,系统级安装
  • macOS:
brew install ffmpeg
  • Ubuntu / Debian:
sudo apt update && sudo apt install ffmpeg
pip install openai-whisper

该命令会自动拉取 torch 等依赖。首次运行时 Whisper 模型文件会下载到 ~/.cache/whisper/

使用系统 Python 或 miniconda 安装 whisper:

# miniconda(推荐,已预装 torch)
/opt/miniconda/bin/pip install openai-whisper

# 或系统 Python
/usr/bin/python3 -m pip install openai-whisper

编写文件:audio2sub.py

在 MacBook Pro M2 Max 上测试 Whisper

转录(X->X)

whisper test.wav --model small

翻译(X->English)

whisper test.wav --model small --task translate

使用 time 命令测试使用资源详情和度量

model user(s) system(s) cpu total(s) 内存
tiny 7.13 6.01 358% 3.664 370M
base 12.21 10.29 362% 6.211 430M
small 39.15 23.90 380% 16.569 1.2G
medium 117.27 68.43 377% 49.172 3.2G
large 184.13 114.85 361% 1:22.73 6.3G

tiny [00:00.000 --> 00:04.000] 荷蘭发布了一份主题为 [00:04.000 --> 00:09.500] 宣布即将对先进半道体知道设备采取的 [00:09.500 --> 00:13.000] 出口管制措施的公告表示 [00:13.000 --> 00:17.000] 坚于技术的发展和地缘政治的背景 [00:17.000 --> 00:20.000] 政府已经得出结论 [00:20.

命令ffmpeg

  • 生成gif(低质量) -pix_fmt(像素格式) -s(设置帧大小WxH)
ffmpeg -y -i input.mp4 -pix_fmt rgb8 -r 10 -s 320x240 output.gif
ffmpeg -y -i input.mp4 -pix_fmt rgb8 -r 10 -vf 'scale=320:-1' output.gif
  • 生成gif(高质量) -ss(开始时间偏移) -t(持续时间)
ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
ffmpeg -y -ss 5 -t 5 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
  • 每秒抽取一张图片 -r(设置帧速率)
ffmpeg -i input.mp4 -r 1 -s 1024x768 -f image2 input-%03d.jpeg