mcpField
A
anibon-local-transcription

anibon-local-transcription

github.com/zenithth创意与媒体137 次安装1 天前更新
代码仓库

关于 anibon-local-transcription

用于处理没有字幕或自动字幕的 YouTube 视频,指导提取音频并用 whisper.cpp 本地转录,也涵盖格式转换和长录音幻觉恢复。

主要功能

  1. 01提取 16 kHz WAV 音频
  2. 02使用 whisper.cpp 本地转录
  3. 03转换为流水线转录格式
  4. 04修复长直播中的幻觉

适用场景

  1. 01转录没有字幕的视频
  2. 02处理长时间直播录制
  3. 03为视频分块准备转录稿

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/zenithth/anibon-stream-synthesis --skill anibon-local-transcription

安装后在 Claude Code 中输入/anibon-local-transcription确认已加载。

详细介绍

Anibon 本地音频转录

概览与触发条件

当 YouTube 没有目标视频的字幕或自动生成字幕时使用。

1. 提取音频

直接将音频流下载为单声道、16kHz、16 位 PCM WAV(避免临时文件,以及长达 8 小时以上的流导致 WAV 文件头溢出):

# Direct single-pass extraction via yt-dlp + ffmpeg:
yt-dlp -f ba -x --audio-format wav --postprocessor-args "ExtractAudio:-ar 16000 -ac 1 -c:a pcm_s16le" -o "audio_16k.%(ext)s" "VIDEO_URL"

说明

如果 YouTube 返回 HTTP Error 403: Forbidden,请先更新 yt-dlp:

yt-dlp --update

或者转换已有音频:

ffmpeg -i audio.wav -ar 16000 -ac 1 -c:a pcm_s16le audio_16k.wav

2. 本地转录

运行支持 GPU 加速的 whisper.cpp 构建版本:

检查系统 PATH 或本地 $HOME/whisper.cpp 构建目录中是否有 whisper-cli:

  • $HOME/whisper.cpp/build/bin/whisper-cli(Windows 上为 .exe)
  • $HOME/whisper.cpp/main

模型路径默认为 $HOME/whisper.cpp/models/ggml-large-v3-turbo.bin。

执行:

# Linux/macOS
whisper-cli -m models/ggml-large-v3-turbo.bin -f audio_16k.wav -l th -ot 240000 -t 8 --output-json -of whisper_output

# Windows (Vulkan Device 0: RX 7600, or Device 1: Tesla P100):
C:\Users\peter\whisper.cpp\build\bin\whisper-cli.exe -m C:\Users\peter\whisper.cpp\models\ggml-large-v3-turbo.bin -f audio_16k.wav -ot 240000 -l th -t 8 --output-json -of whisper_output --print-progress

(-ot 240000 偏移量会跳过待机/片头中的前 4 分钟静音,以避免静音导致的重复循环问题。)

如需完整的构建选项和平台配置,请参阅 BUILD_WHISPERCPP_GUILD.md。

3. 格式转换与 Windows 不变量

将 whisper_output.json 直接转换为流水线标准的 raw_transcript.json:

重要

Windows UTF-8 不变量:

  1. Windows 默认控制台编码(cp1252/charmap)会在打印泰语文本时崩溃。始终使用 python -X utf8 运行脚本。
  2. MSVC whisper-cli 输出可能会在片段缓冲区截断处拆分多字节泰语 UTF-8 字符。始终使用 errors="replace" 解码 JSON。

运行内联 Python 转换器:

python -X utf8 -c "
import json

def fmt_ts(seconds):
    h = int(seconds // 3600)
    m = int((seconds % 3600) // 60)
    s = int(seconds % 60)
    return f'{h:02d}:{m:02d}:{s:02d}'

with open('whisper_output.json', 'rb') as f:
    data = json.loads(f.read().decode('utf-8', errors='replace'))

items = []
for seg in data.get('transcription', []):
    text = seg.get('text', '').strip()
    if not text:
        continue
    offsets = seg.get('offsets', {})
    start_sec = offsets.get('from', 0) / 1000.0
    end_sec = offsets.get('to', 0) / 1000.0
    items.append({
        'text': text,
        'start': round(start_sec, 2),
        'duration': round(end_sec - start_sec, 2),
        'timestamp': fmt_ts(start_sec)
    })

with open('raw_transcript.json', 'w', encoding='utf-8') as out:
    json.dump(items, out, ensure_ascii=False, indent=2)
"

4. 幻觉检测与恢复

对于时长 $\ge 2\text{h}$ 的直播流,Whisper 可能会在音乐或静音片段上循环。运行并行分治恢复:

python -X utf8 ..\whisper-corruption-recovery\scripts\fix_hallucinations.py whisper_output.json audio_16k.wav --devices 0 -w 3 -o raw_transcript.json

然后使用 prepare_video.py 进行分块:

python -X utf8 scripts\prepare_video.py <VIDEO_URL> --workspace <WORKSPACE> --format txt --block 300 --overlap 30

评论

支持 Markdown