惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

依云's Blog

自定义系统默认中文字体 - 依云's Blog Wayfire支持不缩放Xwayland啦 - 依云's Blog 使用wayvnc远程访问无头Wayfire会话 - 依云's Blog 给论坛用上了文本嵌入模型 - 依云's Blog 使用 Restic 备份数据 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog pacfiles: 高速的 pacman -F 替代品 用 Android 手机当电脑的话筒 - 依云's Blog 使用 ffmpeg 对音频文件进行响度归一化 - 依云's Blog 为团队部署邮件服务 - 依云's Blog 使用 nftables 屏蔽大量 IP - 依云's Blog YubiKey 初体验 - 依云's Blog fcitx5 码表同步方案 - 依云's Blog 我正在使用的火狐扩展(2024年版) - 依云's Blog
通过字幕总结YouTube视频内容 - 依云's Blog
依云 · 2026-06-09 · via 依云's Blog

现在YouTube首页的视频推荐越来越差了,好多标题党、clickbait,故意把讨论的主题藏起来。经常我点进去看了好久,才发现原来并没有讨论什么我之前不知道信息,又或者讨论的话题我完全不感兴趣。再不就是把我想知道的信息藏到不知道哪个片段里。虽然我有GlobalSpeed扩展可以依内容信息密度来方便地调整播放速度,但是调太快(超过2x)就听不清啦。总之是好多视频点开看之前十分吸引人,但看完或者看一半时就想骂人、点踩退出,十分浪费时间。

正好最近在尝试Gemini API,于是灵光一现,写了个脚本,使用yt-dlp下载视频字幕,然后调用Gemini API来总结内容。

#!/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)

脚本依赖Python和httpx库。当然鉴于httpx已经不再更新,你换成httpx2应该也能用。Gemini Key可以去这里生成,然后填到脚本开头。我使用的是gemini-3.1-flash-lite这个模型,因为免费版本中,它的每日请求数配额比较充足。

当然啦,视频要有CC字幕这个脚本才能用,否则会报错。默认使用英文字幕,包含自动生成的版本。如果是中文视频,可以使用--lang zh参数指定用中文字幕。