3
2

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.

PollyをAWS SDK for Pythonからコールする

Last updated at Posted at 2016-12-01

boto3の更新

boto3のVersion1.4.1以降でなければPollyに対応していません。
必ずアップデートしてから実行しましょう。

$ pip install boto3 --upgrade

古いバージョンだとこういうエラーが出ます。

known_service_names=', '.join(sorted(known_services)))
botocore.exceptions.UnknownServiceError: Unknown service: 'polly'. Valid service names are: acm, apigateway, application-autoscaling, autoscaling, budgets, cloudformation, cloudfront, cloudhsm, cloudsearch, cloudsearchdomain, cloudtrail, cloudwatch, codecommit, codedeploy, codepipeline, cognito-identity, cognito-idp, cognito-sync, config, datapipeline, devicefarm, directconnect, discovery, dms, ds, dynamodb, dynamodbstreams, ec2, ecr, ecs, efs, elasticache, elasticbeanstalk, elastictranscoder, elb, elbv2, emr, es, events, firehose, gamelift, glacier, iam, importexport, inspector, iot, iot-data, kinesis, kinesisanalytics, kms, lambda, logs, machinelearning, marketplacecommerceanalytics, meteringmarketplace, opsworks, rds, redshift, route53, route53domains, s3, sdb, servicecatalog, ses, sms, snowball, sns, sqs, ssm, storagegateway, sts, support, swf, waf, workspaces

mp3ファイルを作成するスクリプト

参考:https://boto3.readthedocs.io/en/latest/reference/services/polly.html#Polly.Client.synthesize_speech

import boto3

client = boto3.client(
    'polly'
)
response = client.synthesize_speech(
    OutputFormat='mp3',
    Text='Hello World',
    TextType='text',
    VoiceId='Joanna'
)
print response

実行結果

{u'ContentType': 'audio/mpeg', u'RequestCharacters': '11', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '7030dcae-b76c-11e6-8bdd-03f34d26ede6', 'HTTPHeaders': {'x-amzn-requestid': '7030dcae-b76c-11e6-8bdd-03f34d26ede6', 'transfer-encoding': 'chunked', 'x-amzn-requestcharacters': '11', 'content-type': 'audio/mpeg', 'date': 'Thu, 01 Dec 2016 02:18:24 GMT'}}, u'AudioStream': <botocore.response.StreamingBody object at 0x102b69ad0>}

AudioStreamに音声データが入っている様子ですので、これを取り出して処理すれば良さそうです。
もしくはS3あたりに一旦突っ込んで、presigned_urlでDLできるようにするとかすればSaaSっぽいのが作れるかもです。

とりあえずひとまずここまで。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?