Python:无法将wav转换为flac

时间:2018-05-26 11:36:10

标签: python python-3.x wav file-conversion flac

我想将音频(.wav)文件转换为文本。我通过搜索找到了这个Repo,但它是用Python 2.x编写的。我将语法转换为3.x

 #!/usr/bin/python

import sys
import urllib.request, urllib.error, urllib.parse
import os
import json
import subprocess as sp

url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
fileName = str(sys.argv[1])
fileExtension = os.path.splitext(fileName)[1]
converted = False

if fileExtension != ".flac":
    fnull = open(os.devnull, 'w')
    sp.call("pacpl --overwrite -t flac " + fileName, shell = True, stdout = fnull, stderr = fnull)
    fnull.close()
    fileName = os.path.splitext(fileName)[0] + '.flac'
    converted = True

try:
    binary_audio = open(fileName, 'rb')
except:
    print("Failed to get binary data.")

size_of_audio = os.path.getsize(fileName)

if converted:
    os.remove(fileName)

request = urllib.request.Request(url)
request.add_header('Content-type','audio/x-flac; rate=16000')
request.add_header('Content-length', str(size_of_audio))
request.add_data(binary_audio)

try:
    response = urllib.request.urlopen(request)
except urllib.error.URLError as e:
    print("Unable to connect")
except urllib.error.HTTPError as e:
    print("Oops, bad request")

content = response.read()

data = json.loads(content)

print(data["hypotheses"][0]["utterance"])

但是当我运行脚本时,我收到了这个错误:

$ python speech.py /Users/amin/AnacondaProjects/audio-transcribe/softkitty.wav
  

无法获取二进制数据。 Traceback(最近一次调用最后一次):文件   " speech.py​​",第26行,in       size_of_audio = os.path.getsize(fileName)File" /anaconda3/lib/python3.6/genericpath.py" ;,第50行,在getsize中       return os.stat(filename).st_size FileNotFoundError:[Errno 2]没有这样的文件或目录:   ' /Users/amin/AnacondaProjects/audio-transcribe/softkitty.flac'

0 个答案:

没有答案