An open API service indexing awesome lists of open source software.

https://github.com/sixiaolong1117/whisperpythonscript

一个简单的 Whisper Python 脚本,可以将媒体文件的音频通过 whisper 识别成文字,并通过 pysrt 保存为字幕。
https://github.com/sixiaolong1117/whisperpythonscript

pysrt python python3 whisper whisper-ai

Last synced: 21 days ago
JSON representation

一个简单的 Whisper Python 脚本,可以将媒体文件的音频通过 whisper 识别成文字,并通过 pysrt 保存为字幕。

Awesome Lists containing this project

README

          

# Whisper Python Script

一个简单的 Whisper Python 脚本,可以将媒体文件的音频通过 `whisper` 识别成文字,并通过 `pysrt` 保存为字幕。现在整理为统一入口,支持 macOS 和 Apple Silicon(M 系列,包括 M5)上的 `mps` 加速。

## 依赖安装

建议使用 Python 3.11 或 3.12。Apple Silicon 上不建议直接使用系统 Python。

```bash
brew install python@3.12 ffmpeg
```

创建虚拟环境并安装依赖:

```bash
git clone https://github.com/SIXiaolong1117/WhisperPythonScript.git
cd WhisperPythonScript
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

> 第一次运行会下载 Whisper 模型。`large` 模型体积和内存占用较大,如果内存紧张,可以先用 `--model medium` 或 `--model small`。

## 使用方法

### 推荐入口

生成 `.srt` 字幕:

```bash
python whisper_subtitles.py <媒体文件路径> --language zh-Hans
```

生成 `.srt` 并封装为 `.mkv`:

```bash
python whisper_subtitles.py <媒体文件路径> --language zh-Hans --embed
```

把已有同名 `.srt` 封装到 `.mkv`:

```bash
python whisper_subtitles.py <媒体文件路径> --subtitle <字幕文件路径>
```

指定模型、设备和输出路径:

```bash
python whisper_subtitles.py video.mp4 --language zh --model medium --device auto -o video.srt
```

`--device auto` 会在 Apple Silicon 上优先使用 `mps`。为了提高稳定性,脚本默认只在 CUDA 上启用 fp16;如果你确认当前 PyTorch/Whisper 组合在 MPS fp16 下表现正常,可以手动使用:

```bash
python whisper_subtitles.py video.mp4 --device mps --fp16 true
```

### 兼容旧入口

旧脚本仍然可用:

```bash
python v2s.py <媒体文件路径> <语言代码(可选)>
python v2mkv_s.py <媒体文件路径> <语言代码(可选)>
python s2mkv_s.py <媒体文件路径>
```

`v2s.py` 只生成字幕,`v2mkv_s.py` 会生成字幕并封装 MKV,`s2mkv_s.py` 会把同名 `.srt` 封装进 MKV。

## 开源许可

[MIT License](./LICENSE)