summryai / transcribe_audio.py
Amitjadhav01's picture
AMIT's python files
b574243 verified
raw
history blame
1.55 kB
import os
import logging
from whisper import load_model
def transcribe_audio(audio_file):
"""Transcribes audio to text using Whisper."""
# Load Whisper model
model = load_model("base") # Change to desired model size
# Perform transcription
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."""
# Create transcription directory if it doesn't exist
if not os.path.exists('transcriptions'):
os.makedirs('transcriptions')
# Save the transcription to a text file
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__":
# Specify the path to the audio file
audio_file = input("Enter the path to the audio file: ")
# Extract title from the audio file name
title = os.path.splitext(os.path.basename(audio_file))[0]
try:
transcription = transcribe_audio(audio_file)
print("Transcription:", transcription)
# Save the transcription to a file
save_transcription(transcription, title)
except Exception as e:
logging.error(f'An error occurred: {e}')