【问题标题】:Google Speech API "Sample rate in request does not match FLAC header"Google Speech API“请求中的采样率与 FLAC 标头不匹配”
【发布时间】:2016-09-28 04:11:21
【问题描述】:

我正在尝试将 mp4 视频剪辑转换为 FLAC 音频文件,然后让谷歌语音从视频中吐出单词,以便我可以检测是否说出了特定的单词。

除了从 Speech API 收到错误外,我一切正常:

{
  "error": {
    "code": 400,
    "message": "Sample rate in request does not match FLAC header.",
    "status": "INVALID_ARGUMENT"
  }
}

我正在使用 FFMPEG 将 mp4 转换为 FLAC 文件。我在命令中指定 FLAC 文件为 16 位,但是当我右键单击 FLAC 文件时,Windows 告诉我它是 302kbps。

这是我的 PHP 代码:

// convert mp4 video to 16 bit flac audio file
$cmd = 'C:/wamp/www/ffmpeg/bin/ffmpeg.exe -i C:/wamp/www/test.mp4 -c:a flac -sample_fmt s16 C:/wamp/www/test.flac';
exec($cmd, $output);

// convert flac to text so we can detect if certain words were said
$data = array(
    "config" => array(
        "encoding" => "FLAC",
        "sampleRate" => 16000,
        "languageCode" => "en-US"
    ),
    "audio" => array(
        "content" => base64_encode(file_get_contents("test.flac")),
    )
);

$json_data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=MY_API_KEY');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);

【问题讨论】:

  • 样本格式(您已设置为有符号 16)和采样率之间存在差异。
  • 谢谢,我想通了并提供了答案。似乎 Google Speech API 对位、样本和频道非常挑剔。

标签: php ffmpeg flac google-speech-api


【解决方案1】:

通过在我的 FFMPEG 命令中非常具体来修复它:

$cmd = 'C:/wamp/www/ffmpeg/bin/ffmpeg.exe -i C:/wamp/www/test.mp4 -acodec flac -bits_per_raw_sample 16 -ar 44100 -ac 1 C:/wamp/www/test.flac';

【讨论】:

  • 天哪...这确实有效....自从过去 24 小时以来,这一直在杀死我............世界上怎么没有短命令不起作用??
【解决方案2】:

kjdion84 的回答效果很好,我又玩了一点,找出根本原因。

根据this 的回答,所有编码仅支持 1 声道(单声道) 音频

我正在使用以下命令创建 FLAC 文件:

ffmpeg -i test.mp3 test.flac

请求中的采样率与 FLAC 标头不匹配

但添加 -ac 1(将音频通道数设置为 1)修复了此问题。

ffmpeg -i test.mp3 -ac 1 test.flac

这是我完整的Node.js 代码

const Speech = require('@google-cloud/speech');
const projectId = 'EnterProjectIdGeneratedByGoogle';

const speechClient = Speech({
    projectId: projectId
});

// The name of the audio file to transcribe
var fileName = '/home/user/Documents/test/test.flac';


// The audio file's encoding and sample rate
const options = {
    encoding: 'FLAC',
    sampleRate: 44100
};

// Detects speech in the audio file
speechClient.recognize(fileName, options)
    .then((results) => {
        const transcription = results[0];
        console.log(`Transcription: ${transcription}`);
    }, function(err) {
        console.log(err);
    });

采样率可以是 16000 或 44100 或其他有效值,编码可以是 FLAC 或 LINEAR16。 Cloud Speech Docs

【讨论】:

  • 这太棒了。比我的答案更短更简单,如果有什么我喜欢的,那就是它的简单性。
猜你喜欢
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 2016-10-10
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多