通过字幕总结YouTube视频内容
<p style="text-indent: 2em; font-style: italic;">本文来自<a href="https://blog.lilydjwg.me/">依云's Blog</a>,转载请注明。</p><p> 现在YouTube首页的视频推荐越来越差了,好多标题党、clickbait,故意把讨论的主题藏起来。经常我点进去看了好久,才发现原来并没有讨论什么我之前不知道信息,又或者讨论的话题我完全不感兴趣。再不就是把我想知道的信息藏到不知道哪个片段里。虽然我有<a href="https://github.com/polywock/globalSpeed">GlobalSpeed</a>扩展可以依内容信息密度来方便地调整播放速度,但是调太快(超过2x)就听不清啦。总之是好多视频点开看之前十分吸引人,但看完或者看一半时就想骂人、点踩退出,十分浪费时间。</p> <p> 正好最近在尝试Gemini API,于是灵光一现,写了个脚本,使用<a href="https://github.com/yt-dlp/yt-dlp">yt-dlp</a>下载视频字幕,然后调用Gemini API来总结内容。</p> <pre class="brush: python;" title="yt-summarize"> #!/usr/bin/python3 import sys import json import subprocess import tempfile from pathlib import Path import httpx GEMINI_KEY = 'YOUR GEMINI KEY HERE' URL = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:streamGenerateContent?alt=sse' PROMPT = '根据以下字幕文本总结视频内容。总结结果中请不要包含任何赞助商推广信息。' def main(url, sublang): with tempfile.TemporaryDirectory() as d: subprocess.run([ 'yt-dlp', '--sub-langs', sublang, '--write-subs', '--write-auto-subs', '--skip-download', url, ], cwd=d, check=True, ) p = Path(d) try: file = tuple(p.iterdir())[0] except IndexError: sys.exit('No subtitles.') for _ in range(2): try: do_request(file) break except httpx.ReadError as e: print(e, file=sys.stderr) def do_request(filepath): client = httpx.Client(http2=True) filename = filepath.name with filepath.open() as f: subtitles = f.read() parts = [{ 'text': PROMPT, }, { 'text': f'文件名:{filename}\n文件内容:\n{subtitles}', }] j = { 'contents': [{ 'parts': parts }], } with client.stream( 'POST', URL, headers = { "X-goog-api-key": GEMINI_KEY, "Content-Type": "application/json", }, json=j, timeout=120, ) as r: for line in r.iter_lines(): if not line.startswith('data: '): continue line = line.removeprefix('data: ') data = json.loads(line) for a in data['candidates']: for b in a['content']['parts']: text = b['text'] if not text: break print(text, end='', flush=True) print() if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('URL', help='YouTube URL') parser.add_argument('--lang', default='en', help='choose subtitles language') args = parser.parse_args() main(args.URL, args.lang) </pre> <p> 脚本依赖Python和httpx库。当然鉴于httpx已经不再更新,你换成<a href="https://github.com/pydantic/httpx2">httpx2</a>应该也能用。Gemini Key可以去<a href="https://aistudio.google.com/app/projects">这里</a>生成,然后填到脚本开头。我使用的是gemini-3.1-flash-lite这个模型,因为免费版本中,它的每日请求数配额比较充足。</p> <p> 当然啦,视频要有CC字幕这个脚本才能用,否则会报错。默认使用英文字幕,包含自动生成的版本。如果是中文视频,可以使用<code>--lang zh.*</code>参数指定用中文字幕。</p> <hr /> <p> <strong>2026年07月01日更新:</strong>脚本后续有些改进,包括支持传递yt-dlp参数、支持使用参数指定模型、支持本地模型、支持后续对话等。脚本放GitHub上了:<a href="https://github.com/lilydjwg/winterpy/blob/master/pyexe/yt-summarize">yt-summarize</a>。</p>