-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.py
24 lines (19 loc) · 856 Bytes
/
audio.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
from datetime import timedelta
import os
import whisper
def transcribe_audio(path):
model = whisper.load_model("base") # Change this to your desired model
print("Whisper model loaded.")
transcribe = model.transcribe(audio=path)
segments = transcribe['segments']
for segment in segments:
startTime = str(0)+str(timedelta(seconds=int(segment['start'])))+',000'
endTime = str(0)+str(timedelta(seconds=int(segment['end'])))+',000'
text = segment['text']
segmentId = segment['id']+1
segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] is ' ' else text}\n\n"
srtFilename = os.path.join("SrtFiles", f"file.srt")
with open(srtFilename, 'a', encoding='utf-8') as srtFile:
srtFile.write(segment)
return srtFilename
transcribe_audio("audio_new.wav")