-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
224 lines (190 loc) · 8.62 KB
/
Copy pathapp.py
File metadata and controls
224 lines (190 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import time
from filelock import FileLock, Timeout
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.scrolledtext import ScrolledText
from tkinter import ttk
import re
import threading
import utils
import custom_logger
LOGGER = custom_logger.Logger('app.log')
logger = LOGGER.get_logger()
# Global Variables
ROOT_DIR = os.getcwd()
OUTPUT_FILE_PATH = os.path.join(ROOT_DIR, "output/")
VIDEO_FILE_NAME = "video.mp4"
SUBTITLE_FILE_NAME = "video.srt"
# https://stackoverflow.com/questions/9662346/python-code-to-remove-html-tags-from-a-string
SUBS_CLEAN_REGEX = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});')
class SubtitleExtractorApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("AutoDub Subtitles Extractor")
self.geometry("800x600")
self.current_frame = None
self.switch_frame(SelectSourcePage)
# Add logo to the taskbar
self.iconphoto(False, tk.PhotoImage(file='logo.png'))
def switch_frame(self, frame_class, *args):
new_frame = frame_class(self, *args)
if self.current_frame is not None:
self.current_frame.destroy()
self.current_frame = new_frame
self.current_frame.pack()
class SelectSourcePage(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="Select Video Source").pack(pady=10)
tk.Button(self, text="Select Video File", command=self.select_video_file).pack(pady=5)
def select_video_file(self):
try:
file_path = filedialog.askopenfilename(
filetypes=[("MP4 files", "*.mp4"), ("AVI files", "*.avi"), ("MKV files", "*.mkv")]
)
if file_path:
utils.copy_file_from_src_to_dest(
source_path=file_path,
dest_path=VIDEO_FILE_NAME
)
self.master.switch_frame(SubtitleOptionPage)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while selecting the file: {e}")
class SubtitleOptionPage(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text=f"Selected Video: {VIDEO_FILE_NAME}").pack(pady=10)
tk.Button(self, text="Extract Subtitles", command=self.extract_subtitles).pack(pady=5)
tk.Button(self, text="Back", command=lambda: master.switch_frame(SelectSourcePage)).pack(pady=5)
def extract_subtitles(self):
self.master.switch_frame(LoadingPage, "Extracting subtitles...", EditSubtitlePage, "Extracted subtitles will appear here...")
class EditSubtitlePage(tk.Frame):
def __init__(self, master, subtitles):
super().__init__(master)
tk.Label(self, text="Edit Subtitles:").pack(pady=10)
self.subtitle_text = ScrolledText(self, width=80, height=20)
self.subtitle_text.pack(pady=5)
if subtitles == "Extracted subtitles will appear here...":
with open(OUTPUT_FILE_PATH + SUBTITLE_FILE_NAME, 'r') as file:
subtitles = file.read()
cleaned_subtitles = re.sub(SUBS_CLEAN_REGEX, '', subtitles)
self.subtitle_text.insert(tk.END, cleaned_subtitles)
# Ensure the text widget handles the drag event properly
self.subtitle_text.bind("<B1-Motion>", self.on_text_drag)
# Bind undo (Ctrl+Z) and redo (Ctrl+Y) shortcuts
self.subtitle_text.bind('<Control-z>', self.undo)
self.subtitle_text.bind('<Control-y>', self.redo)
# self.subtitle_text.bind('<Meta-z>', self.undo)
# self.subtitle_text.bind('<Meta-y>', self.redo)
tk.Button(self, text="Save", command=self.save_subtitles).pack(pady=5)
tk.Button(self, text="Back", command=lambda: master.switch_frame(SelectSourcePage)).pack(pady=5)
def on_text_drag(self, event):
# Get the current mouse position
y = event.y
x = event.x
# Get the height and width of the text widget
height = self.subtitle_text.winfo_height()
width = self.subtitle_text.winfo_width()
# Determine the speed of scrolling
scroll_speed = 1
# Scroll down if the mouse is near the bottom edge
if y > height - 20:
self.subtitle_text.yview_scroll(scroll_speed, "units")
# Scroll up if the mouse is near the top edge
elif y < 20:
self.subtitle_text.yview_scroll(-scroll_speed, "units")
# Scroll right if the mouse is near the right edge
if x > width - 20:
self.subtitle_text.xview_scroll(scroll_speed, "units")
# Scroll left if the mouse is near the left edge
elif x < 20:
self.subtitle_text.xview_scroll(-scroll_speed, "units")
def undo(self, event=None):
self.subtitle_text.edit_undo()
def redo(self, event=None):
self.subtitle_text.edit_redo()
def save_subtitles(self):
try:
with open(OUTPUT_FILE_PATH + SUBTITLE_FILE_NAME, "w+") as file:
file.write(self.subtitle_text.get("1.0", tk.END))
self.master.switch_frame(FinalPage, OUTPUT_FILE_PATH + SUBTITLE_FILE_NAME)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while saving the subtitles: {e}")
class LoadingPage(tk.Frame):
def __init__(self, master, loading_text, next_frame_class, subtitles):
super().__init__(master)
self.next_frame_class = next_frame_class
self.next_frame_args = (subtitles,)
self.loading_thread = None
self.cancelled = False
tk.Label(self, text=loading_text).pack(pady=10)
self.progress = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(self, maximum=100, variable=self.progress)
self.progress_bar.pack(pady=10)
self.cancel_button = tk.Button(self, text="Cancel", command=self.cancel_extracting)
self.cancel_button.pack(pady=10)
# Add the ScrolledText widget to display the log
self.log_display = ScrolledText(self, width=80, height=20, state='disabled')
self.log_display.pack(pady=10)
self.start_loading()
def start_loading(self):
self.cancelled = False
self.loading_thread = threading.Thread(target=self.simulate_loading)
self.loading_thread.start()
self.log_thread = threading.Thread(target=self.update_log_display)
self.log_thread.start()
def simulate_loading(self):
for i in range(101):
if self.cancelled:
return
self.progress.set(i)
self.update_idletasks()
if i == 50:
utils.extract_hardcoded_subs(VIDEO_FILE_NAME)
time.sleep(0.05)
self.master.switch_frame(self.next_frame_class, *self.next_frame_args)
def cancel_extracting(self):
self.cancelled = True
if self.loading_thread is not None:
self.loading_thread.join()
if self.log_thread is not None:
self.log_thread.join()
self.master.switch_frame(SubtitleOptionPage)
def update_log_display(self):
log_file_path = "logs/app.log"
# Create the log file if it doesn't exist
if not os.path.exists(log_file_path):
with open(log_file_path, 'w'):
pass # Just to create the file
with open(log_file_path, 'r') as file:
while not self.cancelled:
where = file.tell()
line = file.readline()
if not line:
time.sleep(0.1)
file.seek(where)
else:
self.log_display.config(state='normal')
self.log_display.insert(tk.END, line)
self.log_display.config(state='disabled')
self.log_display.yview(tk.END)
class FinalPage(tk.Frame):
def __init__(self, master, final_path):
super().__init__(master)
utils.run_cleanup()
tk.Label(self, text=f"Final subtitle saved at: {final_path}").pack(pady=10)
tk.Button(self, text="Start Afresh", command=lambda: master.switch_frame(SelectSourcePage)).pack(pady=5)
if __name__ == "__main__":
lock = FileLock("my_app.lock")
try:
# Attempt to acquire the lock with a timeout
with lock.acquire(timeout=1):
app = SubtitleExtractorApp()
app.mainloop()
except Timeout:
# If timeout occurs, another instance is running
root = tk.Tk()
root.withdraw() # Hide the main window
messagebox.showerror("Error", "Another instance of this application is already running.")
root.destroy() # Destroy the hidden main window