-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudio_to_text.py
57 lines (44 loc) · 1.62 KB
/
audio_to_text.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
55
56
57
from pytube import YouTube
import whisper
import os
def audio_to_text(link):
try:
# link='https://youtu.be/Wn9iALMyS7c?si=q0LYZBrR9mSbj2b7'
video = YouTube(link)
# video.set_filename('audio_file')
stream = video.streams.filter(only_audio=True).first()
stream.download(filename=f"audiofile.mp3")
print("audio file generated")
model = whisper.load_model("tiny")
audio = whisper.load_audio("audiofile.mp3")
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")
if (max(probs, key=probs.get) !='en'):
print("Can't perform ASR on other languages.")
return("Can't perform ASR on other languages.")
else:
print("performing..")
result = model.transcribe("audiofile.mp3")
# time.sleep(2)
# result = model.transcribe("audiofile.mp3",fp16=False)
final_data=""
with open("transcript.txt","w") as f:
final_data+=result["text"]
f.write(result["text"])
# os.remove('./audiofile.mp3')
print("done")
return('1')
except:
print("Couldn't find audio")
return("Couldn't find audio")
# except:
# print("ASR error")
# return("ASR error")
# return("hieie")
# text = open("transcript.txt",'w')
# text.write(clean_data)
# text.close()
# return("hiiiiiiiiii")
# audio_to_text()