Extract subtitles from a YouTube video and output a Chinese transcript.
Quick start
python3 scripts/get_transcript.py "https://www.youtube.com/watch?v=VIDEO_ID"
Workflow
- Get the video link: extract the YouTube URL from the user message
- Run the script: execute
scripts/get_transcript.pyto extract subtitles - Process the output:
- If the subtitles are in Chinese: output directly
- If the subtitles are in English: translate into Chinese before outputting
- Save the result: save as a Markdown file according to the user's needs
Script parameters
| Parameter | Description | Default value |
|---|---|---|
url |
YouTube video link or ID | Required |
-f, --format |
Output format: text, markdown, json |
markdown |
-t, --timestamps |
Include timestamps | No |
-l, --lang |
Preferred languages (comma-separated) | zh,en |
-o, --output |
Output file path | stdout |
Usage examples
Basic usage
python3 scripts/get_transcript.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
Output with timestamps
python3 scripts/get_transcript.py -t "https://youtu.be/VIDEO_ID"
Save to file
python3 scripts/get_transcript.py -o output.md "https://www.youtube.com/watch?v=VIDEO_ID"
JSON format (convenient for post-processing)
python3 scripts/get_transcript.py -f json "VIDEO_ID"
Supported URL formats
https://www.youtube.com/watch?v=VIDEO_IDhttps://youtu.be/VIDEO_IDhttps://www.youtube.com/embed/VIDEO_ID- Use the Video ID directly
Language priority
The script looks for subtitles in the following order:
- Simplified Chinese (zh-Hans)
- Traditional Chinese (zh-Hant)
- Chinese (zh)
- English (en)
- Auto-generated subtitles
Translation handling
If English subtitles are obtained, they need to be translated into Chinese:
- Run the script to obtain English subtitles
- Use Claude's built-in capabilities to translate into fluent Chinese
- Preserve the original structure and paragraph division
Error handling
| Error | Cause | Solution |
|---|---|---|
| No captions available | The video has no subtitles | Inform the user that no subtitles are available for this video |
| Cannot extract video ID | Incorrect URL format | Ask the user to provide the correct YouTube link |
| Network timeout | Network issue | Retry or check the network connection |
Output format example
Markdown format
# 视频标题
- **Video ID**: abc123
- **语言**: 中文(自动生成)
- **链接**: https://www.youtube.com/watch?v=abc123
---
## 文字稿
这是视频的第一段内容...
这是视频的第二段内容...