-
Notifications
You must be signed in to change notification settings - Fork 0
/
speak_translated_faster_Windows.py
executable file
·60 lines (45 loc) · 1.21 KB
/
speak_translated_faster_Windows.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
import multiprocessing
import os
import pyautogui
import pyperclip
from keyboard import wait
from myLib.normalize_str import removeNewline, removeReturn
from myLib.translate import trans
from myLib.TTS import tts
def normalize_str(text: str) -> str:
text = removeReturn(removeNewline(text))
return text
# kill prosses
def kill(pid: int) -> bool:
try:
os.kill(pid, 9)
except OSError:
return False
else:
return True
def get_text():
pyautogui.hotkey("ctrl", "c")
return pyperclip.paste()
def run(prev_pid: int):
if kill(prev_pid):
print(f"killed {prev_pid}")
else:
text = normalize_str(get_text())
translated = trans(
source_lang="en",
target_lang="vi",
source_text=text,
translator="api",
)
tts(text=translated, mode="online", player="mpv", speed=2.0, engine="main.exe")
def main():
# configs
hotkey = r"F9"
prev_pid = 0
while True:
wait(hotkey)
p = multiprocessing.Process(target=run, args=(prev_pid,))
p.start()
prev_pid = p.pid
if __name__ == "__main__":
main()