Skip to content

Instantly share code, notes, and snippets.

@kirshiyin89
Created May 1, 2022 20:13
Show Gist options
  • Save kirshiyin89/7cc60cfcd91a7956341fd0523deb1e86 to your computer and use it in GitHub Desktop.
Save kirshiyin89/7cc60cfcd91a7956341fd0523deb1e86 to your computer and use it in GitHub Desktop.
Transcribe audio
speech_recognizer = sr.Recognizer()
tokenizer = RegexpTokenizer(r'\w+')
def transcribe_audio(path):
audio = AudioSegment.from_wav(path)
file_name = os.path.basename(path)
chunks = split_on_silence(audio,
min_silence_len = 600,
silence_thresh = audio.dBFS-14,
keep_silence=600,
)
audio_folder = "audio-chunks"
if not os.path.isdir(audio_folder):
os.mkdir(audio_folder)
audio_text = ""
for i, audio_chunk in enumerate(chunks, start=1):
chunk_filename = os.path.join(audio_folder, f"{file_name}{i}")
audio_chunk.export(chunk_filename, format="wav")
with sr.AudioFile(chunk_filename) as source:
audio_file = speech_recognizer.record(source)
try:
text = speech_recognizer.recognize_google(audio_file)
except sr.UnknownValueError as e:
print("An error has occurred while recognising audio file:", e)
else:
text = f"{text.capitalize()}. "
print(chunk_filename, ":", text)
audio_text += text
os.remove(chunk_filename)
return audio_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment