转载自agentzh 的微博:
@agentzh
前两天抽空快速写了一组命令行小工具,可以调用 Amazon Polly 的文本到语音服务,自动生成较大篇幅的英文电子书的语音朗读 MP3 文件。
我把工具开源在了这个 amazon-polly-batch 的 GitHub 仓库里面:O网页链接 我原来都是自己朗读和录音的,但发现还是有些太累,不如让机器来读的好 [嘻嘻]
https://github.com/agentzh/amazon-polly-batch | | | | | from boto3 import Session | | from botocore.exceptions import BotoCoreError, ClientError | | from contextlib import closing | | import argparse | | import os | | import sys | | | | import subprocess | | from tempfile import gettempdir | | | | parser = argparse.ArgumentParser(description='Process some integers.') | | parser.add_argument('-o', metavar='MP3-FILE', type=str, default="a.mp3", | | help='the output .mp3 file name') | | parser.add_argument('--voice', metavar='VOICE', default="Salli", | | help='the AWS Polly voice name. default to Salli') | | parser.add_argument('infile', metavar='SSML-FILE', type=str, | | help='the SSML input file') | | | | args = parser.parse_args() | | | | outfile = args.o | | | | | | | | session = Session() | | polly = session.client("polly") | | | | voice = args.voice or "Salli" | | | | | | infile = args.infile | | index = 1 | | | | pieces = [] | | with open(infile, "rb") as f: | | pieces = [l for l in (line.strip() for line in f) if l] | | | | with open(outfile, "wb") as out: | | i = index | | for piece in pieces: | | print "piece %d: %s" % (i, piece) | | try: | | | | response = polly.synthesize_speech(Text=piece, TextType="ssml", OutputFormat="mp3", | | VoiceId=voice) | | except (BotoCoreError, ClientError) as error: | | | | print(error) | | sys.exit(-1) | | | | | | if "AudioStream" in response: | | | | | | | | | | with closing(response["AudioStream"]) as stream: | | try: | | | | out.write(stream.read()) | | except IOError as error: | | | | print(error) | | sys.exit(-1) | | | | else: | | | | print("Could not stream audio") | | sys.exit(-1) | | | | i = i + 1 | | | | | | | | | | COPY |
|