-
Notifications
You must be signed in to change notification settings - Fork 165
/
subtitler.py
96 lines (71 loc) · 2.59 KB
/
subtitler.py
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
import signal
import sys
import textwrap
import threading
import tkinter as tk
from os import getenv
from queue import Queue
from dotenv import load_dotenv
from modules.audio_translate import translate_audio
load_dotenv()
OFFSET_X = int(getenv('OFFSET_X'))
OFFSET_Y = int(getenv('OFFSET_Y'))
SUBTITLE_FONT_SIZE = int(getenv('SUBTITLE_FONT_SIZE'))
SUBTITLE_COLOR = getenv('SUBTITLE_COLOR')
SUBTITLE_BG_COLOR = getenv('SUBTITLE_BG_COLOR')
SACRIFICIAL_COLOR = getenv('SACRIFICIAL_COLOR')
def subtitle_updater(root, queue, label):
# Check if there is something new in the queue to display.
while not queue.empty():
# destroy old label since new message inbound
label.destroy()
if root.wm_state() == 'withdrawn':
# show root window
root.deiconify()
# create subtitle based on message in queue
msg = queue.get()
label = tk.Label(
text=textwrap.fill(msg, 64),
font=('Comic Sans MS', SUBTITLE_FONT_SIZE, 'bold italic'),
fg=SUBTITLE_COLOR,
bg=SUBTITLE_BG_COLOR
)
# hide root and destroy label after 3s
label.after(3000, root.withdraw)
label.after(3000, label.destroy)
# place subtitle at bottom middle of screen
label.pack(side='bottom', anchor='s')
root.update_idletasks()
# run every 0.5s
root.after(50, lambda: subtitle_updater(root, queue, label))
def setup_overlay():
# set tkinter gui to be topmost without window
root = tk.Tk()
root.overrideredirect(True)
root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}+{OFFSET_X}+{OFFSET_Y}')
root.lift()
root.wm_attributes('-topmost', True)
root.wm_attributes('-disabled', True)
# Sacrifice random color for transparency
root.wm_attributes('-transparentcolor', SACRIFICIAL_COLOR)
root.config(bg=SACRIFICIAL_COLOR)
# hide initial window
root.withdraw()
return root
def close_app(*_):
print('Closing subtitler.')
sys.exit(0)
def start_app():
# catch keyboard interrupt to stop main thread
signal.signal(signal.SIGINT, close_app)
overlay = setup_overlay()
subtitle = tk.Label()
subtitle_queue = Queue()
# thread to listen and translate audio
threading.Thread(target=translate_audio, args=[subtitle_queue], daemon=True).start()
# updates subtitles every 0.5s by checking queue
subtitle_updater(overlay, subtitle_queue, subtitle)
# set full-screen applications to borderless window for subtitles to appear over it
overlay.mainloop()
if __name__ == '__main__':
start_app()