Google Cloud Speech-to-Text API:基于google-api-php-client的语音识别
【免费下载链接】google-api-php-client 项目地址: https://gitcode.com/gh_mirrors/goog/google-api-php-client
你是否还在为实现语音识别功能而烦恼?使用Google Cloud Speech-to-Text API配合google-api-php-client,只需简单几步即可快速构建强大的语音识别应用。读完本文,你将了解如何配置环境、实现基础语音识别、处理长音频文件以及优化识别效果。
准备工作
环境要求
- PHP 7.2+
- Composer
- Google Cloud账号及项目
安装google-api-php-client
通过Composer安装客户端库:
composer require google/apiclient:^2.0
官方安装文档:docs/install.md
认证配置
- 在Google Cloud控制台创建服务账号密钥
- 下载JSON密钥文件
- 设置环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credentials.json"
认证详细说明:docs/auth.md
快速开始:基础语音识别
创建客户端实例
require __DIR__ . '/vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/cloud-platform']);
// 创建Speech-to-Text服务
$speech = new Google\Service\Speech($client);
音频文件识别
$audio = [
'content' => base64_encode(file_get_contents('audio.raw'))
];
$config = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US'
];
$request = new Google\Service\Speech\RecognizeRequest([
'config' => $config,
'audio' => $audio
]);
$response = $speech->speech->recognize($request);
foreach ($response->getResults() as $result) {
echo $result->getAlternatives()[0]->getTranscript() . "\n";
}
处理长音频文件
对于超过1分钟的音频文件,应使用异步识别:
$audio = [
'uri' => 'gs://your-bucket/audio-long.wav'
];
$config = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US'
];
$request = new Google\Service\Speech\LongRunningRecognizeRequest([
'config' => $config,
'audio' => $audio
]);
$operation = $speech->speech->longrunningrecognize($request);
// 等待操作完成
$operation->waitForCompletion();
if ($operation->isDone()) {
$response = $operation->getResult();
// 处理结果
}
长文件处理实现:src/Http/MediaFileUpload.php
高级功能
多语言识别
$config = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'zh-CN',
'alternativeLanguageCodes' => ['en-US', 'ja-JP']
];
语音活动检测
$config = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US',
'enableWordTimeOffsets' => true
];
错误处理
try {
$response = $speech->speech->recognize($request);
} catch (Google\Exception $e) {
echo "Error: " . $e->getMessage();
// 处理错误,如重试或记录日志
}
错误处理机制:src/Exception.php
性能优化
-
批量处理:使用批量操作同时处理多个请求 examples/batch.php
-
压缩音频:使用FLAC或MULAW编码减少带宽使用
-
适当采样率:根据实际需求选择合适的采样率
总结
本文介绍了如何使用google-api-php-client实现Google Cloud Speech-to-Text API的核心功能,包括基础识别、长音频处理和高级配置。通过合理利用这些功能,你可以构建出高效准确的语音识别应用。
项目完整示例:examples/
如果你觉得本文有帮助,请点赞收藏,并关注获取更多Google Cloud API使用技巧。下期我们将介绍如何实现实时语音识别功能。
【免费下载链接】google-api-php-client 项目地址: https://gitcode.com/gh_mirrors/goog/google-api-php-client
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



