使用Python,如何用Whisper来转写音频至SRT字幕
import whisper
import json
import os
import datetime
import whisper.utils
def transcription(audioPath: str, model: whisper.Whisper) -> list:
'''
Transcribe the audio file at the given path. The result is in str format.
'''
result = model.transcribe(audioPath, verbose=False, language="en")
return result
def batchTranscription(audioFolder: str, outputFolder: str):
'''
Transcribe the audio file at the given path. The result is in str format.
'''
model = whisper.load_model("large-v3")
writer = whisper.utils.WriteSRT(outputFolder)
if not os.path.exists(outputFolder):
os.makedirs(outputFolder)
for root, dirs, files in os.walk(audioFolder):
for file in files:
if file.endswith(".mp3"):
scriptPath = os.path.join(outputFolder, file.replace(".mp3", ".srt"))
audioPath = os.path.join(root, file)
result = transcription(audioPath, model)
with open(scriptPath, 'w') as f:
writer.write_result(result, f)
def main():
audioFolder = 'Audio'
outputFolder = 'output'
batchTranscription(audioFolder, outputFolder)
main()
关键:import whisper.utils