Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

AWS Text to Speech

More than 1 year has passed since last update.

AWS喋らせよう!(備忘録 貧乏録)

#!/usr/local/bin/python3.6
# AWS
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os,sys,subprocess

session = Session(region_name="us-west-2")
polly = session.client("polly")
ttsdt=[
    ('Ivy'   ,"Thank you for calling. I'm sorry but I can't answer your call right now. Please leave a message at  the tone . I'll get back to you as soon as possible."),
    ('Joey'  ,"Thank you for calling. I'm sorry but I can't answer your call right now. Please leave a message at  the tone . I'll get back to you as soon as possible."),
    ('Mizuki',"お電話ありがとうございます。ただいま、電話に出ることができません。ピーッという音が鳴りましたら[発信音の後に]、メッセージをお願いします。こちらからすぐにお電話いたします。"),
    ('Takumi',"お電話ありがとうございます。ただいま、電話に出ることができません。ピーッという音が鳴りましたら[発信音の後に]、メッセージをお願いします。こちらからすぐにお電話いたします。")
]
for id,tx in ttsdt:
    print(tx)
    try:
        response = polly.synthesize_speech(Text=tx, OutputFormat="mp3", VoiceId=id)
    except (BotoCoreError, ClientError) as error:
        print(error)
        sys.exit(-1)
    if "AudioStream" in response:
        with closing(response["AudioStream"]) as stream:
            output = id+".mp3"
            try:
                with open(output, "wb") as file:
                    file.write(stream.read())
            except IOError as error:
                print(error)
                sys.exit(-1)
            print("synthesize_speech OK ->>" + output)
            subprocess.Popen(['mpg123', '-q',output ]).wait()
    else:
        print("Could not stream audio")
        sys.exit(-1)

hiratarich
プログラマ
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away