You've already forked PythonLib
mirror of
https://github.com/lifebottle/PythonLib.git
synced 2026-02-13 15:25:50 -08:00
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
# pip install SpeechRecognition pydub
|
|
import speech_recognition as sr
|
|
import os
|
|
import argparse
|
|
from pydub import AudioSegment
|
|
from pydub.silence import split_on_silence
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="A utility to extract transcribe audio files to Japanese. To use this: python wav_to_txt.py file.wav")
|
|
parser.add_argument(
|
|
"file",
|
|
help="Path to audio file that needs be to transcribed to Japanese",
|
|
type=str,
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
path = args.file
|
|
print("\nFull Text:\n", get_large_audio_transcription_on_silence(path))
|
|
|
|
# create a speech recognition object
|
|
r = sr.Recognizer()
|
|
|
|
# a function to recognize speech in the audio file
|
|
# so that we don't repeat ourselves in in other functions
|
|
def transcribe_audio(path):
|
|
# use the audio file as the audio source
|
|
with sr.AudioFile(path) as source:
|
|
audio_listened = r.record(source)
|
|
# try converting it to text
|
|
text = r.recognize_google(audio_listened, language='ja-JP')
|
|
return text
|
|
|
|
# a function that splits the audio file into chunks on silence
|
|
# and applies speech recognition
|
|
def get_large_audio_transcription_on_silence(path):
|
|
"""Splitting the large audio file into chunks
|
|
and apply speech recognition on each of these chunks"""
|
|
# open the audio file using pydub
|
|
sound = AudioSegment.from_file(path)
|
|
# split audio sound where silence is 500 miliseconds or more and get chunks
|
|
chunks = split_on_silence(sound,
|
|
# experiment with this value for your target audio file
|
|
min_silence_len = 500,
|
|
# adjust this per requirement
|
|
silence_thresh = sound.dBFS-14,
|
|
# keep the silence for 1 second, adjustable as well
|
|
keep_silence=500,
|
|
)
|
|
folder_name = "audio-chunks"
|
|
# create a directory to store the audio chunks
|
|
if not os.path.isdir(folder_name):
|
|
os.mkdir(folder_name)
|
|
f = open("whole_text.txt", "a", encoding="utf8")
|
|
whole_text = ""
|
|
# process each chunk
|
|
for i, audio_chunk in enumerate(chunks, start=1):
|
|
# export audio chunk and save it in
|
|
# the `folder_name` directory.
|
|
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
|
|
audio_chunk.export(chunk_filename, format="wav")
|
|
# recognize the chunk
|
|
try:
|
|
text = transcribe_audio(chunk_filename)
|
|
except sr.UnknownValueError as e:
|
|
print("Error:", str(e))
|
|
else:
|
|
text = f"{text.capitalize()}"
|
|
print(chunk_filename, ":", text)
|
|
f.write(text + "\n")
|
|
whole_text+=text + "\n"
|
|
# return the text for all chunks detected
|
|
f.close()
|
|
return whole_text
|
|
|
|
if __name__ == '__main__':
|
|
main()
|