1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS Text to Speech

Posted at

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)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?