CodeIgniter调用Google Speech-to-Text报GPBDecodeException错误求解决
解决CodeIgniter集成Google Speech-to-Text时的GPBDecodeException错误
问题代码
<?php defined('BASEPATH') or exit('No direct script access allowed'); use Google\Cloud\Speech\V1\RecognitionAudio; use Google\Cloud\Speech\V1\RecognitionConfig; use Google\Cloud\Speech\V1\SpeechClient; use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; use Google\Cloud\Speech\V1\StreamingRecognitionConfig; class SpeechToText extends CI_Controller { public function index() { $this->config->load('google_speech', TRUE); $credentialsPath = $this->config->item('google_speech_credentials_path', 'google_speech'); // Create Speech Client $speech = new SpeechClient([ 'credentials' => $credentialsPath ]); $recognitionConfig = new RecognitionConfig(); $recognitionConfig->setEncoding(AudioEncoding::FLAC); $recognitionConfig->setSampleRateHertz(44100); $recognitionConfig->setLanguageCode('en-US'); $config = new StreamingRecognitionConfig(); $config->setConfig($recognitionConfig); $url = base_url('uploads/file_example_WAV_1MG.wav'); $audioResource = fopen($url, 'r'); $responses = $speech->recognizeAudioStream($config, $audioResource); foreach ($responses as $element) { // doSomethingWith($element); } } }
错误信息
Type: Google\Protobuf\Internal\GPBDecodeException Message: Error occurred during parsing: Fail to push limit.
解决方案
1. 修正音频编码与文件格式不匹配问题
你配置的编码为FLAC,但实际使用的是WAV格式文件,二者不兼容。WAV文件通常采用LINEAR16编码,需修改配置:
$recognitionConfig->setEncoding(AudioEncoding::LINEAR16);
若坚持使用FLAC编码,需先将WAV文件转换为FLAC格式。
2. 使用本地文件路径替代HTTP URL
fopen读取HTTP URL可能受服务器allow_url_fopen配置限制,且流式识别更适配本地文件。替换为CodeIgniter本地文件路径:
// 替换原$url和$audioResource代码 $audioPath = APPPATH . '../uploads/file_example_WAV_1MG.wav'; $audioResource = fopen($audioPath, 'r');
根据项目目录结构调整路径,确保文件存在且拥有可读权限。
3. 验证音频采样率与配置一致
检查WAV文件的实际采样率是否为44100Hz,若不一致,修改setSampleRateHertz的值为文件实际采样率(如常见的16000Hz)。
4. 用非流式识别替代流式识别(适合本地文件)
如果仅处理本地音频文件,无需使用流式识别recognizeAudioStream,改用普通recognize方法更稳定:
// 替换原流式识别相关代码 $audio = RecognitionAudio::fromArray([ 'content' => file_get_contents($audioPath) ]); $response = $speech->recognize($recognitionConfig, $audio); foreach ($response->getResults() as $result) { $alternatives = $result->getAlternatives(); foreach ($alternatives as $alternative) { echo '转录结果: ' . $alternative->getTranscript() . "\n"; } }
修正后的完整示例(非流式版本)
<?php defined('BASEPATH') or exit('No direct script access allowed'); use Google\Cloud\Speech\V1\RecognitionAudio; use Google\Cloud\Speech\V1\RecognitionConfig; use Google\Cloud\Speech\V1\SpeechClient; use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; class SpeechToText extends CI_Controller { public function index() { $this->config->load('google_speech', TRUE); $credentialsPath = $this->config->item('google_speech_credentials_path', 'google_speech'); $speech = new SpeechClient([ 'credentials' => $credentialsPath ]); $recognitionConfig = new RecognitionConfig(); $recognitionConfig->setEncoding(AudioEncoding::LINEAR16); $recognitionConfig->setSampleRateHertz(44100); $recognitionConfig->setLanguageCode('en-US'); $audioPath = APPPATH . '../uploads/file_example_WAV_1MG.wav'; $audio = RecognitionAudio::fromArray([ 'content' => file_get_contents($audioPath) ]); try { $response = $speech->recognize($recognitionConfig, $audio); foreach ($response->getResults() as $result) { foreach ($result->getAlternatives() as $alternative) { echo '转录结果: ' . $alternative->getTranscript(); } } } catch (\Exception $e) { echo '错误信息: ' . $e->getMessage(); } finally { $speech->close(); } } }
内容的提问来源于stack exchange,提问作者user24973246
相关产品推荐
相关产品推荐

