youtube-chapter-clipper
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYouTube Chapter Clipper
YouTube 章节剪辑工具
Overview
概述
Generate chapter clips from a YouTube video by downloading MP4 + English subtitles, segmenting content, cutting clips, and producing per-chapter English SRTs. Chapter length is user-selectable (1-2, 2-3, or 3-4 minutes).
通过下载MP4视频+英文字幕、分割内容、剪辑片段并生成按章节划分的英文SRT字幕,从YouTube视频中生成章节片段。章节时长可由用户选择(1-2分钟、2-3分钟或3-4分钟)。
Workflow
工作流程
1) Use the automation script to reduce tokens
1) 使用自动化脚本减少Token消耗
- Prefer for end-to-end runs (download, chaptering, clip cut, subtitle slicing).
scripts/smart_edit.py - The script uses heuristic chaptering to avoid AI token usage.
- Create and use a local venv (no external packages required):
python3 -m venv .venvsource .venv/bin/activatepython scripts/smart_edit.py --help- Speed-focused default: (approximate cuts, faster encode, optional downscale).
--mode fast - Use when you need precise boundaries.
--mode accurate
- 优先使用进行端到端运行(下载、章节划分、片段剪辑、字幕分割)。
scripts/smart_edit.py - 该脚本采用启发式章节划分方式,避免消耗AI Token。
- 创建并使用本地venv(无需外部包):
python3 -m venv .venvsource .venv/bin/activatepython scripts/smart_edit.py --help- 以速度为导向的默认模式:(近似剪辑、更快编码、可选降分辨率)。
--mode fast - 当需要精准边界时使用。
--mode accurate
2) Confirm inputs and environment
2) 确认输入和环境
- Ask for the YouTube URL and whether English subtitles are available (manual preferred; auto as fallback).
- Check tools: and
yt-dlp. If missing, install before proceeding.ffmpeg - Use command templates in .
references/commands.md
- 向用户索要YouTube URL,并确认是否有英文字幕(优先手动字幕;自动字幕作为备选)。
- 检查工具:和
yt-dlp。若缺失,需先安装再继续。ffmpeg - 使用中的命令模板。
references/commands.md
3) Download source video and subtitles
3) 下载源视频和字幕
- Check current directory for existing source files before downloading:
- If and
<id>.mp4already exist, skip yt-dlp download.<id>.en.vtt
- If
- Download highest 1080p MP4 and English VTT. Save in current directory with ID-based names:
<id>.mp4- (or
<id>.en.vttif manual subs absent)<id>.en.auto.vtt
- Also capture video metadata (id, title, duration, uploader) for reporting.
- The script handles this when is provided.
--url
- The script handles this when
- 下载前检查当前目录是否已有源文件:
- 若和
<id>.mp4已存在,跳过yt-dlp下载步骤。<id>.en.vtt
- 若
- 下载最高清1080p MP4视频和英文VTT字幕。以视频ID为命名保存在当前目录:
<id>.mp4- (若没有手动字幕则使用
<id>.en.vtt)<id>.en.auto.vtt
- 同时捕获视频元数据(ID、标题、时长、上传者)用于生成报告。
- 当提供参数时,脚本会自动处理此步骤。
--url
- 当提供
4) Prepare output directory
4) 准备输出目录
- Create output directory using the original video title:
- Replace spaces with underscores.
- Remove/replace filesystem-unsafe characters.
- Place all chapter clips and subtitle files into this directory.
- 以原始视频标题创建输出目录:
- 将空格替换为下划线。
- 移除或替换文件系统不支持的字符。
- 将所有章节片段和字幕文件放入该目录。
5) Generate fine-grained chapters (user-selected length)
5) 生成细粒度章节(用户可选时长)
- Ask the user to choose a chapter length preset: 1-2, 2-3, or 3-4 minutes.
- Perform AI analysis (critical step):
- Read the full subtitle content.
- Understand the semantic flow and topic transitions.
- Identify natural topic switch points.
- Draft chapter boundaries based on semantic topic changes and sentence boundaries.
- Target the selected range; avoid cutting mid-sentence.
- Prefer semantic breaks (new concept, example, recap) over strict timing.
- Produce a chapter list with:
- ,
title,start,endreason - The script uses (or
--chapter-presetfor custom).--min-seconds/--target-seconds/--max-seconds
- 请用户选择章节时长预设:1-2分钟、2-3分钟或3-4分钟。
- 执行AI分析(关键步骤):
- 读取完整字幕内容。
- 理解语义流程和主题转换。
- 识别自然的主题切换点。
- 根据语义主题变化和句子边界草拟章节边界。
- 目标时长符合所选范围;避免在句子中间截断。
- 优先选择语义断点(新概念、示例、回顾)而非严格按时间划分。
- 生成章节列表,包含:
- (标题)、
title(开始时间)、start(结束时间)、end(划分原因)reason - 脚本使用(或自定义参数
--chapter-preset)。--min-seconds/--target-seconds/--max-seconds
6) Cut precise clips (speed vs accuracy)
6) 剪辑精准片段(速度vs精度)
- Use ffmpeg with accurate trimming and stable outputs. Always re-encode:
- Place after
-ssfor accurate seeking.-i - Use +
libx264,aac, and-movflags +faststartto maximize player compatibility.-pix_fmt yuv420p - Use a fast preset (e.g., ) to avoid long encodes and timeouts.
-preset veryfast
- Place
- Run clips serially and avoid external timeouts that kill ffmpeg mid-write.
- After each clip, validate with ; retry once if validation fails.
ffprobe - If speed is the priority (listening practice), prefer approximate cuts:
- Put before
-ssto avoid decoding from the start every time.-i - Use and a higher CRF (e.g., 28).
-preset ultrafast - Optionally downscale (e.g., width 1280) to reduce encode time.
- Put
- Name each clip with an ordered prefix: using safe filenames:
<nn>_<chapter_title>.mp4- Use a 2-digit index starting at 01.
- Replace spaces with underscores.
- Remove filesystem-unsafe characters.
- 使用ffmpeg进行精准修剪并生成稳定输出。始终重新编码:
- 将放在
-ss之后以实现精准定位。-i - 使用+
libx264编码、aac和-movflags +faststart以最大化播放器兼容性。-pix_fmt yuv420p - 使用快速预设(如)避免长时间编码和超时。
-preset veryfast
- 将
- 串行运行剪辑任务,避免外部超时导致ffmpeg中途终止写入。
- 每个片段剪辑完成后,用验证;若验证失败则重试一次。
ffprobe - 若优先考虑速度(如用于听力练习),可选择近似剪辑:
- 将放在
-ss之前,避免每次都从头解码。-i - 使用和更高的CRF值(如28)。
-preset ultrafast - 可选降分辨率(如宽度1280)以减少编码时间。
- 将
- 为每个片段添加有序前缀命名:,使用安全文件名规则:
<nn>_<chapter_title>.mp4- 从01开始的两位数字索引。
- 将空格替换为下划线。
- 移除文件系统不支持的字符。
7) Extract and convert subtitles per chapter
7) 按章节提取并转换字幕
- Extract VTT segment for each chapter by time range.
- Convert each segment to SRT:
<nn>_<chapter_title>.en.srt- The script deletes per-chapter VTT unless is set.
--keep-vtt
- 按时间范围提取每个章节对应的VTT片段。
- 将每个片段转换为SRT格式:
<nn>_<chapter_title>.en.srt- 除非设置参数,否则脚本会删除按章节划分的VTT文件。
--keep-vtt
8) Report outputs
8) 输出报告
- Print output directory path, chapter list, and generated files.
- 打印输出目录路径、章节列表和生成的文件。
Output Rules
输出规则
- Source files stay in current directory (,
<id>.mp4).<id>.en.vtt - All chapter clips and subtitle files are placed in the per-video directory named after the sanitized title.
- Use consistent time formats ().
HH:MM:SS.mmm
- 源文件保留在当前目录(、
<id>.mp4)。<id>.en.vtt - 所有章节片段和字幕文件放入以清理后的标题命名的视频专属目录中。
- 使用统一的时间格式()。
HH:MM:SS.mmm
References
参考资料
- Command templates and copy/paste examples:
references/commands.md - Automation:
scripts/smart_edit.py
- 命令模板和复制粘贴示例:
references/commands.md - 自动化脚本:
scripts/smart_edit.py