Skip to content

Instantly share code, notes, and snippets.

@410063005

410063005/tts.py Secret

Created January 5, 2019 14:24
Show Gist options
  • Save 410063005/7f464d8ca3300c7af43bfbb5af301323 to your computer and use it in GitHub Desktop.
Save 410063005/7f464d8ca3300c7af43bfbb5af301323 to your computer and use it in GitHub Desktop.
文本转音频
import requests
import urllib
def download(word, is_en=True):
url = 'https://sp0.baidu.com/-rM1hT4a2gU2pMbgoY3K/gettts?lan=en&text=%s&spd=2&source=alading' %urllib.parse.quote(word)
r = requests.get(url)
filename = word.replace(' ', '_')
if r.status_code == 200:
print('downloading %s' %word)
else:
print('failed download %s' %word)
with open(filename + '.mp3', 'wb') as fd:
for chunk in r.iter_content(1024):
fd.write(chunk)
if __name__ == '__main__':
with open('word.txt', 'r') as fd:
for line in fd:
if line.strip() == '':
continue
download(line.strip())
print('finish')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment