-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
50 lines (39 loc) · 1.53 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
from datetime import datetime, timedelta
import whisper
import sys
def convert_audio(file_path):
model = whisper.load_model("base")
result = model.transcribe(file_path, language="en", fp16=False, verbose=True)
# Generate the .vtt file
save_target = file_path + '.vtt'
with open(save_target, "w") as file:
file.write('WEBVTT' + '\n' + '\n')
for index, segment in enumerate(result['segments']):
file.write(str(index + 1) + '\n')
start = format_delta_time(segment['start'])
end = format_delta_time(segment['end'])
file.write(str(start) + ' --> ' + str(end) + '\n')
file.write(segment['text'].strip() + '\n')
file.write('\n')
# Generate the .txt file
save_target = file_path + '.txt'
with open(save_target, "w") as file:
file.write("Converted text:\n")
file.write(result['text'])
def format_delta_time(seconds):
delta = timedelta(seconds=seconds)
# Format in "0:00:00.xxx"
formatted_time = "{:0>2}:{:0>2}:{:06.3f}".format(
delta.seconds // 3600, (delta.seconds % 3600) // 60, delta.total_seconds() % 60
)
return formatted_time
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python script.py <argument>")
sys.exit(1)
filepath = sys.argv[1]
start_time = datetime.now()
print("### Starting conversion ###" + '\n')
convert_audio(filepath)
execution_time = datetime.now() - start_time
print('\n' + "### Done in ", execution_time, " ###")