1. 實現功能
M4-1 接入 whisper + ollama qwen3:14b 總結字幕
- 自動下載視頻元數據
- 如果有字幕,只下載字幕
- 使用 ollama 的 qwen3:14b 對字幕內容進行總結
2.運行效果
🔍 正在提取視頻元數據…
📝 正在下載所有可用字幕…
[youtube] Extracting URL: https://youtu.be/AU9F-6uWCgE
[youtube] AU9F-6uWCgE: Downloading webpage
[youtube] AU9F-6uWCgE: Downloading tv client config
[youtube] AU9F-6uWCgE: Downloading tv player API JSON
[info] AU9F-6uWCgE: Downloading subtitles: zh-Hans
[info] AU9F-6uWCgE: Downloading 1 format(s): 248+251
Deleting existing file downloads\這AI會做物理題?Skywork-r1v 智商實測,結果出人意料!WebUI 體驗。.zh-Hans.srt
[info] Writing video subtitles to: downloads\這AI會做物理題?Skywork-r1v 智商實測,結果出人意料!WebUI 體驗。.zh-Hans.srt
WARNING: The extractor specified to use impersonation for this download, but no impersonate target is available. If you encounter errors, then see https://github.com/yt-dlp/yt-dlp#impersonation for information on installing the required dependencies
[download] Destination: downloads\這AI會做物理題?Skywork-r1v 智商實測,結果出人意料!WebUI 體驗。.zh-Hans.srt
[download] 100% of 21.61KiB in 00:00:00 at 116.50KiB/s
🤖 正在用 ollama 的 qwen3:14b 總結字幕 …
=== 總結結果 ===
-
🎯 本期主要話題:介紹開源多模態模型 Skywork-r1v 的功能、部署方式和實際應用效果。
-
📌 內容要點:
- Skywork-r1v 是由昆侖萬維開發的多模態模型,使用強化學習訓練,能夠理解圖片并進行推理。
- 該模型可以通過 GitHub 和 Huggingface 下載,并支持兩種推理方式:Transforms 庫和 VLLM。
- 部署過程中需要安裝依賴組件,并手動配置 WebUI 程序以實現本地運行。
- 模型在處理圖片和推理問題時表現良好,但也存在部分推理錯誤的情況。
- 🌟 精彩片段或亮點:
- 模型能夠根據圖片內容進行推理,例如分析圖片中的杯子數量和長度關系。
- 在演示過程中,模型對一張折射圖片的分析正確,展示了其在特定場景下的準確性和理解能力。
3.實現過程
3.1 搭建環境
Python 環境安裝(推薦):
mkdir ytdlp && cd ytdlp
conda create -n ytdlp python=3.10 -y
conda activate ytdlp
pip install -U “yt-dlp[default]”
3.1.1 打開anaconda
3.1.2 通過圖像化界面創建ytdlp環境
3.1.3 打開ytdlp的open terminal
pip install -U “yt-dlp[default]”
3.1.4 vscode python解析器 選擇ytdlp
3.1.5 獲得cookies.txt內容
3.2 代碼
from yt_dlp import YoutubeDL
import os
import subprocessdef get_parent_cookies():return os.path.join(os.path.dirname(os.path.dirname(__file__)), 'cookies.txt')def extract_video_metadata(url):ydl_opts = {'cookiefile': get_parent_cookies(),'quiet': True,'no_warnings': False,'extract_flat': False,'extractor_args': {'youtube': {'player_client': ['tv_embedded', 'web'],}}}with YoutubeDL(ydl_opts) as ydl:try:info = ydl.extract_info(url, download=False)return infoexcept Exception as e:print(f"提取元數據失敗: {e}")return Nonedef download_subtitles(url, info):subtitles = info.get('subtitles', {})if not subtitles:print("\n📝 沒有可用字幕,跳過下載和總結。")return Noneprint("\n📝 正在下載所有可用字幕...")ydl_opts = {'cookiefile': get_parent_cookies(),'skip_download': True,'writesubtitles': True,'allsubtitles': True,'subtitlesformat': 'srt','outtmpl': 'downloads/%(title).80s.%(ext)s','quiet': False,}with YoutubeDL(ydl_opts) as ydl:ydl.download([url])# 查找下載的srt字幕文件downloads_dir = os.path.join(os.path.dirname(__file__), 'downloads')srt_files = [f for f in os.listdir(downloads_dir) if f.endswith('.srt')]if not srt_files:print("? 沒有找到下載的字幕文件")return Nonesrt_path = os.path.join(downloads_dir, srt_files[0])print(f"? 已下載字幕: {srt_path}")return srt_pathdef summarize_with_ollama(srt_path):print(f"\n🤖 正在用 ollama 的 qwen3:14b 總結字幕 ...")# 自動安裝ollama包(如未安裝)try:import ollamaexcept ImportError:import sys, subprocesssubprocess.check_call([sys.executable, '-m', 'pip', 'install', 'ollama'])import ollama# 讀取并清洗SRT字幕內容,拼接為段落def srt_to_text(srt_content):import relines = srt_content.splitlines()text_lines = []for line in lines:# 跳過序號和時間軸if re.match(r"^\d+$", line):continueif re.match(r"^\d{2}:\d{2}:\d{2},\d{3} --> ", line):continueif line.strip() == '':continuetext_lines.append(line.strip())# 合并為段落,去重相鄰重復from itertools import groupbymerged = [k for k, _ in groupby(text_lines)]return ' '.join(merged)with open(srt_path, 'r', encoding='utf-8') as f:srt_content = f.read()clean_text = srt_to_text(srt_content)# 構造 promptprompt = f"""
你是一名專業的視頻內容總結助手,請對下列中文視頻字幕內容進行總結。目標:讓用戶能在30秒內了解這期視頻的核心內容。請按以下格式輸出:
1. 🎯 本期主要話題(用一句話概括主題)
2. 📌 內容要點(3-5條,每條 1 句話)
3. 🌟 精彩片段或亮點(選出最值得一提的內容,1-2條)?? 不要加入你的思考過程,不要說“我認為”或“可能”,只根據字幕原文總結。字幕內容如下:
———
{clean_text}
———
/no_think
"""try:response = ollama.chat(model='qwen3:14b',messages=[{"role": "user", "content": prompt}])print("\n=== 總結結果 ===\n")print(response['message']['content'])except Exception as e:print(f"調用 ollama 失敗: {e}")def main():url = "https://youtu.be/AU9F-6uWCgE"print("🔍 正在提取視頻元數據...")info = extract_video_metadata(url)if not info:print("? 無法獲取視頻信息")returnsrt_path = download_subtitles(url, info)if srt_path:summarize_with_ollama(srt_path)if __name__ == "__main__":main()