LoginSignup
7
7

More than 5 years have passed since last update.

nodejsからAmazon pollyを操作するサンプルスクリプト

Last updated at Posted at 2016-12-06

ES6記法頑張る

describeVoices

index.js
'use strict';

import aws from 'aws-sdk';
aws.config.loadFromPath('credential.json');

let polly = new aws.Polly({apiVersion: '2016-06-10',region:'us-west-2'});

// describeVoices
let descParams = {
    LanguageCode: 'ja-JP'
};

polly.describeVoices(descParams, (err, data)=>{
   if(err){
       console.log(err);
   } else {
       console.log(JSON.stringify(data))
   }
});

結果

結果
{"Voices":[{"Gender":"Female","Id":"Mizuki","LanguageCode":"ja-JP","LanguageName":"Japanese","Name":"Mizuki"}],"NextToken":null}

CLIと同様問題なく取得できました

synthesizeSpeech

コールバックにネストして書きます

script
let textMsg = 'パンツァーフォー';
       let speechParams = {
           OutputFormat: 'mp3',
           VoiceId: voiceId,
           Text: textMsg,
           SampleRate: '22050',
           TextType: 'text'
       };

       polly.synthesizeSpeech(speechParams).promise()
           .then(data => {
               console.log(data);
           })
           .catch(err => {
               console.log(err);
           });
       });

結果

結果
{ ContentType: 'audio/mpeg',
  RequestCharacters: '8',
  AudioStream: <Buffer 49 44 33 04 00 00 00 00 00 23 54 53 53 45 00 00 00 0f 00 00 03 4c 61 76 66 35 37 2e 34 31 2e 31 30 30 00 00 00 00 00 00 00 00 00 00 00 ff f3 60 c4 00 ... > }

ちゃんと帰ってきました。
CLIでやったときのようにmp3ファイルを取得するには

script
polly.synthesizeSpeech(speechParams), (err, data) =>{
    .then(data => {
        console.log(data);
        fs.writeFile('audio/polly.mp3', data.AudioStream, (err) => {
            if (err) {
                console.log(err);
            } else {
                console.log('Success');
            }
        })
    })
    .catch(err => {
        console.log(err);
    });
});

こんな感じにすると手元に落ちてきます

まとめ

SDKで使うのもCLI同様そんなに難しいサービスではないですね
実際は落としたものをS3にUPして何かするのがよいのでしょうか

スクリプト全体

index.js
'use strict';

const aws = require('aws-sdk');
const fs = require('fs');

aws.config.loadFromPath('<クレデンシャルへのパス>');
let polly = new aws.Polly({apiVersion: '2016-06-10',region:'us-west-2'});


// describeVoices
let descParams = {
    LanguageCode: 'ja-JP'
};

polly.describeVoices(descParams)
    .then(data => {
        //console.log(JSON.stringify(data));
        let voiceId = data.Voices[0].Id;

        // synthesizeSpeech
        let textMsg = 'あんずのうた';
        let speechParams = {
            OutputFormat: 'mp3',
            VoiceId: voiceId,
            Text: textMsg,
            SampleRate: '22050',
            TextType: 'text'
        };

        return polly.synthesizeSpeech(speechParams).promise();
    })
    .then(data => {
        console.log(data);
        fs.writeFile('polly.mp3', data.AudioStream, (err) => {
            if (err) {
                console.log(err);
            } else {
                console.log('Success');
            }
        });
    })
    .catch(err => {
        console.log(err);
    });
});

参考

7
7
1

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