optimised filechooser and fixed multiple bugs

This commit is contained in:
janis
2022-09-12 07:08:33 +02:00
parent 5ca513383e
commit 64f4a25c4f
3 changed files with 66 additions and 26 deletions

View File

@@ -18,24 +18,39 @@ class Checks:
try:
self.custom_quality = float(custom_q)
except ValueError:
if str(custom_q)[len(custom_q) - 1:] == "x":
print("x found")
try:
self.custom_quality = float(str(custom_q)[:len(custom_q) - 1])
except ValueError:
print("invalid custom quality multiplier")
return False
else:
print("invalid letter in custom quality multiplier!")
return False
if 4 >= self.custom_quality >= 1:
print("quality selction ok")
return True
else:
print("invalid range for multiplier!")
def file_checks(self, i_fp, o_fp):
self.i_file_extension = str(i_fp)[len(i_fp) - 4:]
if self.i_file_extension == ".png" or self.i_file_extension == ".jpg":
pass
print("file extensions ok (image)")
elif self.i_file_extension == "jpeg":
if str(i_fp)[len(i_fp) - 5:] == ".jpeg":
pass
print("file extensions ok (image)")
else:
return False
elif self.i_file_extension == ".mp4" or self.i_file_extension == ".mkv":
pass
print("file extensions ok (video)")
else:
return False
if str(i_fp)[len(i_fp) - 4:] == str(o_fp)[len(o_fp) - 4:]:
print("file extensions are the same in output and input")
return True
else:
print("files don't have same extension!")
return False

View File

@@ -16,7 +16,7 @@ import time
# Loading the config file to get user preferred temp path
config = configparser.ConfigParser()
config.read('../config/settings.ini')
config.read('./config/settings.ini')
class Handler:
@@ -29,12 +29,14 @@ class Handler:
def handler(self, fsrpath, filepath, quality_mode, quality_setting, output_path, ffmpegpath):
# Function to be called when using this class as this function automatically determines if file is video or image
if self.os_type == "linux":
self.tmppath = "/tmp/fsru/" # config["PathSettings"]["tmpPathLinux"]
self.tmppath = config["PathSettings"]["tmpPathLinux"]
elif self.os_type == "win32":
self.tmppath = config["PathSettings"]["tmpPathWindows"]
else:
print("OS CURRENTLY UNSUPPORTED!")
return False
self.tmppath += "fsru/"
print(self.tmppath)
# checking for spaces in filepath (for use with terminal commands)
self.filepath = ""
for self.letter in filepath:
@@ -83,9 +85,14 @@ class Handler:
# Retrieving Video metadata
self.duration = self.videometa.get("duration")
self.frames = self.videometa.get("nb_frames")
try:
self.framerate = round(float(self.frames) / float(self.duration), 1)
except TypeError:
self.infos = str(self.videometa.get("r_frame_rate"))
self.framerate = float(self.infos[:len(self.infos) - 2])
# Splitting video into frames
os.remove(self.tmppath)
try:
os.mkdir(self.tmppath)
except FileExistsError:
@@ -93,9 +100,9 @@ class Handler:
if self.os_type == "linux":
print("linux")
self.command = f"ffmpeg -i {str(self.filepath)} {self.tmppath}thumb%04d.jpg -hide_banner"
self.command = f"ffmpeg -i {str(self.filepath)} {self.tmppath}thumb%08d.jpg"
elif self.os_type == "win32":
self.command = f"{ffmpegpath} -i {str(self.filepath)} {self.tmppath}thumb%04d.jpg -hide_banner"
self.command = f"{ffmpegpath} -i {str(self.filepath)} {self.tmppath}thumb%08d.jpg"
else:
print("OS CURRENTLY UNSUPPORTED!")
return False
@@ -110,13 +117,12 @@ class Handler:
self.number = 0
for self.file in self.filelist:
self.number += 1
self.files += f"{self.tmppath}{self.file} {self.tmppath}upscaled/USImage{str(self.number).zfill(4)}.jpg "
self.files += f"{self.tmppath}{self.file} {self.tmppath}upscaled/USImage{str(self.number).zfill(8)}.jpg "
self.maxlength = 32000
self.pos = 1
# Refactoring of commands that are longer than 32K characters
if len(self.files) > self.maxlength:
print("shrinking command length")
self.fileout = []
while self.files[self.maxlength - self.pos:self.maxlength - self.pos + 1] != " ":
@@ -157,7 +163,6 @@ class Handler:
self.fileout.append(self.files[self.maxlength - self.pos:])
else:
self.fileout.append(self.files)
print("filepath assembled")
try:
os.mkdir(f"{self.tmppath}upscaled/")
@@ -185,7 +190,6 @@ class Handler:
else:
print("OS CURRENTLY UNSUPPORTED!")
return False
print(self.command, "\n\n\nCOMMAND to EXECUTE\n\n\n")
os.system(self.command)
print("Finished upscaling this section.")
time.sleep(3)
@@ -209,9 +213,9 @@ class Handler:
# reassemble Video
print("Reassembling Video... with framerate @", self.framerate)
if self.os_type == "linux":
self.command = f"ffmpeg -framerate {self.framerate} -i {self.tmppath}upscaled/USImage%04d.jpg {output_path} -i {self.tmppath}audio.aac"
self.command = f"ffmpeg -framerate {self.framerate} -i {self.tmppath}upscaled/USImage%08d.jpg {output_path} -i {self.tmppath}audio.aac"
elif self.os_type == "win32":
self.command = f"{ffmpegpath} -framerate {self.framerate} -i {self.tmppath}upscaled/USImage%04d.jpg {output_path} -i {self.tmppath}audio.aac"
self.command = f"{ffmpegpath} -framerate {self.framerate} -i {self.tmppath}upscaled/USImage%08d.jpg {output_path} -i {self.tmppath}audio.aac"
else:
print("OS CURRENTLY UNSUPPORTED!")
return False

