I'm trying to convert an mp4 video clip into a FLAC audio file and then have google speech spit out the words from the video so that I can detect if specific words were said.
I have everything working except that I am getting an error from the Speech API:
{
"error": {
"code": 400,
"message": "Sample rate in request does not match FLAC header.",
"status": "INVALID_ARGUMENT"
}
}
I am using FFMPEG in order to convert the mp4 into a FLAC file. I am specifying that the FLAC file be 16 bits in the command, but when I right click on the FLAC file Windows is telling me it is 302kbps.
Here is my PHP code:
// 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);
kjdion84's answer worked well, and I played a bit more to find out the root cause.
As per this answer, all encodings support only 1 channel (mono) audio
I was creating the FLAC file with this command:
But adding the
-ac 1
(setting number of audio channels to 1) fixed this issue.Here is my full
Node.js
codeSample rate could be 16000 or 44100 or other valid ones, and encoding can be FLAC or LINEAR16. Cloud Speech Docs
Fixed it by being very specific in my FFMPEG command: