1. In FFmpeg source code, can often see> > 3What does that mean?

  • Binary shifted three places to the rightSaid,Divided by eight

2. How to passNamed lineAbout the recording equipmentSampling rate,Track number,Sampling format?

  • Let the recording device do the recordingffmpeg -f avfoundation -i :0 out.wav
Input #0, avfoundation, from '0'.:
Duration: N/A, start: 87533.086750, bitrate: 256 kb/s
Stream #0:0: Audio: pcm_f32le, 8000 Hz, mono, flt, 256 kb/s
Copy the code
  • Sampling rate: 8000 hz
  • Track numberMono
  • Sampling format: F32LE (floating point 32-bit small enode mode)

3. How to obtain the recording equipment in the codeSampling rate,Track number,Sampling format?

  • CTX → streams → streams → codec → sample_rate = 8000
  • CTX → streams → streams → sample_fmt = 1
  • CTX → streams → streams → codec_id = AV_CODEC_ID_PCM_F32LE

4. What is RIFF? Which two of the more mainstream audio formats are RIFF compliant?

  • Resource Interchange File Format (RIFF) was proposed by Microsoft and IBM
  • WAV and AVI files are based on the RIFF standard file format. Therefore, the first four bytes of WAV and AVI files are RIFF four characters

5. What does chunk mean? What are the three parts of a chunk?

  • Chunk: data block
  • Each chunk consists of three parts: ID (the identifier of the chunk), data-size (the size of the chunk’s data in bytes), and data (the data of the chunk).

6. Dissect WVA files

  • Using the previous command line to record the audio, can be obtainedout.wavFile, file size:348204byte
  • We will beout.wavWe open it in binary format using vscode, and then we analyze its header data
// out.wav header is hexadecimal data
00000000: 52 49 46 46 24 50 05 00 57 41 56 45 66 6D 74 20RIFF$P.. WAVEfmt.00000010: 10 00 00 00 01 00 02 00 44 AC 00 00 10 B1 02 00. D,..1..00000020: 04 00 10 00 64 61 74 61 00 50 05 00 00 00 0E BB .... data.P..... ;Copy the code
  • rightout.wavThe first 44 bytes are parsed as follows:
52 49 46 46(ChunkID,4Bytes) = 'RIFF'24 50 05 00(ChunkSize,4Byte) =348196  = 348204 - 8

57 41 56 45(Format,4Bytes) = 'WAVE'66 6D 74 20(Subchunk1ID,4Bytes) = 'FMT'10 00 00 00(Subchunk1 Size,4Byte) =16

01 00(AudioFormate,2Bytes) = Audio format =1

02 00(NumChannels,2Bytes) = Number of channels =2

44 AC 00 00(SampleRate,4Bytes) = sample rate =44100

10 B1 02 00(ByteRate,4Bytes) = byte rate =176400

04 00(BlockAlign,2Bytes) = memory alignment =4

10 00(BitsPerSample) = The bit depth of each sample =16

64 61 74 61(Subchunk2ID,4Byte) = 'data'00 50 05 00(Subchunk2 Size,4Bytes) = Audio PCM data size =348160 = 348204 - 44
Copy the code

7. Three pictures to understand

8. Convert PCM to WAV on the cli

ffmpeg -ar 44100 -ac 2 -f s16le -i out.pcm -bitexact out2.wav
Copy the code

9 code to achieve PCM to WAV

void FFmpegs::pcm2wav(WAVHeader &header, const char *pcmFilename, const char *wavFilename) {
 header.blockAlign = header.bitsPerSample * header.numChannels >> 3;
 header.byteRate = header.sampleRate * header.blockAlign;
 
 // Open the PCM file
 QFile pcmFile(pcmFilename);
 if(! pcmFile.open(QFile::ReadOnly)) { qDebug() <<"File opening failed" << pcmFilename;
     return;
 }
 
 
 header.dataChunkDataSize = pcmFile.size();
 header.riffChunkDataSize = header.dataChunkDataSize
                            + sizeof (WAVHeader)
                            - sizeof (header.riffChunkId)
                            - sizeof (header.riffChunkDataSize);
 
 // Open the wav file
 QFile wavFile(wavFilename);
 if(! wavFile.open(QFile::WriteOnly)) { qDebug() <<"File opening failed" << wavFilename;
     
     pcmFile.close();
     return;
 }
 
 // Write the header
 wavFile.write((const char*)&header, sizeof(WAVHeader));
 
 // Write PCM data
 char buf[1024];
 int size;
 while ((size = pcmFile.read(buf, sizeof(buf))) > 0) {
     wavFile.write(buf, size);
 }
 
 // Close the file
 pcmFile.close();
 wavFile.close();
}
Copy the code
  • The call code is as follows:
WAVHeader header;
header.sampleRate = 44100;
header.bitsPerSample = 16;
header.numChannels = 2;

FFmpegs::pcm2wav(header,
              "/Users/linsipei/Documents/ffmpeg_workspcace/05_tmp/in.pcm"."/Users/linsipei/Documents/ffmpeg_workspcace/05_tmp/out.wav");
Copy the code

10 code to achieve the recording of PCM directly to WAV (including dynamic access to recording equipment related information operation)

// Get the input stream
AVStream *stream = ctx->streams[0];
// Get audio parameters
AVCodecParameters *params = stream->codecpar;

// PCM to WAV file
WAVHeader header;
header.sampleRate = params->sample_rate;
header.bitsPerSample = av_get_bits_per_sample(params->codec_id);
header.numChannels = params->channels;
if (params->codec_id >= AV_CODEC_ID_PCM_F32BE) {
 header.audioFormat = AUDIO_FORMAT_FLOAT;
}
FFmpegs::pcm2wav(header, filename.toUtf8().data(), wavFilename.toUtf8().data());
Copy the code