baidu_voice.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:WeixinBot 作者: agenthun 项目源码 文件源码
def use_cloud(self, sourcefile_path, token):
        # sound = AudioSegment.from_mp3("big.mp3")
        # sound.export("/output", format="wav")
        fp = wave.open(sourcefile_path, 'rb')
        nf = fp.getnframes()
        f_len = nf * 2
        audio_data = fp.readframes(nf)

        cuid = "xxxxxxxxxx"  # my xiaomi phone MAC
        srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token
        http_header = [
            'Content-Type: audio/pcm; rate=8000',
            'Content-Length: %d' % f_len
        ]

        c = pycurl.Curl()
        c.setopt(pycurl.URL, str(srv_url))  # curl doesn't support unicode
        # c.setopt(c.RETURNTRANSFER, 1)
        c.setopt(c.HTTPHEADER, http_header)  # must be list, not dict
        c.setopt(c.POST, 1)
        c.setopt(c.CONNECTTIMEOUT, 30)
        c.setopt(c.TIMEOUT, 30)
        c.setopt(c.WRITEFUNCTION, self.dump_res)
        c.setopt(c.POSTFIELDS, audio_data)
        c.setopt(c.POSTFIELDSIZE, f_len)
        c.perform()  # pycurl.perform() has no return val
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号