Audio Transcription Tool Python, AI
👤 Sharing: AI
```python
import speech_recognition as sr
import os
def transcribe_audio(audio_file_path):
"""
Transcribes an audio file using Google Web Speech API.
Args:
audio_file_path (str): The path to the audio file.
Returns:
str: The transcribed text, or None if an error occurred.
"""
r = sr.Recognizer()
with sr.AudioFile(audio_file_path) as source:
audio = r.record(source) # Read the entire audio file.
try:
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return None
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
return None
def transcribe_audio_using_whisper(audio_file_path, model_name="base"): # added whisper implementation
"""
Transcribes an audio file using OpenAI's Whisper model.
Args:
audio_file_path (str): The path to the audio file.
model_name (str): The name of the Whisper model to use (e.g., "base", "small", "medium", "large").
Returns:
str: The transcribed text, or None if an error occurred.
"""
try:
import whisper
except ImportError:
print("Please install the 'whisper' library: pip install openai-whisper")
return None
try:
model = whisper.load_model(model_name)
result = model.transcribe(audio_file_path)
return result["text"]
except Exception as e:
print(f"Error during Whisper transcription: {e}")
return None
def main():
"""
Main function to handle audio transcription.
"""
audio_file = input("Enter the path to the audio file: ")
if not os.path.exists(audio_file):
print("Error: Audio file not found.")
return
transcription_method = input("Choose transcription method (google/whisper): ").lower()
if transcription_method == "google":
transcribed_text = transcribe_audio(audio_file)
elif transcription_method == "whisper":
model_size = input("Enter Whisper model size (base, small, medium, large): ").lower()
transcribed_text = transcribe_audio_using_whisper(audio_file, model_size)
else:
print("Invalid transcription method. Choose google or whisper.")
return
if transcribed_text:
print("\nTranscription:")
print(transcribed_text)
# Optionally save the transcription to a file:
save_to_file = input("Save transcription to file? (y/n): ").lower()
if save_to_file == 'y':
output_file = input("Enter the output file name: ")
try:
with open(output_file, "w") as f:
f.write(transcribed_text)
print(f"Transcription saved to {output_file}")
except Exception as e:
print(f"Error saving to file: {e}")
else:
print("Transcription failed.")
if __name__ == "__main__":
main()
```
👁️ Viewed: 8
Comments