Project :视频转文本
Introduction:通过使用FFmpeg-Python库,将视频的音频抽取出来存放至本地文件夹,再调用
百度语音识别REST Api,将音频转化文字,该Api可识别英语和普通话
Attention :上传的视频不能超过60s.同时需要主机联网才可调用云Api
Quickstart :
1.Download Anaconda and Install it
https://www.anaconda.com/distribution/
2.Open the Terminal,and input this command:
conda env create -f bat_video.yaml
3. python main.py
import os
import ffmpeg
from aip import AipSpeech
# 百度语音识别REST-Api
APP_ID = '176xxxx'
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 输入视频路径
video_path = os.path.abspath('./Raw_Video')
text_path = os.path.abspath('./Output_Text')
radio_path = os.path.abspath('./Raw_Radio')
files= os.listdir(video_path)
# 主函数
def main():
for file in files:
filename = os.path.splitext(str(file))[0]
# ffmpeg视频抽取音频
in_stream = ffmpeg.input(video_path+'/'+str(file))
(in_stream
.output(radio_path+'/'+filename+'.pcm',format='s16le', acodec='pcm_s16le', ac=1, ar='16k')
.run()
)
# 识别本地文件
test=client.asr(get_file_content(radio_path+'/'+filename+'.pcm'), 'pcm', 16000, {
'dev_pid': 1537,
})
# 内容写入文本并保存
txt_name = text_path+'/'+filename+'.txt'
f = open(txt_name, 'w')
f.write(str(test['result']))
f.close()
# 读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
if __name__=='__main__':
main()