File size: 1,549 Bytes
b574243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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}')