diff --git a/mediacurator/curator.py b/mediacurator/curator.py index e392af4..7c05fc3 100755 --- a/mediacurator/curator.py +++ b/mediacurator/curator.py @@ -59,9 +59,9 @@ def main(): # Loading the media library if len(files) > 0: - medialibrary = MediaLibrary(files = files) + medialibrary = MediaLibrary(files = files, inputs = inputs, filters = filters) elif len(directories) > 0: - medialibrary = MediaLibrary(directories = directories) + medialibrary = MediaLibrary(directories = directories, inputs = inputs, filters = filters) else: print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}") diff --git a/mediacurator/library/medialibrary.py b/mediacurator/library/medialibrary.py index 1e11b55..8b6f597 100644 --- a/mediacurator/library/medialibrary.py +++ b/mediacurator/library/medialibrary.py @@ -27,14 +27,23 @@ class MediaLibrary(): pass # self.files = files elif directories: - self.directories = directories + self.directories = directories else: return - self.inputs = inputs - self.filters = filters + self.inputs = inputs + 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))}" 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}") videolist = [] @@ -73,62 +86,49 @@ class MediaLibrary(): videolist_tmp = videolist videolist = [video for video in videolist_tmp if video.is_file()] - video = Video(videolist[0]) - - - - - - #videolist = list(dict.fromkeys(videolist)) - print(video) - exit() - - + # 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))) + #print(f'{iteration} / {len(videolist)}% complete {int(iteration / len(videolist))}% complete', end='\r') + print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r') + self.videos[video] = Video(video) + def filter_videos(self): + ''' + Mark useless videos in the videos dictionary + ''' - # # Filter the list for specific codecs - # 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 = [] + print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}") - # if "old" in self.filters: - # videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]] + iteration = 0 + 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: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]] + # Filter for codecs if codec filter passed by user + 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: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]] + if ("mpeg4" in self.filters or "mpeg" in self.filters) and self.videos[filepath].codec in ["mpeg4", "msmpeg4v3"]: + useful = True - # if "wmv3" in self.filters or "wmv" in self.filters: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]] + if "mpeg" in self.filters and self.videos[filepath].codec in ["mpeg1video"]: + 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)) diff --git a/mediacurator/library/video.py b/mediacurator/library/video.py index 5b21bdb..ca7961d 100644 --- a/mediacurator/library/video.py +++ b/mediacurator/library/video.py @@ -15,19 +15,23 @@ class Video(): filesize_origin = "" filename_new = "" filename_tmp = "" + useful = True codec= "" error = "" definition = "" width = int() height = int() - def __init__(self, filepath): + def __init__(self, filepath, useful = True): ''' ''' #Breaking down the full path in its components - self.path = str(filepath)[:str(filepath).rindex("/") + 1] - self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:] + self.path = 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 self.filesize_origin = self.detect_filesize(filepath) @@ -39,15 +43,35 @@ class Video(): height = self.height ) def __str__(self): + ''' + Building and returning formated information about the video file + ''' + 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" 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: text += f"\n Errors: {self.error}" + + text += f"\n Useful: {self.useful}" + return text + __repr__ = __str__ @staticmethod def detect_codec(filepath): @@ -102,12 +126,12 @@ class Video(): return False if width >= 2160 or height >= 2160: - return "UHD" + return "uhd" elif width >= 1440 or height >= 1080: return "1080p" elif width >= 1280 or height >= 720: return "720p" - return "SD" + return "sd" @staticmethod def detect_filesize(filepath):