Google Voice2Text 无法转录音频文件 - 总计费时间未知

问题描述 投票:0回答:1

我正在尝试转录 ogg 文件(也尝试过 wav)并且总是收到错误:

解析期间发生错误:解析期间发生错误:totalBilledTime 未知。

我正在使用 PHP 8 和最新的库“google/cloud”:“^0.239.0”

尝试了一些不同的 ogg 文件(从 Whatsapp 收到)....它们是小文件,通常为 15 kb。

    function getAudio2Text($RecordingUrl, &$text, &$error)
    {
        // Configura o ambiente com a chave da conta de serviço
        putenv('GOOGLE_APPLICATION_CREDENTIALS=' . GOOGLE_APPLICATION_CREDENTIALS);

        $text = null;
        $error = null;

        // Configura o cliente da API
        $client = new SpeechClient();

        try {
            // Configuração da solicitação
            $config = (new RecognitionConfig())
                ->setEncoding(AudioEncoding::LINEAR16)
                ->setSampleRateHertz(44100)
                ->setLanguageCode('pt-BR'); // Ou o idioma que você precisa

            // Lê o conteúdo do arquivo
            $audioContent = file_get_contents($RecordingUrl);

            $audio = (new RecognitionAudio())
                ->setContent($audioContent);

            // Faz a solicitação e obtém a resposta
            $response = $client->recognize($config, $audio);

            // Processa a resposta
            foreach ($response->getResults() as $result) {
                $alternatives = $result->getAlternatives();
                $mostLikely = $alternatives[0];
                $text .= $mostLikely->getTranscript();
            }
            return array(
                'success' => true,
                'alternatives' => $alternatives,
                'mostLikely' => $mostLikely,
                'text' => $text
            );
        } catch (Exception $e) {
            $error = $e->getMessage();
            return array(
                'success' => false,
                'error' => $error
            );
        } finally {
            $client->close();
        }

        if (empty($text)) {
            $error = "Não foi possível transcrever o áudio.";
        }
    }
google-cloud-speech
1个回答
0
投票

问题出在版本上......我升级了并且它起作用了:

最终版本:

{ “要求”: { "php": "8.3.", “谷歌/云”:“^0.243.0”, "google/apiclient": "^2.0", "guzzlehttp/guzzle": "^7.4.5", "aws/aws-sdk-php": "^3.283", "pusher/pusher-php-server": "^5.0", "谷歌/云文本转语音": "", "twilio/sdk": "dev-main", "sendgrid/sendgrid": "dev-main", “条纹/条纹-php”:“^13.10@beta” }, “最低稳定性”:“开发” }

© www.soinside.com 2019 - 2024. All rights reserved.