Skip to content

Instantly share code, notes, and snippets.

@ph33nx
Last active March 2, 2025 10:38
Show Gist options
  • Save ph33nx/0c320ae1c8d4dfb29c560d8a9f9088ad to your computer and use it in GitHub Desktop.
Save ph33nx/0c320ae1c8d4dfb29c560d8a9f9088ad to your computer and use it in GitHub Desktop.
Python script to recursively download missing subtitles for video files
#!/usr/bin/env python3
import os
import sys
from babelfish import Language
from subliminal import download_best_subtitles, region, save_subtitles, scan_video
# Configure subliminal cache
region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'})
# Supported video extensions
VIDEO_EXTENSIONS = ('.mp4', '.mkv', '.avi', '.mov')
def download_subtitles_for_directory(directory, languages=['eng']):
"""
Recursively scan a directory for video files and download subtitles if missing.
:param directory: Path to the directory to scan
:param languages: List of languages for subtitles (default: ['eng'])
"""
for root, _, files in os.walk(directory):
for file in files:
# Skip files starting with dot (e.g., ".file" or "._file")
if file.startswith('.') or file.startswith('._'):
print(f"Skipping hidden or system file: {file}")
continue
if file.endswith(VIDEO_EXTENSIONS):
video_path = os.path.join(root, file)
subtitle_path = os.path.splitext(video_path)[0] + '.srt'
# Skip if subtitles already exist
if os.path.exists(subtitle_path):
print(f"Subtitle already exists for: {video_path}")
continue
print(f"Processing video: {video_path}")
try:
# Scan the video
video = scan_video(video_path)
if not video:
print(f"Could not process video: {video_path}")
continue
# Download subtitles
subtitles = download_best_subtitles([video], {Language(lang) for lang in languages})
if video in subtitles:
save_subtitles(video, subtitles[video])
print(f"Subtitle downloaded and saved for: {video_path}")
else:
print(f"No subtitles found for: {video_path}")
except Exception as e:
print(f"Error processing {video_path}: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python download_subtitles.py <directory> [language]")
sys.exit(1)
directory = sys.argv[1]
language = sys.argv[2] if len(sys.argv) > 2 else 'eng'
if not os.path.isdir(directory):
print(f"Invalid directory: {directory}")
sys.exit(1)
print(f"Scanning directory: {directory} for language: {language}")
download_subtitles_for_directory(directory, languages=[language])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment