Last active
December 15, 2024 02:56
-
-
Save pietrocaselani/acc50805569e383a0df70988b3faf5f9 to your computer and use it in GitHub Desktop.
Script to download subtitles from all movies in a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Subtitles Downloader | |
This script downloads subtitles for video files from various sources like OpenSubtitles and TheSubDB. It is designed to fetch the best subtitle available for a video file in your chosen language, specifically for **Portuguese (Brazil)** in this setup. | |
## Dependencies | |
This script requires the following Python libraries: | |
- **subliminal**: A Python library for subtitle downloading. | |
- **babelfish**: A library to handle language codes. | |
- **click**: A command-line utility to manage arguments (optional for advanced use). | |
You can install the required dependencies using `pip`: | |
```bash | |
pip install -r requirements.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
subliminal | |
babelfish | |
click |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from datetime import timedelta | |
import os | |
import argparse | |
from babelfish import Language | |
from subliminal import download_best_subtitles, region, save_subtitles, scan_videos | |
# Configure the cache | |
region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'}) | |
def main(): | |
# Parse arguments | |
parser = argparse.ArgumentParser(description="Download PT-BR subtitles for video files in a directory.") | |
parser.add_argument("directory", help="Path to the directory containing video files.") | |
args = parser.parse_args() | |
video_folder = os.path.abspath(args.directory) | |
# Validate the directory | |
if not os.path.isdir(video_folder): | |
print(f"Error: The directory '{video_folder}' does not exist.") | |
return | |
# Scan for videos newer than 2 weeks and their existing subtitles | |
print("Scanning for video files...") | |
videos = scan_videos(video_folder, age=timedelta(weeks=2)) | |
if not videos: | |
print("No video files found in the specified folder.") | |
return | |
print(f"Found {len(videos)} video file(s). Searching for PT-BR subtitles...") | |
# Download the best PT-BR subtitles | |
subtitles = download_best_subtitles(videos, {Language('por', 'BR')}) | |
# Save subtitles next to the video files | |
for video in videos: | |
if video in subtitles: | |
save_subtitles(video, subtitles[video]) | |
print(f"Saved subtitles for: {video.name}") | |
else: | |
print(f"No subtitles found for: {video.name}") | |
print("Subtitle download complete.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment