-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_renamer.py
126 lines (96 loc) · 4.08 KB
/
video_renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# video_renamer
# keytonic
# 18 Aug 24
#
# attempts to itterate current directory and renames all videos correctly
# from:
# Ant-Man.And.The.Wasp.Quantumania.2023.720p.BluRay.x264.AAC-[YTS.MX].mp4
# The.Iron.Claw.2023.720p.BluRay.x264.AAC-[YTS.MX].mp4
# to:
# Ant-Man And The Wasp Quantumania (2023).mp4
# Iron Claw, The (2023).mp4
#
# Will also take one file name via command line
# eg: python video_renamer.py Ant-Man.And.The.Wasp.Quantumania.2023.720p.BluRay.x264.AAC-[YTS.MX].mp4
import sys
import os
#import webbrowser
import requests
from imdb import Cinemagoer
ia = Cinemagoer()
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def get_poster(name,year):
filename = "{} ({}).{}".format(name,year,'jpg')
try:
movies = ia.search_movie("{} {}".format(name,year))
movie = ia.get_movie(movies[0].movieID)
img_data = ""
if(movie.has_key('cover url') == True and movie['cover url'] != ""):
img_data = requests.get(movie['cover url']).content
if(img_data != ""):
with open(filename, 'wb') as handler:
handler.write(img_data)
print(bcolors.OKGREEN + "[Info]" + bcolors.ENDC + " Creating '" + bcolors.OKCYAN + filename + bcolors.ENDC + "'")
else:
print(bcolors.FAIL + "[Fail]" + bcolors.ENDC + " could not save: '" + filename + "'")
except:
print(bcolors.FAIL + "[Fail]" + bcolors.ENDC + " could not save: '" + filename + "'")
#input("Press Enter to continue...")
def rename(name):
#looking at extention first because if it aint a movie
#then there aint no point in doing anything else
ext = (name[name.rfind('.') + 1:]).strip()
if(ext != "mp4" and ext != "mkv"):
#return quietly if its a script or folder
#if(ext != "py" and name.rfind('.') != -1):
if(os.path.isdir(name) == False and ext != "py"):
#return loudly if it is a file with a no shit wrong extention
print(bcolors.FAIL + "[Fail]" + bcolors.ENDC + " extention: '{}' name: '{}'".format(ext,name))
return
#searching for year, looking for first occurance of year_prefix
#hoping year is something like 20xx or 19xx what ever year_prefix is set to
year_prefix = "20"
year = (name[name.find(year_prefix):name.find(year_prefix) + 4]).strip()
if((year.isnumeric() == False) or (year == "")):
year_prefix = "19"
year = (name[name.find(year_prefix):name.find(year_prefix) + 4]).strip()
if((year.isnumeric() == False) or (year == "")):
print(bcolors.FAIL + "[Fail]" + bcolors.ENDC + " year: '{}' name: '{}'".format(year,name))
#sys.exit()
return
#getting the title, doing alot of .srip'n just making sure theres no spaces
#also checking for 'the' in the title. if its in the begining the rename accordingly
title = (name[0:name.find(year_prefix)].replace('.', ' ')).strip()
#removing '(' and ')' incase this file has been processed previously
title = title.replace('(', '').strip()
title = title.replace(')', '').strip()
#need to fix this... triggers on 'they' etc
if((title.find("The ") != -1) and (title.find("The ") == 0)):
title = "{}, The".format(title[4:])
#putting it all together
new_name = "{} ({}).{}".format(title,year,ext)
#let's go!
print(bcolors.OKGREEN + "[Info]" + bcolors.ENDC + " Renaming '" + bcolors.OKCYAN + new_name + bcolors.ENDC + "'")
os.rename(name,new_name)
get_poster(title,year)
#if filename given on commandline then go with it
if(len(sys.argv) > 1):
rename(sys.argv[1])
#otherwise loop through files in directory and process them
else:
#get the list of all files and directories in the current working directory
file_list = os.listdir()
#looping throught them all
for file in file_list:
#ignore this script file name
if(file != os.path.basename(__file__)):
rename(file)