[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] python调用Amazon Polly语音朗读小工具

转载自agentzh 的微博:

@agentzh
前两天抽空快速写了一组命令行小工具,可以调用 Amazon Polly 的文本到语音服务,自动生成较大篇幅的英文电子书的语音朗读 MP3 文件。
我把工具开源在了这个 amazon-polly-batch 的 GitHub 仓库里面:O网页链接 我原来都是自己朗读和录音的,但发现还是有些太累,不如让机器来读的好 [嘻嘻] ​​​​


https://github.com/agentzh/amazon-polly-batch
  1. #!/usr/bin/env python
  2. from boto3 import Session
  3. from botocore.exceptions import BotoCoreError, ClientError
  4. from contextlib import closing
  5. import argparse
  6. import os
  7. import sys
  8. import subprocess
  9. from tempfile import gettempdir
  10. parser = argparse.ArgumentParser(description='Process some integers.')
  11. parser.add_argument('-o', metavar='MP3-FILE', type=str, default="a.mp3",
  12.                     help='the output .mp3 file name')
  13. parser.add_argument('--voice', metavar='VOICE', default="Salli",
  14.                     help='the AWS Polly voice name. default to Salli')
  15. parser.add_argument('infile', metavar='SSML-FILE', type=str,
  16.                     help='the SSML input file')
  17. args = parser.parse_args()
  18. outfile = args.o
  19. # Create a client using the credentials and region defined in the [adminuser]
  20. # section of the AWS credentials file (~/.aws/credentials).
  21. session = Session() #profile_name="adminuser")
  22. polly = session.client("polly")
  23. voice = args.voice or "Salli"
  24. #voice = "Joanna"
  25. infile = args.infile
  26. index = 1
  27. pieces = []
  28. with open(infile, "rb") as f:
  29.     pieces = [l for l in (line.strip() for line in f) if l]
  30. with open(outfile, "wb") as out:
  31.     i = index
  32.     for piece in pieces:
  33.         print "piece %d: %s" % (i, piece)
  34.         try:
  35.             # Request speech synthesis
  36.             response = polly.synthesize_speech(Text=piece, TextType="ssml", OutputFormat="mp3",
  37.                  VoiceId=voice)
  38.         except (BotoCoreError, ClientError) as error:
  39.             # The service returned an error, exit gracefully
  40.             print(error)
  41.             sys.exit(-1)
  42.         # Access the audio stream from the response
  43.         if "AudioStream" in response:
  44.             # Note: Closing the stream is important as the service throttles on the
  45.             # number of parallel connections. Here we are using contextlib.closing to
  46.             # ensure the close method of the stream object will be called automatically
  47.             # at the end of the with statement's scope.
  48.             with closing(response["AudioStream"]) as stream:
  49.                 try:
  50.                     # Open a file for writing the output as a binary stream
  51.                     out.write(stream.read())
  52.                 except IOError as error:
  53.                     # Could not write to file, exit gracefully
  54.                     print(error)
  55.                     sys.exit(-1)
  56.         else:
  57.                # The response didn't contain audio data, exit gracefully
  58.             print("Could not stream audio")
  59.             sys.exit(-1)
  60.         i = i + 1
  61.         # Play the audio using the platform's default player
  62.         # the following works on Mac and Linux. (Darwin = mac, xdg-open = linux).
  63.         #opener = "open" if sys.platform == "darwin" else "xdg-open"
  64.         #subprocess.call([opener, output])
复制代码
1

评分人数

去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

返回列表