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

推荐订阅源

A
About on SuperTechFans
Cloudbric
Cloudbric
C
CERT Recently Published Vulnerability Notes
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
C
Cisco Blogs
T
Tenable Blog
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
I
Intezer
AWS News Blog
AWS News Blog
IT之家
IT之家
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
博客园 - Franky
人人都是产品经理
人人都是产品经理
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hacker News: Front Page
Latest news
Latest news
Scott Helme
Scott Helme
腾讯CDC
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
S
Securelist
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog
Project Zero
Project Zero
Google DeepMind News
Google DeepMind News
P
Palo Alto Networks Blog
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 最新话题
S
Schneier on Security
NISL@THU
NISL@THU
Jina AI
Jina AI
M
MIT News - Artificial intelligence

依云's Blog - Comments

Awesome 的 GitHub 今日贡献指示器:今天你 push 了吗? 自定义系统默认中文字体 - 依云's Blog 使用wayvnc远程访问无头Wayfire会话 - 依云's Blog 为什么我反对普遍地静态链接? - 依云's Blog Poker II 键盘调教记 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog 给论坛用上了文本嵌入模型 - 依云's Blog 给论坛用上了文本嵌入模型 - 依云's Blog 在 Linux 下设置录音笔的时间 - 依云's Blog 在 Linux 下设置录音笔的时间 - 依云's Blog 使用 Restic 备份数据 - 依云's Blog 使用 Restic 备份数据 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog Arch Linux 中文论坛迁移杂记 - 依云's Blog QQ群邮件:美好生活路上的又一障碍 - 依云's Blog QQ群邮件:美好生活路上的又一障碍 - 依云's Blog
通过字幕总结YouTube视频内容 - 依云's Blog
依云 · 2026-06-16 · via 依云's Blog - Comments

现在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参数指定用中文字幕。