Google 的演讲 API 的 MP3 到 FLAC
MP3 to FLAC for Google's Speech API
我正在尝试找到一种简单的方法来将 MP3 发送到 Google 以进行语音识别。目前,我正在使用子进程调用 SoX,将其转换为 WAV。然后,使用 SpeechRecognition,它再次将其转换为 FLAC。理想情况下,我想要一种更便携(不是 OS 特定)的方式来解码 MP3 并在不保存中间文件等的情况下发送它。
这是我目前拥有的:
import speech_recognition as sr
import subprocess
import requests
audio = requests.get('http://somesite.com/some.mp3')
with open('/tmp/audio.mp3', 'wb') as file:
file.write(audio.content)
subprocess.run(['sox', '/tmp/audio.mp3', '/tmp/audio.wav'])
r = sr.Recognizer()
with sr.WavFile('/tmp/audio.wav') as source:
audio = r.record(source)
result = r.recognize_google(audio)
del r
我试过直接使用 SpeechRecognition 中包含的 FLAC 二进制文件,但输出只是静态的。我不太热衷于在 Git 上分发二进制文件,但如果这是唯一的方法,我会的。
一些重要链接:
SR's code for speech recognition
编辑
我正在考虑以一种类似于 FLAC 二进制文件的方式分发 SoX,每个文件一个 OS,如果 SoX 的许可证允许的话...
转念一想,软件许可证令人困惑,我不想惹它。
我决定这样做:
import subprocess
import requests
import shutil
import glob
import json
audio = requests.get('http://somesite.com/some.mp3')
sox = shutil.which('sox') or glob.glob('C:\Program Files*\sox*\sox.exe')[0]
p = subprocess.Popen(sox + ' -t mp3 - -t flac - rate 16k', stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell = True)
stdout, stderr = p.communicate(audio.content)
url = 'http://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-US&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'
headers = {'Content-Type': 'audio/x-flac; rate=16000'}
response = requests.post(url, data = stdout, headers = headers).text
result = None
for line in response.split('\n'):
try:
result = json.loads(line)['result'][0]['alternative'][0]['transcript']
break
except:
pass
我想这更像是一个中间立场,我想从 SR 模块借用一些东西。它需要用户安装 SoX,但是 应该 可以在所有 OS 上工作并且没有任何中间文件。然而,我只在 Linux 上测试过。
我正在尝试找到一种简单的方法来将 MP3 发送到 Google 以进行语音识别。目前,我正在使用子进程调用 SoX,将其转换为 WAV。然后,使用 SpeechRecognition,它再次将其转换为 FLAC。理想情况下,我想要一种更便携(不是 OS 特定)的方式来解码 MP3 并在不保存中间文件等的情况下发送它。
这是我目前拥有的:
import speech_recognition as sr
import subprocess
import requests
audio = requests.get('http://somesite.com/some.mp3')
with open('/tmp/audio.mp3', 'wb') as file:
file.write(audio.content)
subprocess.run(['sox', '/tmp/audio.mp3', '/tmp/audio.wav'])
r = sr.Recognizer()
with sr.WavFile('/tmp/audio.wav') as source:
audio = r.record(source)
result = r.recognize_google(audio)
del r
我试过直接使用 SpeechRecognition 中包含的 FLAC 二进制文件,但输出只是静态的。我不太热衷于在 Git 上分发二进制文件,但如果这是唯一的方法,我会的。
一些重要链接:
SR's code for speech recognition
编辑
我正在考虑以一种类似于 FLAC 二进制文件的方式分发 SoX,每个文件一个 OS,如果 SoX 的许可证允许的话...
转念一想,软件许可证令人困惑,我不想惹它。
我决定这样做:
import subprocess
import requests
import shutil
import glob
import json
audio = requests.get('http://somesite.com/some.mp3')
sox = shutil.which('sox') or glob.glob('C:\Program Files*\sox*\sox.exe')[0]
p = subprocess.Popen(sox + ' -t mp3 - -t flac - rate 16k', stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell = True)
stdout, stderr = p.communicate(audio.content)
url = 'http://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-US&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'
headers = {'Content-Type': 'audio/x-flac; rate=16000'}
response = requests.post(url, data = stdout, headers = headers).text
result = None
for line in response.split('\n'):
try:
result = json.loads(line)['result'][0]['alternative'][0]['transcript']
break
except:
pass
我想这更像是一个中间立场,我想从 SR 模块借用一些东西。它需要用户安装 SoX,但是 应该 可以在所有 OS 上工作并且没有任何中间文件。然而,我只在 Linux 上测试过。