我已经建立了一个小型的烧瓶服务器来处理请求。我要获取的api函数中有3个参数。它们是type
,user_id
,audio_file
,一个是文件。由于它用于音频文件转换。我必须获取一个文件。我已经在Postman音频文件中对此进行了测试,但保存了该文件,但api函数中的subprocess.call(command)无法正常工作。
这是烧瓶服务器代码
@app.route('/voice',methods=['GET','POST'])
def process_audio():
try:
if request.method == 'POST':
input_type = request.form['type']
user_id = request.form['user_id']
static_file = request.files['audio_file']
audio_name = secure_filename(static_file.filename)
path = 't/'
status = static_file.save(path + audio_name)
full_file = path + audio_name
if input_type == 1:
cmd = "python t/convert_english.py --audio " + full_file
res = subprocess.call([cmd],shell=True)
f = open('t/ress.txt','w')
f.write(str(res))
f.close()
return "true"
else:
cmd = "python t/convert_sinhala.py --audio " + full_file
os.system(cmd)
return "true"
else:
return "false"
except Exception as e:
print(e)
logger.error(e)
return e
音频文件将按预期保存在目录中。
这是convert_english.py
import subprocess
import argparse
import os
import logging
import speech_recognition as sr
from tqdm import tqdm
from multiprocessing.dummy import Pool
#subprocess.call('pip install pydub',shell=True)
from os import path
from pydub import AudioSegment
logging.basicConfig(filename='/var/www/img-p/t/ee.log',level=logging.DEBUG,format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
ap = argparse.ArgumentParser()
ap.add_argument("-a","--audio",required=True,help="path to input audio file")
args = vars(ap.parse_args())
src = args["audio"]
dst = "audio.wav"
sound = AudioSegment.from_mp3(src)
sound.export(dst,format="wav")
#subprocess.call('pip install ffmpeg-python',shell=True)
subprocess.call('mkdir parts',shell=True)
subprocess.call('ffmpeg -i audio.wav -f segment -segment_time 30 -c copy parts/out%09d.wav',shell=True)
#subprocess.call('pip install SpeechRecognition',shell=True)
pool = Pool(8) # Number of concurrent threads
with open("api-key.json") as f:
GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read()
r = sr.Recognizer()
files = sorted(os.listdir('parts/'))
def transcribe(data):
idx,file = data
name = "parts/" + file
print(name + " started")
# Load audio file
with sr.AudioFile(name) as source:
audio = r.record(source)
# Transcribe audio file
text = r.recognize_google_cloud(audio,credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)
print(name + " done")
return {
"idx": idx,"text": text
}
all_text = pool.map(transcribe,enumerate(files))
pool.close()
pool.join()
transcript = ""
for t in sorted(all_text,key=lambda x: x['idx']):
total_seconds = t['idx'] * 30
# Cool shortcut from:
# https://stackoverflow.com/questions/775049/python-time-seconds-to-hms
# to get hours,minutes and seconds
m,s = divmod(total_seconds,60)
h,m = divmod(m,60)
# Format time as h:m:s - 30 seconds of text
transcript = transcript + "{:0>2d}:{:0>2d}:{:0>2d} {}\n".format(h,m,s,t['text'])
print(transcript)
with open("transcript.txt","w") as f:
f.write(transcript)
f = open("transcript.txt")
lines = f.readlines()
f.close()
f = open("transcript.txt","w",encoding="utf-8")
for line in lines:
f.write(line[8:])
f.close()
当我在终端中手动运行命令-> python t / convert_english.py --audio t / tttttttttt.mp3时,上述代码起作用了。
但是当我尝试从flask服务器本身运行时,它不起作用。而且我也没有收到错误。