Added user-defined output directory

The user can now define a custom output directory, in which the subsequent output folders are generated in
This commit is contained in:
frarol96 2023-10-11 20:01:43 +00:00
parent e6aff29a81
commit 9b58cacaf3

View File

@ -8,7 +8,7 @@ import feedparser
import subprocess
import platform
import urllib.request
from v2cmodules.toolbox import extract_version, debug, checkinifile, write2ini, extract_changelog, toggle_save, toggle_show
from v2cmodules.toolbox import extract_version, debug, checkinifile, write2ini, extract_changelog, toggle_save, toggle_show, create_output_directory, truncate_path
from v2cmodules.common import *
checkinifile()
@ -176,21 +176,6 @@ def check_for_updates():
tk.messagebox.showerror("Error", f"An error occurred: {str(e)}")
debug('ERROR', f"An error occured while checking for updates:\n{str(e)}\n")
# Function to create the output directory and return its path
def create_output_directory(video_filename):
debug('DEBUG', f"Creating output directory")
base_directory = os.getcwd() # You can change this to specify a different base directory
video_filename_base, _ = os.path.splitext(os.path.basename(video_filename))
output_folder_path = os.path.join(base_directory, video_filename_base)
# Create the output directory if it doesn't exist
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
debug('DEBUG', f"Processing file: {video_filename}")
debug('DEBUG', f"Created output directory at: {output_folder_path}")
return output_folder_path
# Function to calculate ROI position based on alignment and size settings
def calculate_roi(video_width, video_height, vertical_alignment, horizontal_alignment, roi_size):
y1 = (video_height - roi_size) // 2
@ -455,7 +440,7 @@ def cancel_processing():
# Create a button to process the selected files
def process_files():
debug('DEBUG', f"Starting file processing ...")
global processing, processing_thread, selected_files
global processing, processing_thread, selected_files, user_defined_output_dir
if not processing:
processing = True
selected_files = selected_files_listbox.get(0, tk.END)
@ -469,20 +454,24 @@ def process_files():
# Define dynamic process/cancel button
def process_cancel_toggle():
debug('DEBUG', f"Toggling process/cancel button state")
global processing, processing_thread, selected_files
if not processing:
toggle_process_button(False)
processing = True
selected_files = selected_files_listbox.get(0, tk.END)
processing_thread = threading.Thread(target=process_video, args=(selected_files, root))
processing_thread.daemon = True
processing_thread.start()
elif processing:
toggle_process_button(True)
processing = False
if processing_thread.is_alive():
processing_thread.join()
debug('DEBUG', f"Processing/Cancel button state switch finished")
global processing, processing_thread, selected_files, user_defined_output_dir
if os.path.exists(user_defined_output_dir):
if not processing:
toggle_process_button(False)
processing = True
selected_files = selected_files_listbox.get(0, tk.END)
processing_thread = threading.Thread(target=process_video, args=(selected_files, root))
processing_thread.daemon = True
processing_thread.start()
elif processing:
toggle_process_button(True)
processing = False
if processing_thread.is_alive():
processing_thread.join()
debug('DEBUG', f"Processing/Cancel button state switch finished")
elif not os.path.exists(user_defined_output_dir):
debug('DEBUG', f"The provided output directory \'{user_defined_output_dir}\' does not exist, prompting for new path")
messagebox.showinfo("Invalid output directory", f"The current output directory does not exist:\n{user_defined_output_dir}\nPlease select a valid directory in your Preferences!")
async def toggle_process_button(enable=bool):
"""True = Set state to 'Process Files' | False = Set state to 'Cancel'"""
@ -497,6 +486,7 @@ def open_preferences():
debug('DEBUG', f"Opening preferences window")
preferences_window = tk.Toplevel(root)
preferences_window.title("Preferences")
debug('DEBUG', f"Received truncated path: {truncated_udodir} ({type(truncated_udodir)})")
def change_frame_step():
global frame_step
@ -543,6 +533,24 @@ def open_preferences():
else:
messagebox.showinfo("Info", "Video2Crops is currently processing files!")
def change_output_dir():
global user_defined_output_dir
if not processing:
new_user_defined_output_dir = filedialog.askdirectory(title='Select Output Directory', mustexist=True, initialdir=user_defined_output_dir)
if new_user_defined_output_dir:
user_defined_output_dir = new_user_defined_output_dir
write2ini('settings', 'user_defined_output_dir', new_user_defined_output_dir)
debug('DEBUG', f"Set output directory to {new_user_defined_output_dir}")
# Update the text of the Label widget and truncated path
truncated_udodir = truncate_path(new_user_defined_output_dir, pre_max=10, post_max=20)
else:
debug('DEBUG', f"User canceled selection of output directory, reverting to \'{user_defined_output_dir}\'")
truncated_udodir = truncate_path(user_defined_output_dir, pre_max=10, post_max=20)
udodir_var.set(truncated_udodir)
else:
messagebox.showinfo("Info", "Video2Crops is currently processing files!")
debug('DEBUG', f"Creating preference fields ...")
# Create preference fields
@ -603,6 +611,13 @@ def open_preferences():
roi_size_button = tk.Button(preferences_window, text="Edit", command=apply_roi_size)
roi_size_button.grid(row=8, column=2, padx=20, pady=5)
udodir_label = tk.Label(preferences_window, text="Output Directory: ")
udodir_label.grid(row=9, column=0, columnspan=3, padx=20, pady=5)
udodir_value = tk.Label(preferences_window, textvariable=udodir_var)
udodir_value.grid(row=10, column=0, columnspan=2, padx=20, pady=5)
udodir_button = tk.Button(preferences_window, text="Edit", command=change_output_dir)
udodir_button.grid(row=10, column=2, padx=20, pady=5)
debug('DEBUG', f"Done creating preference fields")
debug('DEBUG', 'Functions defined')
@ -615,8 +630,9 @@ root.config(menu=menu)
frame_step_var = tk.StringVar()
contrast_threshold_var = tk.StringVar()
duplicate_threshold_var = tk.StringVar()
save_var = tk.BooleanVar()
show_var = tk.BooleanVar()
save_var = tk.BooleanVar() # Save Output
show_var = tk.BooleanVar() # Show Output
udodir_var = tk.StringVar() # User Defined Output DIRectory
# Variables for ROI
vertical_alignment_var = tk.StringVar()
@ -624,12 +640,14 @@ horizontal_alignment_var = tk.StringVar()
roi_size_var = tk.StringVar()
# Set initial values for preference variables
truncated_udodir = truncate_path(user_defined_output_dir, pre_max=10, post_max=20)
frame_step_var.set(str(frame_step))
contrast_threshold_var.set(str(contrast_threshold))
duplicate_threshold_var.set(str(duplicate_threshold))
save_var.set(SAVE)
show_var.set(SHOW)
roi_size_var.set(roi_size)
udodir_var.set(truncated_udodir)
# Create a "Queue" submenu
queue_menu = tk.Menu(menu, tearoff=0)