admin管理员组文章数量:1356294
i'm trying to implement google speech to text for live audio streams. I have been able to implement for location audio file correctly. However, live audio content is not successful.
I am using V1 of the speech-to-text api. I have gRPC installed already.
This is my PHP script
require 'vendor/autoload.php';
$conn= new mysqli('localhost',"", "", "");
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognizeRequest;
use Google\Cloud\Speech\V1\StreamingRecognizeResponse;
use Google\Cloud\Speech\V1\StreamingRecognitionResult;
use Google\Cloud\Speech\V1\SpeechContext;
use Google\Cloud\Speech\V1\SpeakerDiarizationConfig;
use Google\Cloud\Speech\V1\WordInfo;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/xxx-455121-be3eb805f1d7.json');
$ffmpegCommand = "ffmpeg -re -i -ac 1 -ar 16000 -f s16le pipe:1";
$handle = popen($ffmpegCommand, "r");
try {
$client = new SpeechClient(['transport' => 'grpc', 'credentials' => json_decode(file_get_contents(getenv('GOOGLE_APPLICATION_CREDENTIALS')), true)]);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
$encoding = AudioEncoding::MP3;
try {
$config = (new RecognitionConfig())
->setEncoding($encoding)
->setSampleRateHertz(24000)
->setLanguageCode('en-US')
->setEnableAutomaticPunctuation(true);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
try {
$streamConfig = new StreamingRecognitionConfig([
'config' => $config,
'interim_results' => true,
]);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
try {
$stream = $client->streamingRecognize();
$stream->write(new StreamingRecognizeRequest([
'streaming_config' => $streamConfig
]));
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
while (!feof($handle)) {
usleep(5000);
$chunk = fread($handle, 4096);
printf('chunk: ' . $chunk);
if ($chunk !== false) {
try {
$stream->write(new StreamingRecognizeRequest([
'audio_content' => $chunk
]));
} catch (Exception $e) {
printf('Errorc: ' . $e->getMessage());
}
}
}
print_r($stream->responses());
foreach ($stream->responses() as $response) {
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('loop1')");
foreach ($response->getResults() as $result) {
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('loop2')");
foreach ($result->getAlternatives() as $alternative) {
$trans = $alternative->getTranscript();
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('$trans')");
printf('Transcript: %s' . PHP_EOL, $alternative->getTranscript());
printf('loop');
}
}
}
pclose($handle);
$stream->close();
$client->close();
?>`
The script breaks at $chunk = fread($handle, 4096)
This is the error message
Please assist
本文标签: how can i use google cloud speech to text to stream live audio in PHPStack Overflow
版权声明:本文标题:how can i use google cloud speech to text to stream live audio in PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744003194a2574233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论