From c549f247740286f165ebe38b5a6770a2c4497de2 Mon Sep 17 00:00:00 2001 From: Fabrice Quenneville Date: Wed, 18 Nov 2020 20:07:01 -0500 Subject: [PATCH] Converting video works, now to delete temp on cancel and originals --- mediacurator/curator.py | 86 ++++++---------------- mediacurator/library/medialibrary.py | 14 ++++ mediacurator/library/tools.py | 3 +- mediacurator/library/video.py | 103 +++++++++++++++++---------- 4 files changed, 103 insertions(+), 103 deletions(-) diff --git a/mediacurator/curator.py b/mediacurator/curator.py index 868109c..6fdebb5 100755 --- a/mediacurator/curator.py +++ b/mediacurator/curator.py @@ -77,72 +77,32 @@ def main(): exit() elif sys.argv[1] == "convert": - pass - # if "av1" in outputs: - # codec = "av1" - # else: - # codec = "x265" - # if any("-files" in argv for argv in sys.argv): - # video = sys.argv[sys.argv.index("-files") + 1] - # folder = str(video)[:str(video).rindex("/") + 1] - # oldfilename = str(video)[str(video).rindex("/") + 1:] + counter = 0 + nbuseful = len([filepath for filepath in medialibrary.videos if medialibrary.videos[filepath].useful]) + for filepath in medialibrary.videos: + if medialibrary.videos[filepath].useful: + counter += 1 + # Setting required variables + if "av1" in outputs: + vcodec = "av1" + else: + vcodec = "x265" - # # Setting new filename - # if "mp4" in outputs: - # newfilename = oldfilename[:-4] + ".mp4" - # if oldfilename == newfilename: - # newfilename = oldfilename[:-4] + "[HEVC]" + ".mp4" - # else: - # newfilename = oldfilename[:-4] + ".mkv" - # if oldfilename == newfilename: - # newfilename = oldfilename[:-4] + "[HEVC]" + ".mkv" - + # Verbosing + print(f"{BColors.OKCYAN}****** Starting conversion {counter} of {nbuseful}: '{BColors.OKGREEN}{medialibrary.videos[filepath].filename_origin}{BColors.OKCYAN}' from {BColors.OKGREEN}{medialibrary.videos[filepath].codec}{BColors.OKCYAN} to {BColors.OKGREEN}{vcodec}{BColors.OKCYAN}...{BColors.ENDC}") + print(f"{BColors.OKGREEN}Original file:{BColors.ENDC}") + print(medialibrary.videos[filepath]) + print(f"{BColors.OKGREEN}Converting please wait...{BColors.ENDC}") - - # print(f"{BColors.OKCYAN}*********** converting {oldfilename} to {newfilename} ({codec}) ***********{BColors.ENDC}") - # try: - # if convert(folder + oldfilename, folder + newfilename, codec): - # #subprocess.call(['chown', f"{getuser()}:{getuser()}", folder + newfilename]) - # subprocess.call(['chmod', '777', folder + newfilename]) - # if "-del" in sys.argv: - # delete(folder + oldfilename) - # else: - # delete(folder + newfilename) - # return False - # except: - # delete(folder + newfilename) - # return False - # elif any("-dir" in argv for argv in sys.argv): - # videolist = [] - # for directory in directories: - # videolist += get_videolist(directory, inputs, filters) - # videolist.sort() - # counter = 0 - # for video in videolist: - # folder = str(video)[:str(video).rindex("/") + 1] - # oldfilename = str(video)[str(video).rindex("/") + 1:] + # Converting + if medialibrary.videos[filepath].convert(): + newvid = Video(medialibrary.videos[filepath].path + medialibrary.videos[filepath].filename_new) + print(f"{BColors.OKGREEN}Converted {medialibrary.videos[filepath].filename_origin}{BColors.OKCYAN}({medialibrary.videos[filepath].filesize}mb){BColors.OKGREEN} to {newvid.filename_origin}{BColors.OKCYAN}({newvid.filesize}mb){BColors.OKGREEN} successfully, new file:{BColors.ENDC}") + print(newvid) + + # TODO delete file when -del + - # if "mp4" in outputs: - # newfilename = oldfilename[:-4] + ".mp4" - # if oldfilename == newfilename: - # newfilename = oldfilename[:-4] + "[HEVC]" + ".mp4" - # else: - # newfilename = oldfilename[:-4] + ".mkv" - # if oldfilename == newfilename: - # newfilename = oldfilename[:-4] + "[HEVC]" + ".mkv" - # counter += 1 - # print(f"{BColors.OKCYAN}*********** convert {counter} of {len(videolist)} ***********{BColors.ENDC}") - # try: - # if convert(folder + oldfilename, folder + newfilename, codec): - # #output = (['chown', f"{getuser()}:{getuser()}", folder + newfilename], stderr=subprocess.STDOUT) - # #output = (['chown', f"{getuser()}:{getuser()}", folder + newfilename], stderr=subprocess.STDOUT) - # subprocess.call(['chmod', '777', folder + newfilename]) - # if "-del" in sys.argv: - # delete(folder + oldfilename) - # except: - # delete(folder + newfilename) - # return False - if __name__ == '__main__': main() \ No newline at end of file diff --git a/mediacurator/library/medialibrary.py b/mediacurator/library/medialibrary.py index c292fbb..c5486f2 100644 --- a/mediacurator/library/medialibrary.py +++ b/mediacurator/library/medialibrary.py @@ -8,6 +8,7 @@ import sys from .bcolors import BColors from .video import Video +from .tools import deletefile class MediaLibrary(): ''' @@ -167,3 +168,16 @@ class MediaLibrary(): print(f"{BColors.OKGREEN}Found {len([filepath for filepath in self.videos if self.videos[filepath].useful])} videos for the requested parameters{BColors.ENDC}") + + def unwatch(self, filepath, delete = False): + ''' remove a video from the index and delete it if requested''' + # If the user wanted to delete the film and + if delete: + deletefile(filepath) + try: + video = self.videos.pop(filepath) + if video: + return video + except KeyError: + pass + return False \ No newline at end of file diff --git a/mediacurator/library/tools.py b/mediacurator/library/tools.py index e11bb06..087fe6c 100644 --- a/mediacurator/library/tools.py +++ b/mediacurator/library/tools.py @@ -4,6 +4,7 @@ ''' import subprocess +import os from .bcolors import BColors @@ -23,7 +24,7 @@ def user_confirm(question): print("Please answer with yes (Y) or no (N)...") return user_confirm(question) -def delete(filename): +def deletefile(filename): try: os.remove(filename) except OSError: diff --git a/mediacurator/library/video.py b/mediacurator/library/video.py index 09d8c52..b6d0ddb 100644 --- a/mediacurator/library/video.py +++ b/mediacurator/library/video.py @@ -13,7 +13,7 @@ class Video(): path = "" filename_origin = "" - filesize_origin = "" + filesize = "" filename_new = "" filename_tmp = "" useful = True @@ -23,6 +23,59 @@ class Video(): width = int() height = int() + def convert(self, vcodec = "x265", acodec = False, extension = "mkv"): + ''' + Convert to original file to the requested format / codec + ''' + + + # Setting new filename + if "mp4" in extension: + newfilename = self.filename_origin[:-4] + ".mp4" + if self.filename_origin == newfilename: + newfilename = self.filename_origin[:-4] + "[HEVC]" + ".mp4" + else: + newfilename = self.filename_origin[:-4] + ".mkv" + if self.filename_origin == newfilename: + newfilename = self.filename_origin[:-4] + "[HEVC]" + ".mkv" + self.filename_tmp = newfilename + + + + # Settting ffmpeg + args = ['ffmpeg', '-i', self.path + self.filename_origin] + + # conversion options + if vcodec == "av1": + args += ['-c:v', 'libaom-av1', '-strict', 'experimental'] + elif vcodec == "x265" or vcodec == "hevc": + args += ['-c:v', 'libx265'] + args += ['-max_muxing_queue_size', '1000'] + # conversion output + args += [self.path + self.filename_tmp] + + + + try: + if "-verbose" in sys.argv: + subprocess.call(args) + else: + txt = subprocess.check_output(args, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + self.filename_tmp = "" + print(f"{BColors.FAIL}Conversion failed {e}{BColors.ENDC}") + return False + else: + self.filename_new = self.filename_tmp + self.filename_tmp = "" + + #newsize = get_size(newfilename) + #oldfilename = str(oldfilename)[str(oldfilename).rindex("/") + 1:] + #newfilename = str(newfilename)[str(newfilename).rindex("/") + 1:] + #print(f"{BColors.OKGREEN}Converted {oldfilename}{BColors.OKCYAN}({oldsize}mb){BColors.OKGREEN} to {newfilename}{BColors.OKCYAN}({newsize}mb){BColors.OKGREEN} successfully{BColors.ENDC}") + return True + + def __init__(self, filepath, useful = True): ''' ''' @@ -35,7 +88,7 @@ class Video(): self.useful = useful #Gathering information on the video - self.filesize_origin = self.detect_filesize(filepath) + self.filesize = self.detect_filesize(filepath) self.error = self.detect_fferror(filepath) self.codec = self.detect_codec(filepath) try: @@ -68,10 +121,10 @@ class Video(): text += f" Codec: {self.codec}\n" # 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" + if self.filesize >= 1024: + text += f" size: {self.filesize / 1024 :.2f} gb" else: - text += f" size: {self.filesize_origin} mb" + text += f" size: {self.filesize} mb" if self.error: text += f"{BColors.FAIL}\n Errors: {', '.join(map(str, self.error))}{BColors.ENDC}" @@ -82,6 +135,12 @@ class Video(): __repr__ = __str__ + + + + + + @staticmethod def detect_codec(filepath): try: @@ -147,37 +206,3 @@ class Video(): except subprocess.CalledProcessError: return False return size - - # @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}") - - # # Preparing ffmpeg command and input file - # args = ['ffmpeg', '-i', oldfilename] - - # # conversion options - # if codec == "av1": - # args += ['-c:v', 'libaom-av1', '-strict', 'experimental'] - # else: - # args += ['-c:v', 'libx265'] - # args += ['-max_muxing_queue_size', '1000'] - - # # conversion output - # args += [newfilename] - - # #args = ['ffmpeg', '-i', oldfilename, newfilename] - # try: - # if "-verbose" in sys.argv: - # subprocess.call(args) - # else: - # txt = subprocess.check_output(args, stderr=subprocess.STDOUT) - # except subprocess.CalledProcessError as e: - # print(f"{BColors.FAIL}Conversion failed {e}{BColors.ENDC}") - # return False - # else: - # newsize = get_size(newfilename) - # oldfilename = str(oldfilename)[str(oldfilename).rindex("/") + 1:] - # newfilename = str(newfilename)[str(newfilename).rindex("/") + 1:] - # print(f"{BColors.OKGREEN}Converted {oldfilename}{BColors.OKCYAN}({oldsize}mb){BColors.OKGREEN} to {newfilename}{BColors.OKCYAN}({newsize}mb){BColors.OKGREEN} successfully{BColors.ENDC}") - # return True \ No newline at end of file