简体   繁体   English

声音文件中的UnicodeDecodeError

[英]UnicodeDecodeError from sound file

I'm trying to make a speech recogniser in Python using Google speech API. 我正在尝试使用Google语音API在Python中制作语音识别器。 I've been using and adapting the code from here (converted to Python3). 我一直在使用并适应从代码这里 (转换成Python3)。 I'm using an audio file on my computer that's been converted from mp3 to flac 16000 Hz (as specified in the original code) using an online converter. 我正在使用在线转换器将计算机上的音频文件从mp3转换为flac 16000 Hz(如原始代码中所指定)。 When running the code I get this error: 运行代码时出现此错误:

$ python3 speech_api.py 02-29-2016_00-12_msg1.flac 
Traceback (most recent call last):
  File "speech_api.py", line 12, in <module>
    data = f.read()
  File "/usr/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 9: invalid start byte

This is my code. 这是我的代码。 (I'm sure there are also still things that don't work in Python3, as I've been trying to adapt it and am new to urllib ...) (我敢肯定还有一些东西在Python3中不起作用,因为我一直在努力适应它,并且是urllib新手……)

#!/usr/bin/python
import sys
from urllib.request import urlopen
import json
try:
    filename = sys.argv[1]
except IndexError:
    print('Usage: transcribe.py <file>')
    sys.exit(1)

with open(filename) as f:
    data = f.read()

req = urllib.request('https://www.google.com/intl/en/chrome/demos/speech.html', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'})

try:
    ret = urllib.urlopen(req)
except urllib.URLError:
    print("Error Transcribing Voicemail")
    sys.exit(1)

resp = ret.read()
text = json.loads(resp)['hypotheses'][0]['utterance']
print(text)

Any ideas what I could do? 有什么想法我可以做什么?

You need to open the file in binary mode : 您需要以二进制模式打开文件:

open(filename, 'wb')

Note the 'b' , or the file will be treated as text and decoded to Unicode. 注意'b' ,否则文件将被视为文本并解码为Unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM