文字->语音
from urllib.request import urlopen
from urllib.request import Request
from urllib.parse import urlencode
from urllib.parse import quote_plus
def fetch_token():
post_data = urlencode({
'grant_type': 'client_credentials',
'client_id': 'P7muFm3pmcId0vUs6VeH2Awp',
'client_secret': 'q1StvURZG1RAksAirdWEMT6BZSot9wNp'})
post_data = post_data.encode('utf-8')
req = Request('http://openapi.baidu.com/oauth/2.0/token', post_data)
result_str = urlopen(req, timeout=5).read()
result_str = result_str.decode()
import json
result = json.loads(result_str)
return result['access_token']
def createVoice(text):
params = {'tok': fetch_token(),
'tex': quote_plus(text),
'per': 1,
'spd': 5,
'pit': 5,
'vol': 5,
'aue': 3,
'cuid': "123456PYTHON",
'lan': 'zh',
'ctp': 1}
data = urlencode(params)
req = Request('http://tsn.baidu.com/text2audio',
data.encode('utf-8'))
voice = urlopen(req).read()
open('test.mp3', 'wb').write(voice)
def TimeList():
from mutagen.mp3 import MP3
return MP3('./test.mp3').info.length
if __name__ == "__main__":
createVoice('测试')
print(TimeList())
文字->拼音
import pypinyin
pinyin = pypinyin.pinyin(text, style=pypinyin.NORMAL)
|