Skip to content

Instantly share code, notes, and snippets.

@core2duoe6420
Created October 28, 2025 12:21
Show Gist options
  • Select an option

  • Save core2duoe6420/3ba2b4f8f1eabb292a9869710f802208 to your computer and use it in GitHub Desktop.

Select an option

Save core2duoe6420/3ba2b4f8f1eabb292a9869710f802208 to your computer and use it in GitHub Desktop.
Rename subtitle files for TV series to match the video filename
import os
import re
episodes = {}
regex = re.compile(r"(S(\d+))?EP?(\d+)", re.IGNORECASE)
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".mp4") or f.endswith(".mkv"):
m = regex.search(f)
if m:
season = 1
if m.group(2):
season = int(m.group(2))
episode = int(m.group(3))
episodes[f'S{season}E{episode}'] = f
for root, dirs, files in os.walk("."):
for f in files:
if f.endswith(".ass") or f.endswith(".srt"):
m = regex.search(f)
if m:
season = 1
if m.group(2):
season = int(m.group(2))
episode = int(m.group(3))
filename = episodes[f'S{season}E{episode}'][:-4]
new_filename = f[:-4]
if filename == new_filename:
continue
old_name = os.path.join(root, f)
new_name = os.path.join(root, filename + f[-4:])
print(f"{old_name} => {new_name}")
os.rename(old_name, new_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment