Google Speech API "Sampling rate in the request does not match the FLAC header"

I try to convert a mp4 video clip to a FLAC audio file, and then Google speech spits out words from the video so that I can determine if specific words were specified.

Everything works for me, except that I get an error message from the Speech API:

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

I use FFMPEG to convert mp4 to FLAC file. I indicate that the FLAC file must be 16 bits in the command, but when I right-click on the FLAC file, Windows tells me that it is 302 Kbps.

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);
+1
source share
2 answers

This is fixed, being very specific in my FFMPEG command:

$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';
+6

kjdion84 , , .

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 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.

+1

All Articles