S3上传音频文件,但无法播放。如何将音频流上传到 s3?



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

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


数据结构:

Array
(
    [0] => stdClass Object
        (
            [lyrics] => sample lyrics
        )
    [1] => stdClass Object
        (
            [lyrics] => sample lyrics
        )
    [2] => stdClass Object
        (
            [lyrics] => sample lyrics
        )
)

.
波莉和 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 (AwsS3ExceptionS3Exception $e) {
        echo "There was an error uploading the file.n";
    }
}

波莉回应:

AwsResult Object
(
    [data:AwsResult:private] => Array
        (
            [AudioStream] => GuzzleHttpPsr7Stream Object
                (
                    [stream:GuzzleHttpPsr7Stream:private] => Resource id #264
                    [size:GuzzleHttpPsr7Stream:private] => 
                    [seekable:GuzzleHttpPsr7Stream:private] => 1
                    [readable:GuzzleHttpPsr7Stream:private] => 1
                    [writable:GuzzleHttpPsr7Stream:private] => 1
                    [uri:GuzzleHttpPsr7Stream:private] => php://temp
                    [customMetadata:GuzzleHttpPsr7Stream: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
                                        (
                                        )
                                )
                        )
                )
        )
)
$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 (AwsS3ExceptionS3Exception $e) {
        echo "There was an error uploading the file.n";
    }
}

另一种方法是

$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'];

最新更新