Nov 13, 10:52pm

When using faster-whisper, the Python script you run does not automatically save the transcription to a file—it only prints the results to the console by default. To save the transcription as a text file, you need to modify the script slightly.


How to Save the Transcription to a Text File

1. Update the Script

Here’s a modified version of the script that saves the transcription to a .txt file:

python
from faster_whisper import WhisperModel

model_size = "medium"
model = WhisperModel(model_size, device="cpu", compute_type="int8")

segments, info = model.transcribe("audio_file.mp3", beam_size=5)

# Open a file to write the transcription
with open("transcription.txt", "w", encoding="utf-8") as f:
    for segment in segments:
        line = f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}\n"
        print(line.strip())  # Print to console
        f.write(line)  # Write to file

print("\nTranscription saved to 'transcription.txt'")

2. Run the Script

Save the script as transcribe.py and run it:

bash
python transcribe.py

3. Check the Output

  • The transcription will be printed to the console and saved to transcription.txt in the same directory.

Optional: Save as SRT (Subtitle File)

If you want subtitles (.srt format), use this script:

python
from faster_whisper import WhisperModel

model_size = "medium"
model = WhisperModel(model_size, device="cpu", compute_type="int8")

segments, info = model.transcribe("audio_file.mp3", beam_size=5)

with open("transcription.srt", "w", encoding="utf-8") as f:
    for i, segment in enumerate(segments, start=1):
        start_time = f"{int(segment.start // 3600):02d}:{int((segment.start % 3600) // 60):02d}:{int(segment.start % 60):02d}"
        end_time = f"{int(segment.end // 3600):02d}:{int((segment.end % 3600) // 60):02d}:{int(segment.end % 60):02d}"
        f.write(f"{i}\n{start_time},000 --> {end_time},000\n{segment.text}\n\n")

print("Transcription saved to 'transcription.srt'")

Troubleshooting

  • If you get an error about the file not being found, ensure audio_file.mp3 is in the same directory as the script.
  • If the script runs but the output is empty, check if the audio file is valid and contains speech.
Nov 13, 10:52pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more
Whisper Audio Transcription