diff --git a/mediacurator/curator.py b/mediacurator/curator.py index 833570f..e392af4 100755 --- a/mediacurator/curator.py +++ b/mediacurator/curator.py @@ -65,7 +65,6 @@ def main(): else: print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}") - print(medialibrary) # Actions if sys.argv[1] == "list": @@ -87,7 +86,8 @@ def main(): if any("-files" in argv for argv in sys.argv): pass elif any("-dir" in argv for argv in sys.argv): - print(f"directories = {directories}, inputs = {inputs}, filters = {filters}, outputs = {outputs}") + print(medialibrary) + #print(f"directories = {directories}, inputs = {inputs}, filters = {filters}, outputs = {outputs}") exit() else: print("{BColors.FAIL}Missing directory: {BColors.ENDC}") diff --git a/mediacurator/library/medialibrary.py b/mediacurator/library/medialibrary.py index bb95d21..1e11b55 100644 --- a/mediacurator/library/medialibrary.py +++ b/mediacurator/library/medialibrary.py @@ -3,7 +3,10 @@ This is the container for all the videos found in the folders passed by the user ''' +from pathlib import Path + from .bcolors import BColors +from .video import Video class MediaLibrary(): ''' @@ -13,103 +16,119 @@ class MediaLibrary(): size = int() videos = dict() directories = list() + inputs = [] + filters = [] - def __init__(self, files = False, directories = False): + def __init__(self, files = False, directories = False, inputs = ["any"], filters = []): ''' + This is the library object who holds the information about the workspace and all the videos in it. ''' if files: pass # self.files = files elif directories: self.directories = directories + else: + return + + self.inputs = inputs + self.filters = filters + + self.load_videolist() + def __str__(self): ''' print ''' + text = f"MediaCurator watching: " if self.directories: - return f"MediaCurator watching: {', '.join(map(str, self.directories))}" - - # def delete(filename): - # try: - # os.remove(filename) - # except OSError: - # print(f"{BColors.FAIL}Error deleting {filename}{BColors.ENDC}") - # return False - - # print(f"{BColors.OKGREEN}Deleted {filename}{BColors.ENDC}") - # return True + text += f"{', '.join(map(str, self.directories))}" + return text - # def get_videolist(parentdir, inputs = ["any"], filters = []): - # print(f"{BColors.OKGREEN}Scanning files in {parentdir} for videos{BColors.ENDC}") - # videolist = [] - - # path = Path(parentdir) - # if "wmv" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[wW][mM][vV]")) - # if "avi" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[aA][vV][iI]")) - # if "mkv" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[mM][kK][vV]")) - # if "mp4" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[mM][pP]4")) - # if "m4v" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[mM]4[vV]")) - # if "flv" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[fF][lL][vV]")) - # if "mpg" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[mM][pP][gG]")) - # if "vid" in inputs or "any" in inputs or len(inputs) < 1: - # videolist += list(path.rglob("*.[vV][iI][dD]")) + def load_videolist(self): + print(f"{BColors.OKGREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{BColors.ENDC}") + videolist = [] + for directory in self.directories: + path = Path(directory) + if "wmv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[wW][mM][vV]")) + if "avi" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[aA][vV][iI]")) + if "mkv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[mM][kK][vV]")) + if "mp4" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[mM][pP]4")) + if "m4v" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[mM]4[vV]")) + if "flv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[fF][lL][vV]")) + if "mpg" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[mM][pP][gG]")) + if "vid" in self.inputs or "any" in self.inputs or len(self.inputs) < 1: + videolist += list(path.rglob("*.[vV][iI][dD]")) - # # Remove folders - # videolist_tmp = videolist - # videolist = [video for video in videolist_tmp if video.is_file()] + # Remove folders + 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() + + + - # # 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 filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0: - # videolist = [] + # # 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 = [] - # if "old" in filters: - # videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]] + # if "old" in self.filters: + # videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]] - # if "mpeg4" in filters or "mpeg" in filters: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]] + # if "mpeg4" in self.filters or "mpeg" in self.filters: + # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]] - # if "mpeg" in filters: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]] + # if "mpeg" in self.filters: + # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]] - # if "wmv3" in filters or "wmv" in filters: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]] + # if "wmv3" in self.filters or "wmv" in self.filters: + # videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]] - # if "x264" in filters: - # videolist += [video for video in videolist_tmp if get_codec(video) in ["x264"]] + # if "x264" in self.filters: + # videolist += [video for video in videolist_tmp if get_codec(video) in ["x264"]] - # if len(filters) > 0 and "lowres" in 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(filters) > 0 and "hd" in 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(filters) > 0 and "720p" in 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(filters) > 0 and "1080p" in 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(filters) > 0 and "uhd" in 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 "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(filters) > 0 and "fferror" in filters: - # videolist_tmp = videolist - # videolist = [video for video in videolist_tmp if get_fferror(video)] + # 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}") + # print(f"{BColors.OKGREEN}Found {len(videolist)} videos for the requested parameters{BColors.ENDC}") - # # remove doubles and return - # return list(dict.fromkeys(videolist)) + # # remove doubles and return + # return list(dict.fromkeys(videolist)) diff --git a/mediacurator/library/tools.py b/mediacurator/library/tools.py index 9fef973..e11bb06 100644 --- a/mediacurator/library/tools.py +++ b/mediacurator/library/tools.py @@ -4,6 +4,7 @@ ''' import subprocess +from .bcolors import BColors def detect_ffmpeg(): @@ -20,4 +21,14 @@ def user_confirm(question): elif answer.lower() in ["n","no"]: return False print("Please answer with yes (Y) or no (N)...") - return user_confirm(question) \ No newline at end of file + return user_confirm(question) + +def delete(filename): + try: + os.remove(filename) + except OSError: + print(f"{BColors.FAIL}Error deleting {filename}{BColors.ENDC}") + return False + + print(f"{BColors.OKGREEN}Deleted {filename}{BColors.ENDC}") + return True \ No newline at end of file diff --git a/mediacurator/library/video.py b/mediacurator/library/video.py index acb9df2..5b21bdb 100644 --- a/mediacurator/library/video.py +++ b/mediacurator/library/video.py @@ -38,8 +38,19 @@ class Video(): width = self.width, height = self.height ) - @classmethod - def detect_codec(Video, filepath): + def __str__(self): + text = f"{self.path + self.filename_origin}\n" + text += f" Definition: {self.definition}: ({self.width}x{self.height})\n" + text += f" Codec: {self.codec}\n" + text += f" size: {self.filesize_origin}mb" + if self.error: + text += f"\n Errors: {self.error}" + return text + + + + @staticmethod + def detect_codec(filepath): try: args = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=codec_name", "-of", "default=noprint_wrappers=1:nokey=1", str(filepath)] output = subprocess.check_output(args, stderr=subprocess.STDOUT) @@ -53,8 +64,8 @@ class Video(): return output - @classmethod - def detect_fferror(Video, filepath): + @staticmethod + def detect_fferror(filepath): try: args = ["ffprobe","-v","error","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)] output = subprocess.check_output(args, stderr=subprocess.STDOUT) @@ -66,8 +77,8 @@ class Video(): return False - @classmethod - def detect_resolution(Video, filepath): + @staticmethod + def detect_resolution(filepath): try: args = ["ffprobe","-v","error","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)] output = subprocess.check_output(args, stderr=subprocess.STDOUT) @@ -83,8 +94,8 @@ class Video(): return False return output[0], output[1] - @classmethod - def detect_definition(Video, filepath = False, width = False, height = False): + @staticmethod + def detect_definition(filepath = False, width = False, height = False): if filepath: width, height = Video.detect_resolution(filepath) if not width and not height: @@ -98,8 +109,8 @@ class Video(): return "720p" return "SD" - @classmethod - def detect_filesize(Video, filepath): + @staticmethod + def detect_filesize(filepath): try: size = int(os.path.getsize(filepath) / 1024 / 1024) except subprocess.CalledProcessError: @@ -107,7 +118,7 @@ class Video(): return False return size - # @classmethod + # @staticmethod # def convert(oldfilename, newfilename, codec = "x265"): # oldsize = get_size(oldfilename) # print(f"{BColors.OKGREEN}Starting conversion of {oldfilename}{BColors.OKCYAN}({oldsize}mb)({get_print_resolution(oldfilename)}){BColors.OKGREEN} from {BColors.OKCYAN}{get_codec(oldfilename)}{BColors.OKGREEN} to {BColors.OKCYAN}{codec}{BColors.OKGREEN}...{BColors.ENDC}")