LoginSignup
4
1

More than 3 years have passed since last update.

Lambdaでsoxを使用して音声を左右のチャンネルに分割する

Last updated at Posted at 2019-07-30

※ 2019年末にNodejs:8.10が非推奨になるので、Nodejs:8.10にてLambdaの作成・更新が出来なくなった場合、以下の手順は実現できなくなります。

AmazonConnectの録音ファイルが顧客とエージェントで左右のチャンネルに分割されている為、それを分割しようとした時のメモです。

Q: エージェントと顧客の音声は別々のステレオ音声チャネルに保存されるのですか?

はい。エージェントの音声は右チャネルに保存されます。エンドカスタマーやカンファレンスに参加した人など、すべての着信音声は左チャネルに保存されます。
AWSより引用

ハマった事

lambdaのランタイムをnodejs10.xで検証していたのですが、soxが動作しませんでした。

解決策

lambdaでsoxを動作させたい場合は、nodejs8.10をランタイムに設定しましょう。

分割していく

AmazonConnectからS3に通話ろファイル(.wav)が格納された際にLambdaが実行される前提となっています。

soxを使うためにlambda-audioをインストール。

npm i lambda-audio

typescriptで書いてます。


import {Handler} from 'aws-lambda';
import {S3} from "aws-sdk";

class S3Service {
    constructor(private s3: S3) {
    }

    async get(bucketName, key) {
        const params = {
            Bucket: bucketName,
            Key: key
        };
        return await this.s3.getObject(params).promise();
    }

    async put(bucketName, key, body) {
        const params = {
            Bucket: bucketName,
            Key: key,
            Body: body,
        };
        return await this.s3.putObject(params).promise();
    }
}

export const handler: Handler = async (event: any) => {
    const s3 = new S3Service(new S3());

    const fs = require('fs');
    const lambdaAudio = require('lambda-audio');

    const nowTime = (new Date()).getTime();
    const wavPath = '/tmp/' + nowTime + '.wav';

    // S3に格納された.wavの情報を取得する
    for (let record of event.Records) {
        const key = record.s3.object.key;
        const bucketName = record.s3.bucket.name;
        const object = await s3.get(bucketName, decodeURIComponent(key));

        await fs.writeFile(wavPath, object.Body, 'binary', err => {
            if (err) {
                console.error(err);
                return;
            }
        });
    }

    // ここで左のチャンネルを抽出している
    const leftOutput = `/tmp/${nowTime}_output_l.wav`;
    await lambdaAudio.sox([wavPath, leftOutput, 'remix', '1'])
        .then(response => {
            console.log(`soxLeft: ${response}`);
        })
        .catch(errorResponse => {
            console.log('Error from the sox command:', errorResponse)
        });

    // ここで右のチャンネルを抽出している
    const rightOutput = `/tmp/${nowTime}_output_r.wav`;
    await lambdaAudio.sox([wavPath, rightOutput, 'remix', '2'])
        .then(response => {
            console.log(`soxRight: ${response}`);
        })
        .catch(errorResponse => {
            console.log('Error from the sox command:', errorResponse)
        });

    // S3へ格納(※1)
    await s3.put('BUCKET_NAME', `${nowTime}/l.wav`, fs.readFileSync(leftOutput));
    await s3.put('BUCKET_NAME', `${nowTime}/r.wav`, fs.readFileSync(rightOutput));

    return {}
};

※1 'BUCKET_NAME'に保存先となるS3を指定

まとめ

顧客、エージェントの音声を分割することにより、文字起こし等に使用する際に便利になると思います。
ファイルサイズが2倍になるため、利用料金は2倍になりますが、、、

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