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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
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参数指定用中文字幕。