Skip to content

Instantly share code, notes, and snippets.

@minexew
Last active February 20, 2024 09:52
Show Gist options
  • Save minexew/da8e011170985508be3d6b206ce841b5 to your computer and use it in GitHub Desktop.
Save minexew/da8e011170985508be3d6b206ce841b5 to your computer and use it in GitHub Desktop.
RARBG subtitle fixer for media player compatibility
"""
RARBG subtitle fixer for media player compatibility
Migrates subtitles from a directory structure like this:
Show Title
├── Season 01
│ ├── Subs
│ │ ├── S01E01
│ │ │ └── 3_English.srt
│ │ └── S01E02
│ │ └── 3_English.srt
│ ├── S01E01.mp4
│ └── S01E02.mp4
└── Season 02
├── Subs
│ └── S02E01
│ └── 3_English.srt
└── S02E02.mp4
To a Jellyfin-friendly structure like this:
Show Title
├── Season 01/
│ ├── S01E01.mp4
│ ├── S01E01.en.srt
│ ├── S01E02.mp4
│ └── S01E02.en.srt
└── Season 02/
├── S02E01.mp4
└── S02E01.en.srt
"""
import argparse
from pathlib import Path
import shutil
parser = argparse.ArgumentParser()
parser.add_argument("dir", type=Path, nargs="?", default=Path(),
help="directory containing the show")
parser.add_argument("--lang", default="en",
help="language code, defaults to 'en'")
parser.add_argument("-n", "--dry-run", action="store_true",
help="dry run (preview operations without really copying any files)")
parser.add_argument("--sub-filename", default="3_English.srt",
help="subtitle file name to look for (defaults to '3_English.srt')")
args = parser.parse_args()
def visit_subtitle_file(season_dir: Path, path: Path):
out_path = season_dir / (path.parent.name + f".{args.lang}.srt")
print(path, "==>", out_path)
if not args.dry_run:
shutil.copy(path, out_path)
def process_subs_dir(season_dir: Path, sub_dir: Path):
if sub_dir.is_dir():
for ep_dir in sorted(sub_dir.iterdir()):
sub_path = ep_dir / args.sub_filename
if sub_path.is_file():
visit_subtitle_file(season_dir, sub_path)
sub_dir = args.dir / "Subs"
if sub_dir.is_dir():
process_subs_dir(args.dir, sub_dir)
for season_dir in sorted(args.dir.iterdir()):
sub_dir = season_dir / "Subs"
if sub_dir.is_dir():
process_subs_dir(season_dir, sub_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment