mirror of
https://github.com/k4yt3x/video2x.git
synced 2025-02-20 22:39:39 +00:00
fixed temp directory cleaning problem on exit
This commit is contained in:
parent
19e17b1a8f
commit
332055a4e5
@ -4,7 +4,7 @@
|
|||||||
Name: Video2X Upscaler
|
Name: Video2X Upscaler
|
||||||
Author: K4YT3X
|
Author: K4YT3X
|
||||||
Date Created: December 10, 2018
|
Date Created: December 10, 2018
|
||||||
Last Modified: October 6, 2019
|
Last Modified: December 11, 2019
|
||||||
|
|
||||||
Dev: SAT3LL
|
Dev: SAT3LL
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ class Upscaler:
|
|||||||
"""delete temp directories when done
|
"""delete temp directories when done
|
||||||
"""
|
"""
|
||||||
if not self.preserve_frames:
|
if not self.preserve_frames:
|
||||||
for directory in [self.extracted_frames, self.upscaled_frames]:
|
for directory in [self.extracted_frames, self.upscaled_frames, self.video2x_cache_directory]:
|
||||||
try:
|
try:
|
||||||
# avalon framework cannot be used if python is shutting down
|
# avalon framework cannot be used if python is shutting down
|
||||||
# therefore, plain print is used
|
# therefore, plain print is used
|
||||||
|
@ -13,7 +13,7 @@ __ __ _ _ ___ __ __
|
|||||||
Name: Video2X Controller
|
Name: Video2X Controller
|
||||||
Creator: K4YT3X
|
Creator: K4YT3X
|
||||||
Date Created: Feb 24, 2018
|
Date Created: Feb 24, 2018
|
||||||
Last Modified: November 15, 2019
|
Last Modified: December 11, 2019
|
||||||
|
|
||||||
Editor: BrianPetkovsek
|
Editor: BrianPetkovsek
|
||||||
Editor: SAT3LL
|
Editor: SAT3LL
|
||||||
@ -438,6 +438,10 @@ except Exception:
|
|||||||
Avalon.error('An exception has occurred')
|
Avalon.error('An exception has occurred')
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# try cleaning up temp directories
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
upscaler.cleanup_temp_directories()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# remove Video2X cache directory
|
# remove Video2X cache directory
|
||||||
with contextlib.suppress(FileNotFoundError):
|
with contextlib.suppress(FileNotFoundError):
|
||||||
|
@ -18,6 +18,7 @@ from tkinter import *
|
|||||||
from tkinter import messagebox
|
from tkinter import messagebox
|
||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
from tkinter.filedialog import *
|
from tkinter.filedialog import *
|
||||||
|
import contextlib
|
||||||
import pathlib
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
@ -204,7 +205,7 @@ class Video2xGui():
|
|||||||
|
|
||||||
# preserve frames
|
# preserve frames
|
||||||
self.preserve_frames = BooleanVar(self.options_left)
|
self.preserve_frames = BooleanVar(self.options_left)
|
||||||
self.preserve_frames.set(True)
|
self.preserve_frames.set(False)
|
||||||
Label(self.options_right, text='Preserve Frames', relief=RIDGE, width=15).grid(row=3, column=0, padx=2, pady=3)
|
Label(self.options_right, text='Preserve Frames', relief=RIDGE, width=15).grid(row=3, column=0, padx=2, pady=3)
|
||||||
preserve_frames_menu = OptionMenu(self.options_right, self.preserve_frames, *{True, False})
|
preserve_frames_menu = OptionMenu(self.options_right, self.preserve_frames, *{True, False})
|
||||||
preserve_frames_menu.grid(row=3, column=1, padx=2, pady=3, sticky=W)
|
preserve_frames_menu.grid(row=3, column=1, padx=2, pady=3, sticky=W)
|
||||||
@ -267,95 +268,104 @@ class Video2xGui():
|
|||||||
|
|
||||||
def _upscale(self):
|
def _upscale(self):
|
||||||
|
|
||||||
# start timer
|
try:
|
||||||
begin_time = time.time()
|
# start timer
|
||||||
|
begin_time = time.time()
|
||||||
|
|
||||||
# read configuration file
|
# read configuration file
|
||||||
config = read_config(VIDEO2X_CONFIG)
|
config = read_config(VIDEO2X_CONFIG)
|
||||||
config = absolutify_paths(config)
|
config = absolutify_paths(config)
|
||||||
|
|
||||||
input_file = pathlib.Path(self.input_file.get())
|
input_file = pathlib.Path(self.input_file.get())
|
||||||
output_file = pathlib.Path(self.output_file.get())
|
output_file = pathlib.Path(self.output_file.get())
|
||||||
driver = AVAILABLE_DRIVERS[self.driver.get()]
|
driver = AVAILABLE_DRIVERS[self.driver.get()]
|
||||||
|
|
||||||
# load specified driver's config into driver_settings
|
# load specified driver's config into driver_settings
|
||||||
driver_settings = config[driver]
|
driver_settings = config[driver]
|
||||||
|
|
||||||
# if executable doesn't exist, show warning
|
# if executable doesn't exist, show warning
|
||||||
if not pathlib.Path(driver_settings['path']).is_file() and not pathlib.Path(f'{driver_settings["path"]}.exe').is_file():
|
if not pathlib.Path(driver_settings['path']).is_file() and not pathlib.Path(f'{driver_settings["path"]}.exe').is_file():
|
||||||
messagebox.showerror('Error', 'Specified driver directory doesn\'t exist\nPlease check the configuration file settings')
|
messagebox.showerror('Error', 'Specified driver directory doesn\'t exist\nPlease check the configuration file settings')
|
||||||
raise FileNotFoundError(driver_settings['path'])
|
raise FileNotFoundError(driver_settings['path'])
|
||||||
|
|
||||||
# read FFmpeg configuration
|
# read FFmpeg configuration
|
||||||
ffmpeg_settings = config['ffmpeg']
|
ffmpeg_settings = config['ffmpeg']
|
||||||
|
|
||||||
# load video2x settings
|
# load video2x settings
|
||||||
image_format = config['video2x']['image_format'].lower()
|
image_format = config['video2x']['image_format'].lower()
|
||||||
preserve_frames = config['video2x']['preserve_frames']
|
preserve_frames = config['video2x']['preserve_frames']
|
||||||
|
|
||||||
# load cache directory
|
# load cache directory
|
||||||
if isinstance(config['video2x']['video2x_cache_directory'], str):
|
if isinstance(config['video2x']['video2x_cache_directory'], str):
|
||||||
video2x_cache_directory = pathlib.Path(config['video2x']['video2x_cache_directory'])
|
video2x_cache_directory = pathlib.Path(config['video2x']['video2x_cache_directory'])
|
||||||
else:
|
|
||||||
video2x_cache_directory = pathlib.Path(tempfile.gettempdir()) / 'video2x'
|
|
||||||
|
|
||||||
if video2x_cache_directory.exists() and not video2x_cache_directory.is_dir():
|
|
||||||
messagebox.showerror('Error', 'Specified cache directory is a file/link')
|
|
||||||
raise FileExistsError('Specified cache directory is a file/link')
|
|
||||||
|
|
||||||
elif not video2x_cache_directory.exists():
|
|
||||||
# try creating the cache directory
|
|
||||||
if messagebox.askyesno('Question', f'Specified cache directory {video2x_cache_directory} does not exist\nCreate directory?'):
|
|
||||||
try:
|
|
||||||
video2x_cache_directory.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
# there can be a number of exceptions here
|
|
||||||
# PermissionError, FileExistsError, etc.
|
|
||||||
# therefore, we put a catch-them-all here
|
|
||||||
except Exception as e:
|
|
||||||
messagebox.showerror('Error', f'Unable to create {video2x_cache_directory}\nAborting...')
|
|
||||||
raise e
|
|
||||||
else:
|
else:
|
||||||
raise FileNotFoundError('Could not create cache directory')
|
video2x_cache_directory = pathlib.Path(tempfile.gettempdir()) / 'video2x'
|
||||||
|
|
||||||
# load more settings from gui
|
if video2x_cache_directory.exists() and not video2x_cache_directory.is_dir():
|
||||||
width = self.width.get()
|
messagebox.showerror('Error', 'Specified cache directory is a file/link')
|
||||||
height = self.height.get()
|
raise FileExistsError('Specified cache directory is a file/link')
|
||||||
scale_ratio = self.scale_ratio.get()
|
|
||||||
image_format = self.image_format.get()
|
|
||||||
threads = self.threads.get()
|
|
||||||
method = AVAILABLE_METHODS[self.method.get()]
|
|
||||||
preserve_frames = self.preserve_frames.get()
|
|
||||||
|
|
||||||
self.upscaler = Upscaler(input_video=input_file, output_video=output_file, method=method, driver_settings=driver_settings, ffmpeg_settings=ffmpeg_settings)
|
elif not video2x_cache_directory.exists():
|
||||||
|
# try creating the cache directory
|
||||||
|
if messagebox.askyesno('Question', f'Specified cache directory {video2x_cache_directory} does not exist\nCreate directory?'):
|
||||||
|
try:
|
||||||
|
video2x_cache_directory.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# set optional options
|
# there can be a number of exceptions here
|
||||||
self.upscaler.waifu2x_driver = driver
|
# PermissionError, FileExistsError, etc.
|
||||||
self.upscaler.scale_width = width
|
# therefore, we put a catch-them-all here
|
||||||
self.upscaler.scale_height = height
|
except Exception as e:
|
||||||
self.upscaler.scale_ratio = scale_ratio
|
messagebox.showerror('Error', f'Unable to create {video2x_cache_directory}\nAborting...')
|
||||||
self.upscaler.model_dir = None
|
raise e
|
||||||
self.upscaler.threads = threads
|
else:
|
||||||
self.upscaler.video2x_cache_directory = video2x_cache_directory
|
raise FileNotFoundError('Could not create cache directory')
|
||||||
self.upscaler.image_format = image_format
|
|
||||||
self.upscaler.preserve_frames = preserve_frames
|
|
||||||
|
|
||||||
# run upscaler
|
# load more settings from gui
|
||||||
self.upscaler.create_temp_directories()
|
width = self.width.get()
|
||||||
|
height = self.height.get()
|
||||||
|
scale_ratio = self.scale_ratio.get()
|
||||||
|
image_format = self.image_format.get()
|
||||||
|
threads = self.threads.get()
|
||||||
|
method = AVAILABLE_METHODS[self.method.get()]
|
||||||
|
preserve_frames = self.preserve_frames.get()
|
||||||
|
|
||||||
# start progress bar
|
self.upscaler = Upscaler(input_video=input_file, output_video=output_file, method=method, driver_settings=driver_settings, ffmpeg_settings=ffmpeg_settings)
|
||||||
progress_bar = threading.Thread(target=self._progress_bar)
|
|
||||||
progress_bar.start()
|
|
||||||
|
|
||||||
# start upscaling
|
# set optional options
|
||||||
self.upscaler.run()
|
self.upscaler.waifu2x_driver = driver
|
||||||
self.upscaler.cleanup_temp_directories()
|
self.upscaler.scale_width = width
|
||||||
|
self.upscaler.scale_height = height
|
||||||
|
self.upscaler.scale_ratio = scale_ratio
|
||||||
|
self.upscaler.model_dir = None
|
||||||
|
self.upscaler.threads = threads
|
||||||
|
self.upscaler.video2x_cache_directory = video2x_cache_directory
|
||||||
|
self.upscaler.image_format = image_format
|
||||||
|
self.upscaler.preserve_frames = preserve_frames
|
||||||
|
|
||||||
|
# run upscaler
|
||||||
|
self.upscaler.create_temp_directories()
|
||||||
|
|
||||||
|
# start progress bar
|
||||||
|
progress_bar = threading.Thread(target=self._progress_bar)
|
||||||
|
progress_bar.start()
|
||||||
|
|
||||||
|
# start upscaling
|
||||||
|
self.upscaler.run()
|
||||||
|
self.upscaler.cleanup_temp_directories()
|
||||||
|
|
||||||
|
# show message when upscaling completes
|
||||||
|
messagebox.showinfo('Info', f'Upscaling Completed\nTime Taken: {round((time.time() - begin_time), 5)} seconds')
|
||||||
|
self.progress_bar['value'] = 100
|
||||||
|
self.running = False
|
||||||
|
self.start_button_text.set('Start')
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
messagebox.showerror('Error', f'Upscaler ran into an error:\n{e}')
|
||||||
|
|
||||||
|
# try cleaning up temp directories
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
self.upscaler.cleanup_temp_directories()
|
||||||
|
|
||||||
# show message when upscaling completes
|
|
||||||
messagebox.showinfo('Info', f'Upscaling Completed\nTime Taken: {round((time.time() - begin_time), 5)} seconds')
|
|
||||||
self.progress_bar['value'] = 100
|
|
||||||
self.running = False
|
|
||||||
self.start_button_text.set('Start')
|
|
||||||
|
|
||||||
def _progress_bar(self):
|
def _progress_bar(self):
|
||||||
""" This method prints a progress bar
|
""" This method prints a progress bar
|
||||||
|
Loading…
Reference in New Issue
Block a user