Skip to content

Instantly share code, notes, and snippets.

@krisives
Created November 17, 2018 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisives/7a15b90ba56658d2fe44b19e684616d6 to your computer and use it in GitHub Desktop.
Save krisives/7a15b90ba56658d2fe44b19e684616d6 to your computer and use it in GitHub Desktop.
Fix season/episode named files to be simple
#!/usr/bin/env python3
import sys
import re
import os.path
for path in sys.argv[1:]:
name = os.path.basename(path)
match = re.search(r's([0-9]+).?e([0-9]+)', name, re.I)
if match:
dir = os.path.dirname(path)
season = int(match[1])
episode = int(match[2])
ext = os.path.splitext(name)
renamed = os.path.join(dir, "S%02dE%02d" % (season, episode) + ext[1])
if not os.path.exists(renamed):
os.rename(path, renamed)

Download the fix-season-names file and place it into /usr/local/bin then anytime you download a file and wish to rename it to a simpler name, run fix-season-names <name of file> on the command line and it will rename the files to a simple name like "S02E03.mkv" or similar.

If you have a lot of files, you can run it like this:

fix-season-names *.mkv *.mp4

If you have files in nested directories you can run it like this:

find -name "*.mkv" | xargs -n 1 fix-season-names

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment