0

尝试使用 AWS SDK PHP API 转录存储在我可以访问的 S3 存储桶中的音频文件,如下所示,给了我一个限制异常:超出速率。

我已阅读文档十几次,但找不到一个简单的工作示例来说明如何使用 AWS transcribe 及其 PHP API 成功转录文件。

date_default_timezone_set('America/New_York');
try 
{
    require '/var/www/html/aws/sdk/aws-autoloader.php';
} 
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
use Aws\TranscribeService\TranscribeServiceClient;

$client = new Aws\TranscribeService\TranscribeServiceClient([
    'version'       => 'latest',
    'region'        => 'us-east-1',
    'credentials'   => [
                    'key'           => 'xxxx',
                    'secret'        => 'yyyy',
                    'curl.options'  => array(CURLOPT_VERBOSE => true)
                    ]
        ]);
$job_name = "tjob".date("mdyhisa");
$job_uri = "https://s3.amazonaws.com/....mp3";          

$result = $client->startTranscriptionJob([
    'LanguageCode' => 'en-US', 
    'Media' => [ 
    'MediaFileUri' => "$job_uri",
    ],
    'MediaFormat' => 'mp3', 

    'TranscriptionJobName' => "$job_name", 
]);
/* removing this loop and the sleep() below would retrieve some structured response, 
but of course the operation status is IN_PROGRESS */
while(true)
{
    /* added to discover if holding a few seconds would work: it doesn't
       and gives back a 504 Gateway Timeout */
    sleep(rand(3,5));
    /* -- */
    $result = $client->getTranscriptionJob(['TranscriptionJobName' => "$job_name"]);
    if ( ($result['TranscriptionJob']['TranscriptionJobStatus']=='COMPLETED') || ($result['TranscriptionJob']['TranscriptionJobStatus']=='FAILED'))
    {
        break;  
    }
}
var_dump($result);

所以问题是:如何获得转录输出?

顺便说一句,我不需要这个异步......我的小项目等待它处理并返回就可以了。

4

1 回答 1

0

您的代码可能运行良好,但您的while(true)循环调用 API 的次数过多,因此出现throttling exception: rate exceeded错误。

我建议您在每次调用getTranscriptionJob. 我发现一项工作可能需要大约 60 秒才能完成,因此您无需连续调用它。

于 2019-04-03T02:15:49.130 回答