【发布时间】:2019-03-02 21:01:58
【问题描述】:
我正在拼命想弄清楚如何通过 PHP(我知道的唯一语言)创建一个简单的音频转录脚本(用于更长的音频文件)。我收到错误找不到类“Google\Cloud\Storage\StorageClient”
我正在使用 gcloud 控制台代码编辑器,并且应该安装所有内容(除非有一个单独的 composer 安装仅用于云存储,尽管我无法在文档中找到任何关于它的内容(如果有的话))。
我还输入了 gcloud auth application-default print-access-token,它打印出一个访问令牌,但我不知道我应该如何处理其他令牌(如果有的话)而不是我复制并粘贴到控制台 shell 提示符中的“set GOOGLE_APPLICATION_CREDENTIALS”命令。
这是 php 代码:
<?php
namespace Google\Cloud\Samples\Speech;
require __DIR__ . '/vendor/autoload.php';
use Exception;
# [START speech_transcribe_async_gcs]
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\ExponentialBackoff;
$projectId = 'xxxx';
$speech = new SpeechClient([
'projectId' => $projectId,
'languageCode' => 'en-US',
]);
$filename = "20180925_184741_L.mp3";
# The audio file's encoding and sample rate
$options = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US',
'enableWordTimeOffsets' => false,
'enableAutomaticPunctuation' => true,
'model' => 'video',
];
function transcribe_async_gcs($bucketName, $objectName, $languageCode = 'en-US', $options = [])
{
// Create the speech client
$speech = new SpeechClient([
'languageCode' => $languageCode,
]);
// Fetch the storage object
$storage = new StorageClient();
$object = $storage->bucket($bucketName)->object($objectName);
// Create the asyncronous recognize operation
$operation = $speech->beginRecognizeOperation(
$object,
$options
);
// Wait for the operation to complete
$backoff = new ExponentialBackoff(10);
$backoff->execute(function () use ($operation) {
print('Waiting for operation to complete' . PHP_EOL);
$operation->reload();
if (!$operation->isComplete()) {
throw new Exception('Job has not yet completed', 500);
}
});
// Print the results
if ($operation->isComplete()) {
$results = $operation->results();
foreach ($results as $result) {
$alternative = $result->alternatives()[0];
printf('Transcript: %s' . PHP_EOL, $alternative['transcript']);
printf('Confidence: %s' . PHP_EOL, $alternative['confidence']);
}
}
}
# [END speech_transcribe_async_gcs]
transcribe_async_gcs("session_audio", $filename, "en-US", $options);
【问题讨论】:
标签: php google-cloud-platform google-cloud-storage