|
import os
|
|
import logging
|
|
from whisper import load_model
|
|
|
|
def transcribe_audio(audio_file):
|
|
"""Transcribes audio to text using Whisper."""
|
|
|
|
model = load_model("base")
|
|
|
|
|
|
try:
|
|
result = model.transcribe(audio_file)
|
|
return result['text']
|
|
except Exception as e:
|
|
logging.error(f'Error transcribing audio: {e}')
|
|
raise Exception('Failed to transcribe audio')
|
|
|
|
def save_transcription(transcription, title):
|
|
"""Saves the transcription to a text file."""
|
|
|
|
if not os.path.exists('transcriptions'):
|
|
os.makedirs('transcriptions')
|
|
|
|
|
|
transcription_file = os.path.join('transcriptions', f'{title}.txt')
|
|
with open(transcription_file, 'w', encoding='utf-8') as f:
|
|
f.write(transcription)
|
|
print(f'Transcription saved to: {transcription_file}')
|
|
|
|
if __name__ == "__main__":
|
|
|
|
audio_file = input("Enter the path to the audio file: ")
|
|
|
|
|
|
title = os.path.splitext(os.path.basename(audio_file))[0]
|
|
|
|
try:
|
|
transcription = transcribe_audio(audio_file)
|
|
print("Transcription:", transcription)
|
|
|
|
|
|
save_transcription(transcription, title)
|
|
except Exception as e:
|
|
logging.error(f'An error occurred: {e}')
|
|
|