Skip to content

omayaro/subtitle_generator

Repository files navigation

Subtitle Generator

Automatically generate subtitles for video files using FFmpeg and Whisper. Supports English subtitles and Korean translation.

Features

  • 🎬 Extract audio from video files using FFmpeg
  • 🎀 Generate English subtitles using Whisper AI
  • 🌏 Translate English subtitles to Korean (free, no API key required)
  • πŸ”€ Merge Korean and English subtitles into a single file
  • 🧹 Automatic cleanup of temporary files

Prerequisites

1. Python (Required)

Download and install Python 3.7 or higher from python.org

Verify installation:

python --version

2. FFmpeg (Required)

FFmpeg is used to extract audio from video files.

Windows Installation Options:

Option A: Using Chocolatey

First, install Chocolatey (if not already installed):

  1. Open PowerShell as Administrator
  2. Run this command:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  1. Verify installation: choco --version

Then install FFmpeg:

choco install ffmpeg

Option B: Using Winget (Recommended - No extra installation needed)

Winget is built into Windows 10 (version 1809+) and Windows 11. No additional installation required.

Check if winget is available:

winget --version

If available, install FFmpeg:

winget install ffmpeg

Note: If winget is not found, you may need to update Windows or use Option A or C instead.

Option C: Manual Installation

  1. Download from ffmpeg.org
  2. Extract and add to system PATH
  3. Or download Windows builds from ffmpeg.zeranoe.com

Verify installation:

ffmpeg -version

3. Python Libraries (Required)

Install all required Python packages using requirements.txt:

pip install -r requirements.txt

This will install:

  • openai-whisper - For subtitle generation
  • deep-translator - For translation

Alternative: Install individually

pip install openai-whisper
pip install deep-translator

Note: First run will download the Whisper model (medium size, ~1.4GB). This is a one-time download.

GPU Acceleration (Optional):

By default, pip install openai-whisper installs PyTorch CPU version, which works but is slower. If you have an NVIDIA GPU and want faster processing:

  1. First, install the regular packages:

    pip install -r requirements.txt
  2. Then install PyTorch with CUDA support:

    pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

    (Replace cu118 with your CUDA version if different. Check your CUDA version with nvidia-smi)

Note:

  • If you don't have an NVIDIA GPU β†’ Skip this step, CPU version will work fine (just slower)
  • If you have an NVIDIA GPU β†’ Installing CUDA PyTorch will make processing much faster

Verify installation:

whisper --help

Installation Summary

Run these commands to install everything:

# Install Python (if not already installed)
# Download from https://www.python.org/downloads/

# Install FFmpeg (choose one method)
# Option 1: Using Chocolatey (requires Chocolatey installation first)
choco install ffmpeg
# Option 2: Using Winget (Windows 10/11 built-in, no extra installation needed)
winget install ffmpeg
# Option 3: Manual download from ffmpeg.org

# Install Python libraries (recommended)
pip install -r requirements.txt

# OR install individually:
# pip install openai-whisper
# pip install deep-translator

Usage

Basic Usage

Video File Processing

# Generate English subtitles only (default)
subtitle_generator.bat video.mp4

# Generate Korean subtitles only (translated from English)
subtitle_generator.bat video.mp4 ko

# Generate both Korean and English subtitles (Korean on top)
subtitle_generator.bat video.mp4 both

SRT File Processing

# Translate SRT file (auto-detects Korean <-> English)
subtitle_generator.bat english.srt translate
subtitle_generator.bat korean.srt translate

# Merge Korean and English SRT files into one file
subtitle_generator.bat korean.srt english.srt merge

Examples

Video File Examples

# English subtitles
subtitle_generator.bat my_video.mp4

# Korean subtitles (translated)
subtitle_generator.bat my_video.mp4 ko

# Bilingual subtitles
subtitle_generator.bat my_video.mp4 both

SRT File Examples

