Skip to content

Commit 6add15a

Browse files
committed
Added IMDb Episode Renamer Script
1 parent 171a58e commit 6add15a

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

imdb_episode_rename/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# IMDB Episode Rename
2+
Python script to rename episodes and subtitles and video tags.
3+
The episode names are taken from IMDb using the IMDBPy package.
4+
Video Tags are renamed using MKVToolNix's CLI.
5+
6+
## Setup
7+
- Use the included ```requirements.txt``` to install all dependencies using pip. Run ```pip install -r requirements.txt```.
8+
- To use the tag rename feature, make sure to install MKVToolNix from [here](https://mkvtoolnix.download/) and add the installation directory to your System PATH.
9+
10+
## Usage
11+
12+
```
13+
usage: python rename.py [-h] [options]
14+
15+
optional arguments:
16+
-h, --help show this help message and exit
17+
-c , --code IMDb CODE for the Series
18+
-e , --episode Rename EPISODES of the series in the specified Directory
19+
-s , --subtitle Rename SUBTITLES of the Series in the Directory
20+
-t , --tag Rename VIDEO TAGS for MKV Videos
21+
```
22+
23+
### Example
24+
- For *It's Always Sunny in Philadelphia*, the IMDb Code is ```tt000472954```. You can use ```000472954``` as well.
25+
- The Folder Structure is as given below:
26+
```
27+
. Windows D: Drive
28+
├── Episode Directory # Directory containing the episodes
29+
└── Subtitle Directory # Directory containing the subtitles
30+
```
31+
32+
- Run the script using this convention:
33+
```
34+
python rename.py -c tt000472954 -e "D:\Episode Directory" -s "D:\Subtitle Directory" -t "D:\Episode Directory"
35+
36+
python rename.py --code tt000472954 --episode "D:\Episode Directory" --subtitle "D:\Subtitle Directory" --tag "D:\Episode Directory"
37+
```
38+
39+
- The IMDb Code is compulsory. ```-e```, ```-s```, ```-t``` tags aren't.
40+
41+
## NOTES
42+
- Make sure that **ALL** episodes/subtitles are available in the one folder, not nested folders!
43+
- Due to certain limitations not yet handled, ensure that the **Episodes and Subtitles are in separate folders**!
44+
45+
## Authors
46+
[IAmOZRules](https://github.com/IAmOZRules/)

imdb_episode_rename/rename.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import os
2+
import imdb
3+
import argparse
4+
from termcolor import colored
5+
6+
arg_parser = argparse.ArgumentParser(
7+
usage='python rename.py [-h] [options]')
8+
9+
arg_parser.add_argument(
10+
'-c',
11+
'--code',
12+
type=str,
13+
help='IMDb CODE for the Series',
14+
required=False,
15+
metavar='')
16+
17+
arg_parser.add_argument(
18+
'-e',
19+
'--episode',
20+
type=str,
21+
help='Rename EPISODES of the series in the specified Directory',
22+
default=False,
23+
metavar='')
24+
25+
arg_parser.add_argument('-s', '--subtitle', type=str,
26+
help='Rename SUBTITLES of the Series in the Directory',
27+
default=False, metavar='')
28+
29+
arg_parser.add_argument(
30+
'-t',
31+
'--tag',
32+
type=str,
33+
help='Rename VIDEO TAGS for MKV Videos',
34+
required=False,
35+
metavar='')
36+
37+
args = arg_parser.parse_args()
38+
39+
not_allowed = r":\*<>\|"
40+
41+
42+
def get_episodes(imdb_code):
43+
ia = imdb.IMDb()
44+
if "tt" in imdb_code:
45+
imdb_code = imdb_code[2:]
46+
47+
series = ia.get_movie(imdb_code)
48+
print("SERIES: {}".format(colored(series, "magenta")))
49+
50+
ia.update(series, "episodes")
51+
episodes = series.data['episodes']
52+
53+
episode_list = []
54+
55+
for season in episodes.keys():
56+
for episode in episodes[season]:
57+
title = episodes[season][episode]['title']
58+
59+
if season < 10:
60+
if episode < 10:
61+
ep_name = "{} S0{}E0{} - {}".format(series,
62+
season, episode, title)
63+
else:
64+
ep_name = "{} S0{}E{} - {}".format(series,
65+
season, episode, title)
66+
else:
67+
if episode < 10:
68+
ep_name = "{} S{}E0{} - {}".format(series,
69+
season, episode, title)
70+
else:
71+
ep_name = "{} S{}E{} - {}".format(series,
72+
season, episode, title)
73+
74+
for i in ep_name:
75+
if i in not_allowed:
76+
ep_name = ep_name.replace(i, " -")
77+
if i in "\\/":
78+
ep_name = ep_name.replace(i, "-")
79+
if i in "?":
80+
ep_name = ep_name.replace(i, "")
81+
episode_list.append(ep_name)
82+
return episode_list
83+
84+
85+
def rename_episode(directory, episode_list):
86+
files = os.listdir(directory)
87+
88+
print("Renaming episodes...")
89+
90+
for i in range(len(files)):
91+
ep_name = episode_list[i]
92+
og_name = files[i]
93+
ext = og_name[-4:]
94+
if ext != ".srt":
95+
ep_name += ext
96+
filename = os.path.join(directory, og_name)
97+
newfilename = os.path.join(directory, ep_name)
98+
print("Renaming {} to {}".format(
99+
colored(filename, 'yellow'),
100+
colored(newfilename, 'green')))
101+
os.rename(filename, newfilename)
102+
print(
103+
colored(
104+
"RENAMING", "red"), colored(
105+
"EPISODES", "cyan"), colored(
106+
"FINISHED!\n", "red"))
107+
108+
109+
def rename_subtitle(directory, episode_list):
110+
files = os.listdir(directory)
111+
print("Renaming subtitles...")
112+
113+
for i in range(len(files)):
114+
ep_name = episode_list[i]
115+
og_name = files[i]
116+
ext = og_name[-4:]
117+
if ext == ".srt":
118+
ep_name += ext
119+
filename = os.path.join(directory, og_name)
120+
newfilename = os.path.join(directory, ep_name)
121+
print(
122+
"Renaming {} to {}".format(
123+
colored(
124+
filename, 'yellow'), colored(
125+
newfilename, 'green')))
126+
os.rename(filename, newfilename)
127+
print(
128+
colored(
129+
"RENAMING", "red"), colored(
130+
"SUBTITLES", "cyan"), colored(
131+
"FINISHED!\n", "red"))
132+
133+
134+
def edit_tag(directory):
135+
for file in os.listdir(directory):
136+
if ".mkv" in file:
137+
title = file.replace(".mkv", "")
138+
command = r'mkvpropedit "{}\{}" --set "title={}"'.format(
139+
directory, file, title)
140+
os.system(command)
141+
142+
if ".mp4" in file:
143+
title = file.replace(".mp4", "")
144+
command = r'mkvpropedit "{}\{}" --set "title={}"'.format(
145+
directory, file, title)
146+
os.system(command)
147+
148+
print(
149+
colored(
150+
"RENAMING", "red"), colored(
151+
"VIDEO TAGS", "cyan"), colored(
152+
"FINISHED!\n", "red"))
153+
154+
155+
if __name__ == "__main__":
156+
157+
if args.code:
158+
episodes = get_episodes(args.code)
159+
160+
if args.episode:
161+
args.episode = r'{}'.format(args.episode)
162+
rename_episode(args.episode, episodes)
163+
164+
if args.subtitle:
165+
args.subtitle = r'{}'.format(args.subtitle)
166+
rename_subtitle(args.subtitle, episodes)
167+
168+
if args.tag:
169+
args.tag = r'{}'.format(args.tag)
170+
edit_tag(args.tag)

imdb_episode_rename/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IMDbPY
2+
termcolor

0 commit comments

Comments
 (0)