How to Build an AI Video Subtitle Generator
Generate accurate subtitles for videos automatically using AI.
Jay Banlasan
The AI Systems Guy
I run an ai video subtitle generator automated across every client that publishes video content. Manual subtitle work eats hours per video. This system does it in minutes with better accuracy than most freelancers.
The approach: extract audio from video, run it through a speech-to-text model, format the output as SRT or VTT, and sync the timestamps. One script handles the full pipeline.
What You Need
- Python 3.9+
- OpenAI Whisper (local) or Deepgram API (cloud)
- FFmpeg installed on your system
- A video file to process
Step 1: Install Dependencies
pip install openai-whisper python-dotenv
brew install ffmpeg # macOS, or apt install ffmpeg on Linux
Step 2: Extract and Transcribe Audio
import whisper
import json
model = whisper.load_model("base")
def generate_subtitles(video_path):
result = model.transcribe(video_path, word_timestamps=True)
return result["segments"]
Step 3: Format as SRT
def segments_to_srt(segments):
srt_lines = []
for i, seg in enumerate(segments, 1):
start = format_timestamp(seg["start"])
end = format_timestamp(seg["end"])
text = seg["text"].strip()
srt_lines.append(f"{i}\n{start} --> {end}\n{text}\n")
return "\n".join(srt_lines)
def format_timestamp(seconds):
h = int(seconds // 3600)
m = int((seconds % 3600) // 60)
s = int(seconds % 60)
ms = int((seconds % 1) * 1000)
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
Step 4: Write the Output File
def process_video(video_path, output_path=None):
if not output_path:
output_path = video_path.rsplit('.', 1)[0] + '.srt'
segments = generate_subtitles(video_path)
srt_content = segments_to_srt(segments)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(srt_content)
print(f"Subtitles saved: {output_path}")
print(f"Segments: {len(segments)}")
return output_path
process_video("client-video.mp4")
Step 5: Add Batch Processing for Multiple Videos
import os
def batch_subtitle(video_dir, output_dir="subtitles"):
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(video_dir):
if filename.endswith(('.mp4', '.mov', '.avi', '.mkv')):
video_path = os.path.join(video_dir, filename)
srt_name = filename.rsplit('.', 1)[0] + '.srt'
output_path = os.path.join(output_dir, srt_name)
process_video(video_path, output_path)
batch_subtitle("./raw-videos")
What to Build Next
Connect this to a webhook that triggers whenever new video files land in a shared folder. The subtitles generate automatically and drop into your editing pipeline without anyone touching them.
Related Reading
- How to Build Automated Alerts That Actually Help - practical guidance for building AI-powered business systems
- Building an Automated Social Media Calendar - keeping schedules in sync without the back and forth
- Building an Automated FAQ From Customer Questions - practical guidance for building AI-powered business systems
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment