Converting video works, now to delete temp on cancel and originals
This commit is contained in:
parent
39b7c137a0
commit
c549f24774
@ -77,72 +77,32 @@ def main():
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
elif sys.argv[1] == "convert":
|
elif sys.argv[1] == "convert":
|
||||||
pass
|
counter = 0
|
||||||
# if "av1" in outputs:
|
nbuseful = len([filepath for filepath in medialibrary.videos if medialibrary.videos[filepath].useful])
|
||||||
# codec = "av1"
|
for filepath in medialibrary.videos:
|
||||||
# else:
|
if medialibrary.videos[filepath].useful:
|
||||||
# codec = "x265"
|
counter += 1
|
||||||
# if any("-files" in argv for argv in sys.argv):
|
# Setting required variables
|
||||||
# video = sys.argv[sys.argv.index("-files") + 1]
|
if "av1" in outputs:
|
||||||
# folder = str(video)[:str(video).rindex("/") + 1]
|
vcodec = "av1"
|
||||||
# oldfilename = str(video)[str(video).rindex("/") + 1:]
|
else:
|
||||||
|
vcodec = "x265"
|
||||||
|
|
||||||
# # Setting new filename
|
# Verbosing
|
||||||
# if "mp4" in outputs:
|
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}")
|
||||||
# newfilename = oldfilename[:-4] + ".mp4"
|
print(f"{BColors.OKGREEN}Original file:{BColors.ENDC}")
|
||||||
# if oldfilename == newfilename:
|
print(medialibrary.videos[filepath])
|
||||||
# newfilename = oldfilename[:-4] + "[HEVC]" + ".mp4"
|
print(f"{BColors.OKGREEN}Converting please wait...{BColors.ENDC}")
|
||||||
# else:
|
|
||||||
# newfilename = oldfilename[:-4] + ".mkv"
|
|
||||||
# if oldfilename == newfilename:
|
|
||||||
# newfilename = oldfilename[:-4] + "[HEVC]" + ".mkv"
|
|
||||||
|
|
||||||
|
|
||||||
|
# Converting
|
||||||
# print(f"{BColors.OKCYAN}*********** converting {oldfilename} to {newfilename} ({codec}) ***********{BColors.ENDC}")
|
if medialibrary.videos[filepath].convert():
|
||||||
# try:
|
newvid = Video(medialibrary.videos[filepath].path + medialibrary.videos[filepath].filename_new)
|
||||||
# if convert(folder + oldfilename, folder + newfilename, codec):
|
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}")
|
||||||
# #subprocess.call(['chown', f"{getuser()}:{getuser()}", folder + newfilename])
|
print(newvid)
|
||||||
# subprocess.call(['chmod', '777', folder + newfilename])
|
|
||||||
# if "-del" in sys.argv:
|
# TODO delete file when -del
|
||||||
# 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:]
|
|
||||||
|
|
||||||
# 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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
@ -8,6 +8,7 @@ import sys
|
|||||||
|
|
||||||
from .bcolors import BColors
|
from .bcolors import BColors
|
||||||
from .video import Video
|
from .video import Video
|
||||||
|
from .tools import deletefile
|
||||||
|
|
||||||
class MediaLibrary():
|
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}")
|
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
|
||||||
@ -4,6 +4,7 @@
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import os
|
||||||
from .bcolors import BColors
|
from .bcolors import BColors
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +24,7 @@ def user_confirm(question):
|
|||||||
print("Please answer with yes (Y) or no (N)...")
|
print("Please answer with yes (Y) or no (N)...")
|
||||||
return user_confirm(question)
|
return user_confirm(question)
|
||||||
|
|
||||||
def delete(filename):
|
def deletefile(filename):
|
||||||
try:
|
try:
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|||||||
@ -13,7 +13,7 @@ class Video():
|
|||||||
|
|
||||||
path = ""
|
path = ""
|
||||||
filename_origin = ""
|
filename_origin = ""
|
||||||
filesize_origin = ""
|
filesize = ""
|
||||||
filename_new = ""
|
filename_new = ""
|
||||||
filename_tmp = ""
|
filename_tmp = ""
|
||||||
useful = True
|
useful = True
|
||||||
@ -23,6 +23,59 @@ class Video():
|
|||||||
width = int()
|
width = int()
|
||||||
height = 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):
|
def __init__(self, filepath, useful = True):
|
||||||
'''
|
'''
|
||||||
'''
|
'''
|
||||||
@ -35,7 +88,7 @@ class Video():
|
|||||||
self.useful = useful
|
self.useful = useful
|
||||||
|
|
||||||
#Gathering information on the video
|
#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.error = self.detect_fferror(filepath)
|
||||||
self.codec = self.detect_codec(filepath)
|
self.codec = self.detect_codec(filepath)
|
||||||
try:
|
try:
|
||||||
@ -68,10 +121,10 @@ class Video():
|
|||||||
text += f" Codec: {self.codec}\n"
|
text += f" Codec: {self.codec}\n"
|
||||||
|
|
||||||
# Return the size in mb or gb if more than 1024 mb
|
# Return the size in mb or gb if more than 1024 mb
|
||||||
if self.filesize_origin >= 1024:
|
if self.filesize >= 1024:
|
||||||
text += f" size: {self.filesize_origin / 1024 :.2f} gb"
|
text += f" size: {self.filesize / 1024 :.2f} gb"
|
||||||
else:
|
else:
|
||||||
text += f" size: {self.filesize_origin} mb"
|
text += f" size: {self.filesize} mb"
|
||||||
|
|
||||||
if self.error:
|
if self.error:
|
||||||
text += f"{BColors.FAIL}\n Errors: {', '.join(map(str, self.error))}{BColors.ENDC}"
|
text += f"{BColors.FAIL}\n Errors: {', '.join(map(str, self.error))}{BColors.ENDC}"
|
||||||
@ -82,6 +135,12 @@ class Video():
|
|||||||
|
|
||||||
__repr__ = __str__
|
__repr__ = __str__
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_codec(filepath):
|
def detect_codec(filepath):
|
||||||
try:
|
try:
|
||||||
@ -147,37 +206,3 @@ class Video():
|
|||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
return False
|
return False
|
||||||
return size
|
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
|
|
||||||
Loading…
Reference in New Issue
Block a user