【代码分享】Python语音合成demo
JoSeaboy 发布于2018-02 浏览:5406 回复:4
1
收藏

  随着人工智能技术的发展,一些重复性的工作可以交给系统来做,例如到期续费催缴就是一个应用场景,下面是一段用Python + 百度语音合成写一段demo。

# -*- coding:utf-8 -*-
import subprocess
import shlex
from aip import AipSpeech

# 定义常量
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 需要合成语音的文本内容
content = u'尊敬的客户您好,您在我司购买的某某业务将于2018年3月30日到期,请您及时续费。'

# 调用百度语音合成方法 
result = aipSpeech.synthesis(content, 'zh', 1, {'vol': 5,'per':4})

# 识别正确返回语音二进制, 错误则返回dict 参照Python SDK错误码
if not isinstance(result, dict):
    # 生成语音文件
    with open('e://test.wav', 'wb') as f:
        f.write(result)

# 如果将语音用于呼叫中心或其他应用,需要根据实际情况对音频进行调整
# 这里使用ffmpeg对音频进行调整 码率128kbps 单声道 采样率8000HZ
error = subprocess.call(['ffmpeg', '-i', 'e://test.wav', '-ab', '128k', '-ac', '1', '-ar', '8000', 'e://test1.wav'])

 

收藏
点赞
1
个赞
共4条回复 最后由用户已被禁言回复于2022-04
#5fghr767回复于2021-10

错误,无法执行

 

0
#4a554242113回复于2018-12

啊啊,已经解决,是ffmpeg没加入环境!

0
#3a554242113回复于2018-12

你好问下报错:

E:\anaconda\lib\subprocess.py in call(timeout, *popenargs, **kwargs)
265 retcode = call(["ls", "-l"])
266 """
--> 267 with Popen(*popenargs, **kwargs) as p:
268 try:
269 return p.wait(timeout=timeout)

E:\anaconda\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
707 c2pread, c2pwrite,
708 errread, errwrite,
--> 709 restore_signals, start_new_session)
710 except:
711 # Cleanup if the child failed starting.

E:\anaconda\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
995 env,
996 os.fspath(cwd) if cwd is not None else None,
--> 997 startupinfo)
998 finally:
999 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

得如何解决?

0
#2fujiayi1984回复于2018-03

感谢分享

0
TOP
切换版块