Last active
October 31, 2016 18:34
-
-
Save seniorpreacher/5f10be07338b10f3a8dddcaa91742e2f to your computer and use it in GitHub Desktop.
A short script to find and rename .str (subtitle) files after it's movie or episode file.
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
import os | |
import re | |
import time | |
print(os.getcwd()) | |
def get_part(filename, part): | |
season = re.search(r"" + part + "\d{2}", filename.lower()) | |
if season: | |
season = season.start() | |
return season | |
def get_season(filename): | |
return get_part(filename, 's') | |
def get_episode(filename): | |
return get_part(filename, 'e') | |
def get_season_name_alternate(filename): | |
pos = re.search(r"\d{1,2}x\d{2}", filename.lower()) | |
if pos: | |
pos = pos.start() | |
return tuple(filename[pos:pos+5].strip().split('x')) | |
return (None, None) | |
files = os.listdir(".") | |
movie_mode = True | |
movie_file = None | |
subtitle_file = None | |
for _file in files: | |
if _file[-4:] in ['.mkv', '.avi'] and 'sample' not in _file: | |
if movie_mode and movie_file is None: | |
movie_file = _file | |
else: | |
movie_mode = False | |
break | |
if _file[-4:] in ['.srt']: | |
if movie_mode and subtitle_file is None: | |
subtitle_file = _file | |
else: | |
movie_mode = False | |
break | |
if movie_mode and movie_file and subtitle_file: | |
print('mode: movie') | |
new_subtitle_file = movie_file[:-4] + '.srt' | |
if subtitle_file != new_subtitle_file: | |
os.rename(subtitle_file, new_subtitle_file) | |
print('\nMovie file: ' + movie_file + '\nOld subtitle file: ' + subtitle_file + '\nNew subtitle file: ' + new_subtitle_file) | |
else: | |
print('mode: tv show') | |
for episode_file in files: | |
if episode_file[-4:] in ['.mkv', '.avi']: | |
season_pos = get_season(episode_file) | |
episode_pos = get_episode(episode_file) | |
if season_pos and episode_pos: | |
season = episode_file[season_pos + 1:season_pos + 3] | |
episode = episode_file[episode_pos + 1:episode_pos + 3] | |
for subtitle_file in files: | |
if subtitle_file[-4:] in ['.srt']: | |
sub_season_pos = get_season(subtitle_file) | |
sub_episode_pos = get_episode(subtitle_file) | |
if sub_season_pos is None and sub_episode_pos is None: | |
sub_season, sub_episode = get_season_name_alternate(subtitle_file) | |
else: | |
sub_season, sub_episode = subtitle_file[sub_season_pos + 1:sub_season_pos + 3], subtitle_file[sub_episode_pos + 1:sub_episode_pos + 3] | |
if season is not None and episode is not None and sub_season is not None and sub_episode is not None: | |
season = int(season) | |
episode = int(episode) | |
sub_season = int(sub_season) | |
sub_episode = int(sub_episode) | |
if sub_season == season and sub_episode == episode: | |
new_subtitle_file = episode_file[:-4] + '.srt' | |
if subtitle_file != new_subtitle_file: | |
os.rename(subtitle_file, new_subtitle_file) | |
print('\nEpisode file: ' + episode_file + '\nOld subtitle file: ' + subtitle_file + '\nNew subtitle file: ' + new_subtitle_file) | |
print('\n --- script finished ---') | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment