-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisperExample1.py
49 lines (39 loc) · 1.45 KB
/
whisperExample1.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
import os
import whisper
import numpy as np
from pydub import AudioSegment
from whisper.utils import get_writer
def transcribe():
# define model to use
model = whisper.load_model("small")
# path of the audio file to transcribe
audio_path = "audio2.mp3"
# path where to save transcription
output_directory = "./transcriptions1"
# try to create output directory
# if directory already exists, ignore and continue code
try:
os.mkdir(output_directory)
except FileExistsError:
''
except Exception:
raise
# transcribe audio by passing audio data as numpy array to the model's 'transcribe' function
result = model.transcribe(audio_path)
# print transcription on console without line breaks
print(result["text"])
# build options for transcription writer objects
options = {'max_line_width': 500, 'max_line_count':500, "highlight_words": False}
# get objects to write transcription as different files
txt_writer = get_writer("txt", output_directory)
json_writer = get_writer("json", output_directory)
vtt_writer = get_writer("vtt", output_directory)
tsv_writer = get_writer("tsv", output_directory)
srt_writer = get_writer("srt", output_directory)
# save transcription in different format files
txt_writer(result, audio_path, options)
json_writer(result, audio_path, options)
vtt_writer(result, audio_path, options)
tsv_writer(result, audio_path, options)
srt_writer(result, audio_path, options)
transcribe()