Skip to content

Commit d223472

Browse files
authored
Create whisperGUI.py
1 parent f84997f commit d223472

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

old/whisperGUI.py

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
from gooey import Gooey, GooeyParser
2+
import subprocess
3+
import os
4+
import re
5+
6+
@Gooey(program_name='whisper.cppGUI',
7+
menu=[{'name': 'File',
8+
'items': [{
9+
'type': 'AboutDialog',
10+
'menuTitle': 'Download GGML models',
11+
'name': 'Download the GGML models below',
12+
'website': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/tree/main'
13+
},{
14+
'type': 'Link',
15+
'menuTitle': 'Visit Our Site',
16+
'url': 'https://github.com/Topping1/whispercppGUI'
17+
}]
18+
}]
19+
)
20+
def main():
21+
parser = GooeyParser(description='GUI for whisper.cpp, a high-performance C++ port of OpenAI\'s Whisper')
22+
23+
parser.add_argument(
24+
'--file',
25+
metavar='File to transcribe',
26+
help='supports all file types supported by FFMPEG',
27+
widget='FileChooser')
28+
29+
parser.add_argument(
30+
'--model',
31+
metavar='GGML model (.bin)',
32+
help='select GGML model (tiny,base,small,medium,large)',
33+
widget='FileChooser')
34+
35+
parser.add_argument(
36+
'--language',
37+
metavar='Language',
38+
help='select language for transcription',
39+
choices=['en','zh','de','es','ru','ko','fr','ja','pt','tr','pl','ca','nl','ar','sv','it','id','hi','fi','vi','iw','uk','el','ms','cs','ro','da','hu','ta','no','th','ur','hr','bg','lt','la','mi','ml','cy','sk','te','fa','lv','bn','sr','az','sl','kn','et','mk','br','eu','is','hy','ne','mn','bs','kk','sq','sw','gl','mr','pa','si','km','sn','yo','so','af','oc','ka','be','tg','sd','gu','am','yi','lo','uz','fo','ht','ps','tk','nn','mt','sa','lb','my','bo','tl','mg','as','tt','haw','ln','ha','ba','jw','su'],
40+
widget='FilterableDropdown')
41+
42+
parser.add_argument(
43+
'--translate',
44+
action='store_true',
45+
help='check to translate transcription to english')
46+
47+
parser.add_argument(
48+
'-otxt',
49+
'--output-txt',
50+
action='store_true',
51+
help='check to output result in a text file')
52+
53+
parser.add_argument(
54+
'-osrt',
55+
'--output-srt',
56+
action='store_true',
57+
help='check to output result in a srt file')
58+
59+
parser.add_argument(
60+
'-ovtt',
61+
'--output-vtt',
62+
action='store_true',
63+
help='check to output result in a vtt file')
64+
65+
parser.add_argument(
66+
'--speed-up2',
67+
action='store',
68+
default=1,
69+
help='alternative speed up based on FFMPEG. Type here the speed up factor (e.g. 1.5). This enables automatically SRT output with corrected timestamps',
70+
widget='DecimalField')
71+
72+
parser.add_argument(
73+
'--others',
74+
action='store',
75+
default="",
76+
help='This textbox lets the user add other command line parameters that are not included in this GUI')
77+
78+
parser.add_argument(
79+
'--shell',
80+
action='store_true',
81+
help='check to show the shell window instead of using the Gooey window. This can fix some UTF-8 errors')
82+
83+
84+
args = parser.parse_args()
85+
#enable for debugging
86+
# print(args)
87+
#pass args for later use in args=main()
88+
return args
89+
90+
91+
92+
if __name__ == '__main__':
93+
94+
95+
# this section is inspired by
96+
# https://stackoverflow.com/questions/48767005/using-python-gooey-how-to-open-another-gui-after-clicking-one-out-of-multiple-bu
97+
98+
#get arguments from main() for use here
99+
args=main()
100+
101+
#workaround to process the arguments that evaluate to "True" or "False"
102+
103+
if args.translate == True:
104+
arg_translate = "--translate"
105+
else:
106+
arg_translate = ""
107+
108+
if args.output_txt == True:
109+
arg_out_txt = "--output-txt"
110+
else:
111+
arg_out_txt = ""
112+
113+
if args.output_srt == True:
114+
arg_out_srt = "--output-srt"
115+
else:
116+
arg_out_srt = ""
117+
118+
if args.output_vtt == True:
119+
arg_out_vtt = "--output-vtt"
120+
else:
121+
arg_out_vtt = ""
122+
123+
124+
#check if ffmpeg speedup was selected. If true, disable txt and vtt output
125+
#and disable whispercpp internal speed up.
126+
if float(args.speed_up2) != 1.0:
127+
arg_out_txt = ""
128+
arg_out_vtt = ""
129+
arg_speed = ""
130+
arg_out_srt = "--output-srt"
131+
132+
# Split the input filename and extension
133+
input_basename, _ = os.path.splitext(os.path.basename(args.file))
134+
135+
# Construct new output filenames based on input filename
136+
output_wav = f"{input_basename}.wav"
137+
output_srt = f"{input_basename}.wav.srt"
138+
output_fixed_srt = f"{input_basename}_fixed.srt"
139+
140+
#first we process the input file with ffmpeg
141+
#here we construct the command line for ffmpeg and apply the FFMPEG speedup IF selected
142+
if float(args.speed_up2) != 1.0:
143+
cmd = f"ffmpeg.exe -y -i \"{args.file}\" -ar 16000 -ac 1 -c:a pcm_s16le -af atempo={args.speed_up2} {output_wav}"
144+
else:
145+
cmd = f"ffmpeg.exe -y -i \"{args.file}\" -ar 16000 -ac 1 -c:a pcm_s16le {output_wav}"
146+
147+
if args.shell == True:
148+
#here we call the program with extra parameters to capture ffmpeg output
149+
process=subprocess.Popen(cmd, text=True)
150+
else:
151+
#workaround required to show ffmpeg output in the Gooey window
152+
#reference https://github.com/chriskiehl/Gooey/issues/355
153+
startupinfo = subprocess.STARTUPINFO()
154+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
155+
#here we call the program with extra parameters to capture ffmpeg output
156+
process=subprocess.Popen(cmd,
157+
startupinfo=startupinfo,
158+
stdout=subprocess.PIPE,
159+
stdin=subprocess.PIPE,
160+
stderr=subprocess.STDOUT)
161+
#here we print ffmpeg output to the Gooey window
162+
for line in process.stdout:
163+
line1=line.decode('utf-8')
164+
print(line1.rstrip())
165+
166+
167+
168+
169+
#here we run whisperCPP
170+
#here we construct the command line for whisperCPP
171+
cmd = f"main.exe -f {output_wav} -m {args.model} -l {args.language} {arg_translate} {arg_out_txt} {arg_out_srt} {arg_out_vtt} {args.others}"
172+
173+
174+
if args.shell == True:
175+
#here we call the program with extra parameters to capture ffmpeg output
176+
process=subprocess.Popen(cmd, text=True)
177+
else:
178+
179+
#workaround required to show whisperCPP output in the Gooey window
180+
#reference https://github.com/chriskiehl/Gooey/issues/355
181+
182+
startupinfo = subprocess.STARTUPINFO()
183+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
184+
#here we call the program with extra parameters to capture whisperCPP output
185+
process=subprocess.Popen(cmd,
186+
startupinfo=startupinfo,
187+
stdout=subprocess.PIPE,
188+
stdin=subprocess.PIPE,
189+
stderr=subprocess.STDOUT)
190+
#here we print whisperCPP output to the Gooey window
191+
for line in process.stdout:
192+
line1=line.decode('utf-8')
193+
print(line1.rstrip())
194+
195+
#this section fixes the timestamps of the SRT file if the FFMPEG speedup was selected
196+
#
197+
198+
if float(args.speed_up2) != 1.0:
199+
speedup_factor = float(args.speed_up2) # assign the speedup factor
200+
with open(output_srt, "r") as file: # Open the input SRT file
201+
content = file.read()
202+
file.close()
203+
matches = re.findall(r"\d{2}:\d{2}:\d{2},\d{3}", content) # Use regular expressions to match timestamps in the SRT file
204+
for match in matches: # Multiply the timestamps by the speedup factor
205+
parts = match.split(":")
206+
hours = int(parts[0])
207+
minutes = int(parts[1])
208+
seconds = int(parts[2].split(",")[0])
209+
milliseconds = int(parts[2].split(",")[1])
210+
total_milliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds
211+
total_milliseconds *= speedup_factor
212+
new_hours = f'{int(total_milliseconds // 3600000):02d}'
213+
new_minutes = f'{int((total_milliseconds % 3600000) // 60000):02d}'
214+
new_seconds = f'{int(((total_milliseconds % 3600000) % 60000) // 1000):02d}'
215+
new_milliseconds = f'{int(((total_milliseconds % 3600000) % 60000) % 1000):03d}'
216+
new_time = f"{new_hours}:{new_minutes}:{new_seconds},{new_milliseconds}"
217+
content = content.replace(match, new_time)
218+
with open(output_fixed_srt, "w") as file: # Write the adjusted SRT file to the output file
219+
file.write(content)
220+
file.close()
221+
os.remove(output_srt) #remove output.srt temporary file
222+
#end of section that fixes the timestamps
223+
224+
#remove output.wav temporary file created by ffmpeg
225+
os.remove(output_wav)

0 commit comments

Comments
 (0)