A simple Python script to download transcripts from YouTube videos with optional timestamps.
This script downloads the transcript (subtitles/captions) from any YouTube video and saves it as a text file. It also fetches the video title and includes it at the top of the transcript for easy reference.
- Downloads transcripts from YouTube videos
- Automatically fetches and includes video title
- Optional timestamp inclusion in
[MM:SS]format - Saves transcript to a text file named
{video_id}_transcript.txt - Clean, readable output
You'll need Python 3.6 or higher installed on your system.
- Install the required dependencies:
pip install youtube-transcript-api yt-dlpNote: Make sure to use pip install (not apt-get) for Python packages.
- Open
youtube_transcript_download.pyin a text editor - Update the
urlvariable with your YouTube video URL:url = "https://www.youtube.com/watch?v=YOUR_VIDEO_ID"
- Run the script:
python youtube_transcript_download.py
The transcript will be printed to the console and saved to a file named {video_id}_transcript.txt.
To include timestamps in the transcript, set include_timestamps to True:
include_timestamps = TrueThis will format each line like:
[00:15] Welcome to this video
[00:18] Today we're going to talk about...
Keep include_timestamps as False for a clean transcript without timestamps:
include_timestamps = FalseThe script has two main configuration options in the === CONFIGURATION === section:
url: The YouTube video URL you want to download the transcript frominclude_timestamps: Boolean flag (TrueorFalse) to include/exclude timestamps
File: kE3hPpAanXg_transcript.txt
Title: Example Video Title
This is the first line of the transcript.
This is the second line.
And so on...
- The video may not have captions/subtitles available
- Try a different video that you know has captions
- The script will still work, but the title will show as "Unknown Title"
- Check your internet connection
- Make sure you've installed both dependencies:
youtube-transcript-apiandyt-dlp - Use
pip installnotapt-getfor Python packages
This script uses youtube-transcript-api version 1.x, which uses the instance-based approach:
ytt_api = YouTubeTranscriptApi()
transcript = ytt_api.fetch(video_id)