Working on the filters in medialibrary and verbosity

This commit is contained in:
Fabrice Quenneville 2020-11-18 02:06:42 -05:00
parent 29f50451f0
commit 2d90e0d6e8
3 changed files with 86 additions and 62 deletions

View File

@ -59,9 +59,9 @@ def main():
# Loading the media library # Loading the media library
if len(files) > 0: if len(files) > 0:
medialibrary = MediaLibrary(files = files) medialibrary = MediaLibrary(files = files, inputs = inputs, filters = filters)
elif len(directories) > 0: elif len(directories) > 0:
medialibrary = MediaLibrary(directories = directories) medialibrary = MediaLibrary(directories = directories, inputs = inputs, filters = filters)
else: else:
print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}") print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}")

View File

@ -27,14 +27,23 @@ class MediaLibrary():
pass pass
# self.files = files # self.files = files
elif directories: elif directories:
self.directories = directories self.directories = directories
else: else:
return return
self.inputs = inputs self.inputs = inputs
self.filters = filters self.filters = filters
self.load_videolist() self.load_videos()
self.filter_videos()
for filepath in self.videos:
# if self.videos[filepath].useful:
# print(self.videos[filepath])
print(self.videos[filepath])
@ -45,8 +54,12 @@ class MediaLibrary():
text += f"{', '.join(map(str, self.directories))}" text += f"{', '.join(map(str, self.directories))}"
return text return text
def load_videos(self):
'''
Scan folders for video files respecting the inputs requested by the user
Save them to the videos dictionary
'''
def load_videolist(self):
print(f"{BColors.OKGREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{BColors.ENDC}") print(f"{BColors.OKGREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{BColors.ENDC}")
videolist = [] videolist = []
@ -73,62 +86,49 @@ class MediaLibrary():
videolist_tmp = videolist videolist_tmp = videolist
videolist = [video for video in videolist_tmp if video.is_file()] videolist = [video for video in videolist_tmp if video.is_file()]
video = Video(videolist[0]) # Map it all to the videos dictionary as initiated Video objects
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}")
iteration = 0
for video in videolist:
iteration += 1
#print(int(iteration / len(videolist)))
#videolist = list(dict.fromkeys(videolist)) #print(f'{iteration} / {len(videolist)}% complete {int(iteration / len(videolist))}% complete', end='\r')
print(video) print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
exit() self.videos[video] = Video(video)
def filter_videos(self):
'''
Mark useless videos in the videos dictionary
'''
# # Filter the list for specific codecs print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}")
# videolist_tmp = videolist
# print(f"{BColors.OKGREEN}Filtering {len(videolist)} videos for the requested parameters{BColors.ENDC}")
# if len([filt for filt in self.filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0:
# videolist = []
# if "old" in self.filters: iteration = 0
# videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]] for filepath in self.videos:
iteration += 1
print(f'{int((iteration / len(self.videos)* 100))}% complete', end='\r')
# if "mpeg4" in self.filters or "mpeg" in self.filters: # Filter for codecs if codec filter passed by user
# videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]] if len([filt for filt in self.filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0:
useful = False
if "old" in self.filters and self.videos[filepath].codec not in ["hevc", "av1"]:
useful = True
# if "mpeg" in self.filters: if ("mpeg4" in self.filters or "mpeg" in self.filters) and self.videos[filepath].codec in ["mpeg4", "msmpeg4v3"]:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]] useful = True
# if "wmv3" in self.filters or "wmv" in self.filters: if "mpeg" in self.filters and self.videos[filepath].codec in ["mpeg1video"]:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]] useful = True
if ("wmv3" in self.filters or "wmv" in self.filters) and self.videos[filepath].codec in ["wmv3"]:
useful = True
if "x264" in self.filters and self.videos[filepath].codec in ["x264"]:
useful = True
self.videos[filepath].useful = useful
# if "x264" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["x264"]]
# if len(self.filters) > 0 and "lowres" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] < 1280 or get_resolution(video)[0] <= 480]
# elif len(self.filters) > 0 and "hd" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] >= 720]
# elif len(self.filters) > 0 and "720p" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] == 720]
# elif len(self.filters) > 0 and "1080p" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if (get_resolution(video)[1] >= 1440 and get_resolution(video)[1] < 3840) or get_resolution(video)[0] == 1080]
# elif len(self.filters) > 0 and "uhd" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 3840 or get_resolution(video)[0] >= 2160]
# if len(self.filters) > 0 and "fferror" in self.filters:
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_fferror(video)]
# print(f"{BColors.OKGREEN}Found {len(videolist)} videos for the requested parameters{BColors.ENDC}")
# # remove doubles and return
# return list(dict.fromkeys(videolist))

View File

@ -15,19 +15,23 @@ class Video():
filesize_origin = "" filesize_origin = ""
filename_new = "" filename_new = ""
filename_tmp = "" filename_tmp = ""
useful = True
codec= "" codec= ""
error = "" error = ""
definition = "" definition = ""
width = int() width = int()
height = int() height = int()
def __init__(self, filepath): def __init__(self, filepath, useful = True):
''' '''
''' '''
#Breaking down the full path in its components #Breaking down the full path in its components
self.path = str(filepath)[:str(filepath).rindex("/") + 1] self.path = str(filepath)[:str(filepath).rindex("/") + 1]
self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:] self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:]
# Marking useful is user manually set it.
self.useful = useful
#Gathering information on the video #Gathering information on the video
self.filesize_origin = self.detect_filesize(filepath) self.filesize_origin = self.detect_filesize(filepath)
@ -39,15 +43,35 @@ class Video():
height = self.height ) height = self.height )
def __str__(self): def __str__(self):
'''
Building and returning formated information about the video file
'''
text = f"{self.path + self.filename_origin}\n" text = f"{self.path + self.filename_origin}\n"
text += f" Definition: {self.definition}: ({self.width}x{self.height})\n"
# If the first character of the definition is not a number (ie UHD and not 720p) upper it
if self.definition[0] and not self.definition[0].isnumeric():
text += f" Definition: {self.definition.upper()}: ({self.width}x{self.height})\n"
else:
text += f" Definition: {self.definition}: ({self.width}x{self.height})\n"
text += f" Codec: {self.codec}\n" text += f" Codec: {self.codec}\n"
text += f" size: {self.filesize_origin}mb"
# Return the size in mb or gb if more than 1024 mb
if self.filesize_origin >= 1024:
text += f" size: {self.filesize_origin / 1024 :.2f} gb"
else:
text += f" size: {self.filesize_origin} mb"
if self.error: if self.error:
text += f"\n Errors: {self.error}" text += f"\n Errors: {self.error}"
text += f"\n Useful: {self.useful}"
return text return text
__repr__ = __str__
@staticmethod @staticmethod
def detect_codec(filepath): def detect_codec(filepath):
@ -102,12 +126,12 @@ class Video():
return False return False
if width >= 2160 or height >= 2160: if width >= 2160 or height >= 2160:
return "UHD" return "uhd"
elif width >= 1440 or height >= 1080: elif width >= 1440 or height >= 1080:
return "1080p" return "1080p"
elif width >= 1280 or height >= 720: elif width >= 1280 or height >= 720:
return "720p" return "720p"
return "SD" return "sd"
@staticmethod @staticmethod
def detect_filesize(filepath): def detect_filesize(filepath):