In this short guide, you'll see how to automatically generate YouTube subtitles using Python and OpenAI Whisper.
Here you can find the short answer:
(1) Using OpenAI Whisper
import whisper
model = whisper.load_model("base")
result = model.transcribe("video.mp4")
(2) Generate SRT files with timestamps
from whisper.utils import get_writer
writer = get_writer("srt", "output/")
writer(result, "video.srt")
So let's see several methods to generate accurate subtitles for YouTube videos automatically.
1: Install and setup OpenAI Whisper
Let's start with installing OpenAI Whisper, the most accurate speech-to-text model for subtitle generation:
pip install openai-whisper
Basic transcription:
import whisper
model = whisper.load_model("base")
audio_file = "video.mp4"
result = model.transcribe(audio_file)
print("Transcription:")
print(result["text"])
result will be:
Transcription:
Welcome to this tutorial on Python programming. Today we'll learn about automatic subtitle generation using OpenAI Whisper. This powerful tool can transcribe audio with high accuracy in multiple languages.
Available Whisper models (by size and accuracy):
| Model | Size | Speed | Accuracy |
|---|---|---|---|
| tiny | 39 MB | Fastest | Good |
| base | 74 MB | Fast | Better |
| small | 244 MB | Medium | Great |
| medium | 769 MB | Slow | Excellent |
| large | 1550 MB | Slowest | Best |
Choose "base" for quick testing, "small" for production balance, "large" for maximum accuracy.
2: Generate SRT subtitle files with timestamps
The most useful output format for YouTube is SRT (SubRip) which includes timestamps for each subtitle segment:
import whisper
from whisper.utils import get_writer
model = whisper.load_model("base")
audio_file = "tutorial_video.mp4"
result = model.transcribe(audio_file)
output_dir = "subtitles/"
srt_writer = get_writer("srt", output_dir)
srt_writer(result, audio_file)
print(f"SRT file created: {output_dir}tutorial_video.srt")
result:
SRT file created: subtitles/tutorial_video.srt
Generated SRT file content:
1
00:00:00,000 --> 00:00:03,500
Welcome to this tutorial on Python programming.
2
00:00:03,500 --> 00:00:07,200
Today we'll learn about automatic subtitle generation.
3
00:00:07,200 --> 00:00:11,800
This powerful tool can transcribe audio with high accuracy.
This SRT file can be directly uploaded to YouTube, added to video editors, or used with media players.
3: Generate multilingual subtitles
Whisper supports 99 languages automatically. Specify the language for better accuracy or auto-detect:
import whisper
model = whisper.load_model("base")
audio_file = "spanish_video.mp4"
result_auto = model.transcribe(audio_file)
result_spanish = model.transcribe(audio_file, language="es")
print(f"Auto-detected language: {result_auto['language']}")
print(f"\nTranscription: {result_spanish['text'][:100]}...")
result:
Auto-detected language: es
Transcription: Bienvenidos a este tutorial sobre programación en Python. Hoy aprenderemos sobre la generación...
Popular language codes:
- English:
en - Spanish:
es - French:
fr - German:
de - Japanese:
ja - Chinese:
zh - Hindi:
hi
4: Batch process multiple videos
For YouTube channels or course creators, process multiple videos efficiently:
import whisper
import os
from pathlib import Path
video_folder = "videos/"
output_folder = "subtitles/"
model = whisper.load_model("small")
video_files = list(Path(video_folder).glob("*.mp4"))
print(f"Processing {len(video_files)} videos...")
for i, video_path in enumerate(video_files, 1):
print(f"\n[{i}/{len(video_files)}] Processing: {video_path.name}")
result = model.transcribe(str(video_path))
from whisper.utils import get_writer
srt_writer = get_writer("srt", output_folder)
srt_writer(result, video_path.stem)
print(f"✓ Created: {output_folder}{video_path.stem}.srt")
print(f"\nCompleted! {len(video_files)} subtitle files generated.")
result:
Processing 3 videos...
[1/3] Processing: lecture_01.mp4
✓ Created: subtitles/lecture_01.srt
[2/3] Processing: lecture_02.mp4
✓ Created: subtitles/lecture_02.srt
[3/3] Processing: lecture_03.mp4
✓ Created: subtitles/lecture_03.srt
Completed! 3 subtitle files generated.
This saves hours of manual transcription for video series or course content.
Free Alternative Tools
For Windows users without Python:
-
OpenAI Whisper Desktop - GUI wrapper for Whisper
- Download: GitHub
- Drag-and-drop interface
- No coding required
-
YouTube Subtitles Editor
- Built into YouTube Studio
- Auto-generates subtitles
- Manual editing interface
- Free for all creators
-
Subtitle Edit
- Open-source subtitle editor
- Auto-transcription integration
- Works with Whisper
- Download: subtitleedit.org