-
Notifications
You must be signed in to change notification settings - Fork 1
/
zerotranscriber.py
55 lines (44 loc) · 1.72 KB
/
zerotranscriber.py
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
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
"""ZeroTranscriber.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1fCkOTuFS4htPXQyQQRV4ZnX0I7ibuqf_
"""
pip install pydub
pip install ffmpeg-python
pip install git+https://github.com/openai/whisper.git
# Old Code
import whisper
from pydub import AudioSegment
import os
def convert_to_wav(audio_file):
# Convert audio file to WAV format
sound = AudioSegment.from_file(audio_file)
wav_file = os.path.splitext(audio_file)[0] + ".wav"
sound.export(wav_file, format="wav")
return wav_file
def transcribe_audio_to_english(audio_file):
try:
model = whisper.load_model("large-v3")
result = model.transcribe(audio_file, fp16=False, task='translate', verbose=True)
return result["text"]
except Exception as e:
print(f"Error transcribing {audio_file}: {str(e)}")
return None
if __name__ == "__main__":
audio_file = input("Enter the path of the audio file: ")
if os.path.exists(audio_file):
if audio_file.lower().endswith(('.mp3', '.wav', '.ogg')):
if audio_file.lower().endswith('.mp3'):
audio_file = convert_to_wav(audio_file)
transcription = transcribe_audio_to_english(audio_file)
if transcription:
print(f"Transcription for {audio_file}: {transcription}")
else:
print(f"Failed to transcribe {audio_file}.")
if audio_file.lower().endswith('.wav'):
os.remove(audio_file) # Remove temporary WAV file
else:
print(f"Unsupported audio format: {audio_file}")
else:
print("File not found. Please provide a valid file path.")