View File

@@ -6,7 +6,7 @@
# Developed 2022 by Janis Hutz
#
###########################################################
import sys
import gi
import bin.handler
import multiprocessing
@@ -28,7 +28,7 @@ class ProgressIndicator(Gtk.Dialog):
self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK)
self.set_default_size(150, 100)
self.spinner = Gtk.Spinner()
self.label = Gtk.Label(label=" Upscaling. This process will take long (if a Video). \n Duration depends on your Hardware and length and resolution of video \n You may see the output of the app, if you switch to the other window that is behind it. ")
self.label = Gtk.Label(label=" Upscaling. This process will take long (if a Video). \n Duration depends on your Hardware and length and resolution of video \n You may see the output of the app, if you switch to the other window that is behind it. \n\n\n click \"ok\" to start upscaling")
self.box = self.get_content_area()
self.box.pack_start(self.label, True, True, 20)
self.box.pack_start(self.spinner, True, True, 20)
@@ -41,7 +41,7 @@ class ErrorDialogFileMissing(Gtk.Dialog):
super().__init__(title="Error", transient_for=parent, flags=0)
self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK)
self.set_default_size(150, 100)
self.label = Gtk.Label(label=" No file specified. Please select a input AND output file! ")
self.label = Gtk.Label(label=" No file specified. Please select an input AND output file! ")
self.box = self.get_content_area()
self.box.pack_start(self.label, True, True, 20)
self.show_all()
@@ -63,7 +63,7 @@ class ErrorDialogCheckFail(Gtk.Dialog):
super().__init__(title="Error", transient_for=parent, flags=0)
self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK)
self.set_default_size(150, 100)
self.label = Gtk.Label(label=" Filechecks failed. Make sure to specify the same file extension in the output like in the input ")
self.label = Gtk.Label(label=" File and settings check failed. \n Make sure to specify the same file extension in the output like in the input \n make sure that the entries you made as settings are valid! (4 >= scale >= 1) ")
self.box = self.get_content_area()
self.box.pack_start(self.label, True, True, 20)
self.show_all()
@@ -72,6 +72,7 @@ class ErrorDialogCheckFail(Gtk.Dialog):
class HomeWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Test")
self.os_type = sys.platform
self.save_file = ""
self.open_file = ""
@@ -124,7 +125,7 @@ class HomeWindow(Gtk.Window):
self.quality_select_box.pack_start(self.quality_select_shrink, True, True, 20)
# Custom Quality Selector
self.custom_quality_selector_title = Gtk.Label(label="Custom Upscaling Multiplier")
self.custom_quality_selector_title = Gtk.Label(label="Custom Upscaling Multiplier\nNOTE that factors greater than 2 are not recommended!\nFactors greater than 4 will not run!")
self.custom_quality_selector_shrink = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.custom_quality_selector = Gtk.Entry()
self.custom_quality_selector_shrink.pack_start(self.custom_quality_selector, True, False, 30)
@@ -171,7 +172,24 @@ class HomeWindow(Gtk.Window):
def opfilechooser_clicked(self, widget):
self.filechooserdialog_save = Gtk.FileChooserDialog(title="Choose output file", action=Gtk.FileChooserAction.SAVE)
Gtk.FileChooser.set_do_overwrite_confirmation(self.filechooserdialog_save, True)
if self.os_type == "linux":
Gtk.FileChooser.set_current_folder(self.filechooserdialog_save, "/home")
elif self.os_type == "win32":
Gtk.FileChooser.set_current_folder(self.filechooserdialog_save, "%HOMEPATH%")
else:
pass
if str(self.open_file)[len(self.open_file) - 4:] == ".mp4":
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "video.mp4")
elif str(self.open_file)[len(self.open_file) - 4:] == ".mkv":
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "video.mkv")
elif str(self.open_file)[len(self.open_file) - 4:] == ".png":
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "image.png")
elif str(self.open_file)[len(self.open_file) - 4:] == ".jpg":
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "image.jpg")
elif str(self.open_file)[len(self.open_file) - 4:] == ".jpeg":
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "image.jpeg")
else:
Gtk.FileChooser.set_current_name(self.filechooserdialog_save, "")
self.filechooserdialog_save.add_buttons(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
@@ -180,10 +198,9 @@ class HomeWindow(Gtk.Window):
)
self.response = self.filechooserdialog_save.run()
if self.response == Gtk.ResponseType.OK:
print("ok, selected file:", self.filechooserdialog_save.get_filename())
self.save_file = self.filechooserdialog_save.get_filename()
elif self.response == Gtk.ResponseType.CANCEL:
print("cancel")
pass
self.filechooserdialog_save.destroy()
def start_clicked(self, widget):
@@ -199,16 +216,19 @@ class HomeWindow(Gtk.Window):
if self.respawn:
if str(self.open_file) != "" and str(self.save_file) != "":
print("ok")
if checks.perform(self.output, self.custom_quality_selector.get_text(), self.open_file, self.save_file):
if self.output == "Custom (will respect value below)":
self.quality_selected = "custom"
if self.custom_quality_selector.get_text()[len(self.custom_quality_selector.get_text()) - 1] == "x":
self.q = f"{self.custom_quality_selector.get_text()} {self.custom_quality_selector.get_text()}"
else:
self.q = f"{self.custom_quality_selector.get_text()}x {self.custom_quality_selector.get_text()}x"
else:
self.quality_selected = "default"
self.q = str(arg.get(self.output))
self.go = True
if self.go:
print("\n\nStarting upscaling process!\n\n")
self.pr_i = ProgressIndicator(self)
self.pr_i.run()
self.pr_i.destroy()
@@ -229,6 +249,7 @@ class HomeWindow(Gtk.Window):
print("no file specified")
self.fileerror()
else:
self.runningerror()
print("Already running!")
def runningerror(self):