【问题标题】:MP3 to FLAC for Google's Speech API用于 Google 语音 API 的 MP3 到 FLAC
【发布时间】:2016-01-01 17:36:35
【问题描述】:

我正在尝试寻找一种简单的方法将 MP3 发送到 Google 以进行语音识别。目前,我正在使用一个子进程来调用 SoX,它将其转换为 WAV。然后,使用SpeechRecognition,它再次将其转换为 FLAC。理想情况下,我想要一种更便携(不是特定于操作系统)的方式来解码 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

SR's code for WAV to FLAC

编辑

我正在考虑以类似于 FLAC 二进制文件的方式分发 SoX,如果 SoX 的许可证允许的话,每个操作系统都有一个...

第二个想法,软件许可证令人困惑,我不想搞砸。

【问题讨论】:

  • 我更关心谷歌禁止你的用户每天超过 50 个请求而不是 sox 许可证。
  • @NikolayShmyrev 我的意思是,这是他们的问题。我认为那将是 IP 禁令。我将提出一个免责声明,但根据 Google 搜索,该 API 密钥在其他几个地方使用。

标签: python speech-recognition


【解决方案1】:

我决定这样做:

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,但 应该 可以在所有操作系统上运行并且没有任何中间文件。不过我只在 Linux 上测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2017-03-07
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多