2

我正在尝试将我的 Amazon Polly 语音文件上传到 s3。他们成功上传,所以没有我可以处理的错误,但他们不玩。

我有一个对象数组,其中包括字符串的歌词。我循环它们并创建一个 mp3 文件,然后上传到 s3。


数据结构:

Array
(
    [0] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

    [1] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

    [2] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

)

.
Polly 和 S3 功能:

foreach($final as $key=>$f){

    $pollySpeech = $polly->synthesizeSpeech([
        'OutputFormat' => 'mp3',
        'Text' => $f->lyrics,
        'TextType' => 'text',
        'VoiceId' => 'Salli',
    ]);

    print_r($pollySpeech);

    try {
        $s3->putObject([
                'Bucket' => 'testbucket'
                'Key'    => $key.'.mp3',
                'Body'   => $pollySpeech,
                'ContentType' => 'audio/mpeg',
            'ACL'    => 'public-read',
        ]);
    } catch (Aws\S3\Exception\S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }

}

波莉回应:

Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [AudioStream] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #264
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                        (
                        )

                )

            [ContentType] => audio/mpeg
            [RequestCharacters] => 90
            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://polly.eu-west-1.amazonaws.com/v1/speech
                    [headers] => Array
                        (
                            [x-amzn-requestid] => fc1a7ebf-4f8c-11e7-a1a3-555e1409e93f
                            [x-amzn-requestcharacters] => 90
                            [content-type] => audio/mpeg
                            [transfer-encoding] => chunked
                            [date] => Mon, 12 Jun 2017 16:34:20 GMT
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
4

2 回答 2

2
$pollySpeech->get('AudioStream')->getContents();

所以似乎我试图将整个对象上传到 S3。上面的行让我可以正确上传音频流。

foreach($final as $key=>$f){

    $pollySpeech = $polly->synthesizeSpeech([
        'OutputFormat' => 'mp3',
        'Text' => $f->lyrics,
        'TextType' => 'text',
        'VoiceId' => 'Salli',
    ]);

    print_r($pollySpeech);

    try {
        $s3->putObject([
                'Bucket' => 'testbucket'
                'Key'    => $key.'.mp3',
                'Body'   =>  $pollySpeech->get('AudioStream')->getContents(),
                'ContentType' => 'audio/mpeg',
            'ACL'    => 'public-read',
        ]);
    } catch (Aws\S3\Exception\S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }

}
于 2017-06-13T15:05:17.097 回答
0

另一种方法是

$text = "Hi my name is Bob";
$format = 'mp3'; //json|mp3|ogg_vorbis|pcm
$voice = 'Joanna';
$result = $PollyClient->synthesizeSpeech([
'Text' => $text,
'OutputFormat' => $format,

'VoiceId' => $voice,
]);
$resultData = $result->get('AudioStream')->getContents();
#Save MP3 to S3
$s3bucket = 'bucket-name';
$filename = "message-".$id.'.mp3';
$result_s3 = $S3Client->putObject([
'Key'         => $filename,
'ACL'         => 'public-read',
'Body'        => $resultData,
'Bucket'      => $s3bucket,
'ContentType' => 'audio/mpeg',
'SampleRate'  => '8000'
]);
return $result_s3['ObjectURL'];
于 2021-02-02T11:24:56.950 回答