【问题标题】:s3 uploads audio file but it doesn't play. How to upload audio stream to s3?s3 上传音频文件但不播放。如何将音频流上传到 s3?
【发布时间】:2017-11-14 05:39:15
【问题描述】:

我正在尝试将我的 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
                                        (
                                        )

                                )

                        )

                )

        )

)

【问题讨论】:

    标签: javascript php amazon-web-services amazon-s3 amazon-polly


    【解决方案1】:
    $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";
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 2023-01-24
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        • 1970-01-01
        相关资源
        最近更新 更多