windows Under the use of FFmpeg generate PCM Audio file and play （ By way of command ）
One ,PCM Definition of documents
PCM file ： Analog audio signal is converted into analog to digital （A/D Transformation ） Directly formed binary sequence
, The file has no additional header and end of file flag .Windows Of Convert The tool can make PCM Convert audio format files into Microsoft Of WAV File format . Digitize audio , In fact, it's about digitizing sound . The most common way is through pulse code modulation PCM(Pulse
Code Modulation) .
The above paragraph is excerpted by bloggers on the Internet PCM Definition of documents . actually PCM In a nutshell , Is an uncompressed original audio format , And what we know MP3 Audio files of the same format are compressed （ The result of no compression is that it takes up a lot of space , Will greatly increase the cost of storage and transmission , So what is stored on the computer is usually MP3 Files in the same compressed format ）. The player wants to play MP3,AAC,APE,FLAC Audio files in the same format , First, you have to decode them into PCM data （ After decoding, it is usually FLT Format of , Also through resampling to the sound card support S16 format ）, Then it can be played .
Two , adopt FFmpeg Command conversion PCM file
As shown in the figure below , There are audio1.mp3 this MP3 file , Next, we introduce how to convert it into PCM Method of documentation .
At the command prompt cmd.exe Enter the directory where the current project is located （ffmpeg.exe and audio1.mp3 Directory ）, Then enter the command ：
ffmpeg -i audio1.mp3 -f s16le audio1.pcm
The function of this command is to set the audio1.mp3 File to s16( The sound card supports this format ）, Small end format PCM file ：audio1.pcm.
-i Represents the set input stream
-f It means to set the output format
After the command is executed , We found that the project directory will generate audio1.pcm file , As shown in the figure below ：
Three ,PCM Document analysis
We can see the generated audio1.pcm The file size is 49.5MB, than audio1.mp3 Document's 11.2MB Several times bigger , As shown in the figure below , That proves it PCM File occupancy ratio MP3 The file should be large .
as for audio1.pcm The file size is 49.5MB, this 49.5MB, It can be calculated according to the formula . Use another blog post from the blogger 《FFmpeg Unpacking （ Demultiplexing ） And get the information of local media files 》
You can see audio1.mp3 Sampling frequency of （ Audio sampling rate ） by 44100Hz, The sampling bits are 16 position （ This is from the audio sampling format ：AV_SAMPLE_FMT_S16P It can be seen that ), Vocal tract （ Number of audio channels ） by 2. time （ Total audio duration ） by 294 second , As shown in the figure below ：（ We use the same one in both blogs audio1.mp3 file , The following is an excerpt from the above blog ）
according to PCM The formula of file capacity ： Storage capacity = ( sampling frequency * Sampling bits * Vocal tract )* time /8( Company ： Number of bytes ).
Calculated audio1.pcm Size of = (44100 * 16 * 2) * 294 / 8 = 51861600（ byte ） = 49.45MB
This is what we saw on the computer audio1.pcm The file size is 49.5MB It's consistent . This proves that PCM The size of the file can be calculated .
Four , use ffplay.exe play PCM file
PCM The file is the original uncompressed audio format file , With popular players on the market, we can't play them directly . But we can go through FFmpeg Provided ffplay.exe play PCM file .
At the command prompt cmd.exe Enter the directory where the current project is located （ffplay.exe and audio1.pcm Directory ）, Then enter the command ：
ffplay -ar 44100 -ac 2 -f s16le -i audio1.pcm
The purpose of this command is to use the ffplay Playback frequency is 44100Hz, Dual channel ,16 position , Small end audio file audio1.pcm.
-i Represents the specified input file
-f Represents a format that is mandatory
-ar Represents the sampling rate of played audio data
-ac Represents the number of channels for playing audio data
As shown in the figure below , The following pictures appear and music can be heard , It means that the playback is successful .
Five , Problems encountered ：
If used ffplay.exe Error playing media file ：WASAPI can't initialize audio client. As shown in the figure below ：
Execute the command at the command prompt first ：
And then use it ffplay.exe Play media files .
Six , Resource download
Download resources at https://download.csdn.net/download/u014552102/10671629
, Contains all the resources for the above demo . Including the above ffmpeg.exe,ffplay.exe,mp3 file ,pcm Documents, etc .