0
I need to transcribe a 21 minute audio using speech_recognition and recognize_google the problem is that the api does not allow any audio size, I do not know exactly which but it is greater than 30 seconds so I created the function:
with contextlib.closing(wave.open(fname, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
splitaudio = duration / 30
blocos = int(round(splitaudio, 0) + 1)
That returns me 43 blocks.
That employee code to follow:
with sr.AudioFile(AUDIO_FILE) as source:
audio1 = r.record(source, duration=30)
audio2 = r.record(source, duration=30)
audio3 = r.record(source, duration=30)
audio4 = r.record(source, duration=30)
audio5 = r.record(source, duration=30)
audio6 = r.record(source, duration=30)
audio7 = r.record(source, duration=30)
audio8 = r.record(source, duration=30)
#... até atingir o valor de blocos que tenho ... #
speech1 = ("Transcription: " + r.recognize_google(audio1, language='pt-br') + '\n')
speech2 = ("Transcription: " + r.recognize_google(audio2, language='pt-br') + '\n')
speech3 = ("Transcription: " + r.recognize_google(audio3, language='pt-br') + '\n')
speech4 = ("Transcription: " + r.recognize_google(audio4, language='pt-br') + '\n')
speech5 = ("Transcription: " + r.recognize_google(audio5, language='pt-br') + '\n')
speech6 = ("Transcription: " + r.recognize_google(audio6, language='pt-br') + '\n')
speech7= ("Transcription: " + r.recognize_google(audio7, language='pt-br') + '\n')
speech8 = ("Transcription: " + r.recognize_google(audio8, language='pt-br') + '\n')
#até completar
with open('frases/frases.csv', 'a') as writefile:
writefile.write(speech1)
writefile.write(speech2)
writefile.write(speech3)
writefile.write(speech4)
writefile.write(speech5)
writefile.write(speech6)
writefile.write(speech7)
#... até completar
However I need to keep creating many variables and this code would only be useful for this audio, any idea how to improve this code creating these variables dynamically ? Or some other approach not to be necessary to type so much and make the code cleaner ?
****** EDIT ******
In that part :
with sr.AudioFile(AUDIO_FILE) as source:
audio1 = r.record(source, duration=30)
audio2 = r.record(source, duration=30)
...
The audio1 variable records the first 30 seconds of audio, the audio2 variable from 30 to 60 and so on.
Study about Python lists.
– Woss
By the question, I believe you intend to write in a file, so write the list values in the file.
– Woss
When inside with : audio1 = r.record(source, Duration=30) records the first 30 seconds. audio2 = r.record(source, Duration=30) records from 30 to 60 and so on so I need to create these variables dynamically, if it weren’t for that I could simply . append as you might be suggesting.
– Filipe Gonçalves