-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_to_text.py
40 lines (34 loc) · 1.36 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
import speech_recognition as sr
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
from moviepy.editor import VideoFileClip
import shutil
def convert_audio_to_text(video_file_name_id):
r = sr.Recognizer()
sound = AudioSegment.from_wav(video_file_name_id)
# Split audio where silence is 700ms or greater and get chunks
chunks = split_on_silence(sound, min_silence_len=700, silence_thresh=sound.dBFS-14, keep_silence=700)
# Create folder to store audio chunks
folder_name = "audio-chunks"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
# Process each chunk
for i, audio_chunk in enumerate(chunks, start=1):
# Export chunk and save in folder
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
# Recognize chunk
with sr.AudioFile(chunk_filename) as source:
audio_listened = r.record(source)
# Convert to text
try:
text = r.recognize_google(audio_listened)
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}. "
# print(chunk_filename, ":", text)
whole_text += text
return whole_text