Skip to content

Instantly share code, notes, and snippets.

@ychoi-kr
Created November 5, 2023 09:37
Show Gist options
  • Save ychoi-kr/a14c484fa81cbc825451ed336182334d to your computer and use it in GitHub Desktop.
Save ychoi-kr/a14c484fa81cbc825451ed336182334d to your computer and use it in GitHub Desktop.
whisper scripts
# modified script from https://github.com/openai/whisper/discussions/98#discussioncomment-3725983
from datetime import timedelta
import os
import sys
import whisper
def transcribe_audio(path):
model = whisper.load_model("large")
print("Whisper model loaded.")
transcribe = model.transcribe(audio=path)
segments = transcribe['segments']
base_filename = os.path.splitext(os.path.basename(path))[0]
srtFilename = f"{base_filename}.srt"
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] == ' ' else text}\n\n"
with open(srtFilename, 'a', encoding='utf-8') as srtFile:
srtFile.write(segment)
return srtFilename
if __name__ == '__main__':
transcribe_audio(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment