音频到文本的声音很慢,单词也被丢弃

时间:2019-03-08 13:30:49

标签: python-3.x ffmpeg speech-recognition speech-to-text google-speech-api

我有一个代码,可以从输入文件夹中获取视频,然后使用ffmpeg将其转换为音频文件(.wav)。 然后,它会通过录制30秒音频(dura = 30)并使用Google翻译API将其转换为文本,从而将音频文件转换为文本。

问题在于代码将视频转换为文本需要花费大量时间,并且每30秒就会丢弃前两个单词和一些单词。

import speech_recognition as sr
import sys
import shutil
from googletrans import Translator
from pathlib import Path
import os
import wave
def audio_to_text(self,video_lst,deploy_path,video_path,audio_path):
    try:
        txt_lst=[]
        for video_file in video_lst:
            file_part=video_file.split('.')
            audio_path_mod = audio_path +'/'+ '.'.join(file_part[:-1])
            dir_path=video_path+'.'.join(file_part[:-1])
            self.createDirectory(audio_path_mod)
            audio_file='.'.join(file_part[:-1])+'.wav'
            command_ffmpeg='set PATH=%PATH%;'+deploy_path.replace('config','script')+'audio_video/ffmpeg/bin/'
            command='ffmpeg -i '+video_path+'/'+video_file+' '+audio_path_mod+'/'+audio_file
            os.system(command_ffmpeg)
            os.system(command)
            r=sr.Recognizer()
            dura=30
            lang='en'
            wav_filename=audio_path_mod+'/'+audio_file

            f = wave.open(wav_filename, 'r')
            frames = f.getnframes()
            rate = f.getframerate()
            audio_duration = frames / float(rate)
            final_text_lst=[]
            counter=0

            with sr.AudioFile(wav_filename) as source:
                while counter<audio_duration:
                    audio=r.record(source,duration=dura)
                    counter+=dura
                    try:
                        str=r.recognize_google(audio)
                        final_text_lst.append(str)
                    except Exception as e:
                        print(e)
            print('Text data generated..')

            text_path=audio_path_mod+'/'+audio_file.replace('.wav','_audio_text.csv')
            with open(text_path, 'w') as f:
                f.write(' '.join(final_text_lst))

    except Exception as e:
        print(e)

任何帮助/建议都是有价值的。预先感谢。

0 个答案:

没有答案