# Translate English SRT to Korean (auto-detected)
subtitle_generator.bat video.srt translate
# Output: video.ko.srt

# Translate Korean SRT to English (auto-detected)
subtitle_generator.bat video.ko.srt translate
# Output: video.en.srt

# Merge Korean and English SRT files
subtitle_generator.bat video.ko.srt video.srt merge
# Output: video.merged.srt (Korean on top, English below)

Output Files

Video File Processing

  • English only: video.srt
  • Korean only: video.srt (translated from English)
  • Both languages: video.srt (Korean on top, English below)

SRT File Processing

  • Translate:
    • English β†’ Korean: video.ko.srt (auto-detected)
    • Korean β†’ English: video.en.srt (auto-detected)
  • Merge: video.merged.srt (Combined Korean + English subtitles)

Example bilingual subtitle format:

1
00:00:00,000 --> 00:00:02,000
μ•ˆλ…• 쒋은 아침이야
hi good morning

How It Works

  1. Audio Extraction: FFmpeg extracts audio from video file
  2. Subtitle Generation: Whisper generates English subtitles from audio
  3. Translation (if needed): Google Translate API (via deep-translator) translates English to Korean
  4. Merging (if both): Python script merges Korean and English subtitles by time
  5. Cleanup: Temporary files (WAV, intermediate SRT files) are automatically deleted

Files

  • subtitle_generator.bat - Main batch script (handles both video and SRT files)
  • process_subtitle.py - Main Python script that orchestrates the workflow
  • merge_srt.py - Merges Korean and English SRT files
  • translate_srt.py - Translates English SRT to Korean
  • requirements.txt - Python package dependencies

Notes

  • Internet Connection: Required for translation feature (uses Google Translate)
  • Processing Time: Translation may take time depending on subtitle count
  • Rate Limiting: Excessive use may result in temporary blocking (built-in delays help prevent this)
  • Model Size: Whisper medium model (~1.4GB) is downloaded on first use
  • Supported Formats: Any video format supported by FFmpeg (MP4, AVI, MKV, etc.)

Troubleshooting

Check if GPU/CUDA is available

To check if your system can use GPU acceleration:

Method 1: Check PyTorch CUDA support

python -c "import torch; print('CUDA available:', torch.cuda.is_available()); print('CUDA version:', torch.version.cuda if torch.cuda.is_available() else 'N/A')"
  • If output shows CUDA available: True β†’ GPU will be used automatically
  • If output shows CUDA available: False β†’ Only CPU will be used (slower)

Method 2: Check NVIDIA GPU and driver

nvidia-smi
  • If this command works and shows GPU info β†’ NVIDIA GPU is detected
  • If command not found β†’ No NVIDIA GPU or driver not installed

Method 3: Check if CUDA toolkit is installed

nvcc --version
  • If this shows CUDA version β†’ CUDA toolkit is installed
  • If command not found β†’ CUDA toolkit may not be installed

Note: Even if you have an NVIDIA GPU, you need:

  1. NVIDIA GPU driver installed
  2. CUDA toolkit installed (optional, but recommended)
  3. PyTorch with CUDA support installed (see Prerequisites section for installation)

When do you need CUDA PyTorch?

  • βœ… You have NVIDIA GPU + want faster processing β†’ Install CUDA PyTorch
  • ❌ You don't have NVIDIA GPU β†’ Use default CPU version (no extra installation needed)
  • ❌ You have GPU but don't mind slower processing β†’ Use default CPU version

FFmpeg not found

  • Make sure FFmpeg is installed and added to system PATH
  • Restart command prompt after installation

Whisper not found

  • Verify installation: pip install openai-whisper
  • Check if Python Scripts folder is in PATH

Translation fails

  • Check internet connection
  • Verify deep-translator is installed: pip install deep-translator
  • Wait a few minutes if rate limited

Python not found

  • Install Python from python.org
  • Make sure "Add Python to PATH" is checked during installation

License

This project uses:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages