-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
147 lines (109 loc) · 3.18 KB
/
main.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
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
import time
from pynput import keyboard
from pynput.keyboard import Key, Controller
import pyperclip
import youtube_dl
import subprocess
import os
import re
from thefuzz import process
from fuzzywuzzy import fuzz
import sys
import webbrowser
whisper_path = "" #TODO set this to your whisper path
controller = Controller()
url = None
cwd = None
transcript = []
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'temp/out.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
def getVideo():
global cwd, url, transcript
with controller.pressed(Key.cmd):
controller.tap("l")
controller.tap("a")
controller.tap("c")
time.sleep(0.1)
url = pyperclip.paste()
if not url: return
print("Video:", url)
cwd = os.getcwd()
os.chdir(whisper_path)
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info_dict)
ffmpeg_command = [
'ffmpeg',
'-i', 'temp/out.mp3', # Input file
'-ar', '16000', # Set sample rate to 16kHz
'-ac', '1', # Set number of audio channels to 1 (mono)
'-c:a', 'pcm_s16le', # Set audio codec to PCM 16-bit little-endian
'temp/out.wav' # Output file
]
try:
subprocess.run(ffmpeg_command, check=True)
print("Conversion completed successfully.")
except subprocess.CalledProcessError as e:
print("Conversion failed:", e)
output = subprocess.check_output("./main -f temp/out.wav", shell=True).decode("utf-8")
transcript = output.split("\n")
def time_to_seconds(time_str):
pattern = r'\[(\d+):(\d+):(\d+)\.\d+'
match = re.match(pattern, time_str)
if match:
hours = int(match.group(1))
minutes = int(match.group(2))
seconds = int(match.group(3))
# Convert the time to seconds
total_seconds = hours * 3600 + minutes * 60 + seconds
return str(total_seconds)
else:
return None
def getQuery():
global url, transcript
if "&" in url:
url = url[:url.index("&")]
with controller.pressed(Key.cmd):
controller.tap("a")
controller.tap("c")
time.sleep(0.1)
query = pyperclip.paste()
if not query: return
print("Query:", query)
matches = process.extract(query, transcript, scorer=fuzz.ratio)
seconds = time_to_seconds(matches[0][0])
print("Top Matched URL: ", url + "&t=" + seconds)
webbrowser.open(url + "&t=" + seconds, new=2, autoraise=True)
def f9():
try:
getVideo()
except Exception as e:
print(e)
def f10():
try:
getQuery()
except Exception as e:
print(e)
def exit_handler():
try:
subprocess.call(["rm", "temp/out.wav"])
subprocess.call(["rm", "temp/out.mp3"])
os.chdir(cwd)
except:
print("---Error---")
finally:
print("---Exit---")
sys.exit(0)
if __name__ == "__main__":
try:
with keyboard.GlobalHotKeys({"<109>": f10, "<101>": f9}) as h:
h.join()
except KeyboardInterrupt:
exit_handler()