声音文件中的UnicodeDecodeError [英] UnicodeDecodeError from sound file

查看:110
本文介绍了声音文件中的UnicodeDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google语音API在Python中制作语音识别器.我一直在此处(并转换为Python3)中使用和改编代码.我正在使用在线转换器将计算机上的音频文件从mp3转换为flac 16000 Hz(如原始代码中所指定).运行代码时出现此错误:

I'm trying to make a speech recogniser in Python using Google speech API. I've been using and adapting the code from here (converted to 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. 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

这是我的代码. (我确定还有某些东西在Python3中不起作用,因为我一直在尝试对其进行调整,这是urllib ...的新手.)

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...)

#!/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?

推荐答案

您需要以 binary模式打开文件:

open(filename, 'wb')

请注意'b',否则该文件将被视为文本并解码为Unicode.

Note the 'b', or the file will be treated as text and decoded to Unicode.

这篇关于声音文件中的UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