Location>code7788 >text

FFmpeg development notes (fifty-eight) to 32-bit samples of MP3 converted to 16-bit PCM audio

Popularity:274 ℃/2024-10-19 12:47:54
5.1.2 Saving Audio Streams as PCM Files" in the book "FFmpeg Development in Action: From Zero Basics to Short Videos on the Web" describes how to save the audio streams in media files as raw PCM audio, and in the transfer process of the sample code, the decoded PCM data is directly saved to a binary file without any processing. saved to a binary file. In other words, what is the sampling frequency of the original audio, what is the sampling frequency of the PCM file; what is the number of channels of the original audio, what is the number of channels of the PCM file; what is the number of sampling bits of the original audio, what is the number of sampling bits of the PCM file.

There is nothing wrong with the original PCM file, but in practical applications, some business scenarios require specific PCM audio specifications. For example, a manufacturer's speech recognition engine requires only 16-bit PCM data input, but the standard MP3 audio uses 32-bit sampling, so you have to find a way to convert 32-bit MP3 audio to 16-bit PCM audio.
Considering that it is more convenient to use FFmpeg's command line conversion, the following ffmpeg format conversion command is executed on the console to convert the sample frequency and number of channels together with the number of sample bits.

ffmpeg -i night.mp3 -ar 16000 -ac 1 -acodec pcm_s16le 

The console outputs the following error message "pcm_s16le codec not supported", meaning that the 16-bit PCM encoder is not supported.

pcm_s16le codec not supported

Gee, how could FFmpeg not support such a basic PCM encoder? Go ahead and execute the following encoder view command:

ffmpeg -encoders | grep pcm

The following pcm_s16le message appears in the output query, indicating that FFmpeg already supports this encoder by default.

A....D pcm_s16le            PCM signed 16-bit little-endian

So why does the ffmpeg command line not convert the sample bits of PCM audio properly?
searched around and found that there is no use of ffmpeg successful conversion of the number of samples of the case, had to first convert the original audio to 32-bit samples of the PCM file, the conversion command is shown below:

ffmpeg -i night.mp3 -ar 16000 -ac 1 -acodec pcm_f32le -f f32le 

Next, in addition to writing the code to convert the number of bits of audio samples, the code content is shown below:

#include <>
#include <>
#include <>

int pcm32_to_pcm16(const char *filename)
{  
    FILE *fp =  fopen(filename, "rb");
    FILE *fp1 = fopen("output_16.pcm", "wb");
    unsigned char *sample = (unsigned char*)calloc(1, 4+1);
    while(!feof(fp))
    {
        fread(sample, 4, 1, fp);
        sample[4] = '\0';
        float *sample32 = (float*)sample;
        short sample16 = (short)floor( (*sample32) * 32767 );
        fwrite(&sample16, 2, 1, fp1);
    }
    free(sample);
    fclose(fp);
    fclose(fp1);
    return 0;  
}

int main(int argc, char **argv) {
    const char *src_name = "";
    if (argc > 1) {
        src_name = argv[1];
    }
    pcm32_to_pcm16(src_name);
}

Save the code and then execute the following compile command.

gcc  -o convertpcm 

The compilation is complete, then execute the following sample bit conversion command.

./convertpcm 

The output_16.pcm generated is now the 16-bit sampled PCM file that can be used for speech recognition.

For more details on FFmpeg development seeFFmpeg Development in Action: From Zero Basics to Short Video OnlineA book.