安裝
如果已經安裝了pip,執行 pip install baidu-aip 即可。
如果已安裝setuptools,執行 python setup.py install 即可。
登錄百度ia網站:
用百度賬號登錄
進入左側語言應用
創建新應用



from aip import AipSpeech app_id = "16078218" # 寫注冊百度語言的賬號 api_key = "mc4fLpCV5um1Lta7uZGxhayn" # 寫注冊的api的密鑰 secret_key = "CDD41zKslm1uEdUFrMlTanaBFwG41kAB" # 寫注冊的secret的密鑰 def speech(request): if request.method == "GET": """把文本轉為語音,返回mp3格式的音頻文件""" client = AipSpeech(app_id, api_key, secret_key) # synthesis參數: 文本,語言zh(中文),1為pc端,語音{"vol":音量,"spd":語速,"pit":語調,"per":聲道(0:女,1:男,2:逍遙音,3:小蘿莉)} chapter_audio = client.synthesis("蠢逼", "zh", 1, { "vol": 5, "spd": 4, "pit": 5, "per": 0 }) with open("media/xx.mp3",'wb') as f: # 生成帶有上述 "蠢逼" 的mp3文件 f.write(chapter_audio) return render(request, 'yuyin.html',locals())
前端識別語音進行播放
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> # 連接mp3文件,自動語音播放 <audio src="/media/xx.mp3/" controls="controls" style="height: 20px;width: 180px;"></audio> </body> </html>
后台文件
