windows Next useFFmpeg generatePCM Audio file and play（ By command）
One,PCM Definition of documents
PCM file： Analog audio signal through a / D conversion（A/D Transformation） Directly formed binary sequence
, The file does not have an attached header and end of file flag.Windows OfConvert Tools canPCM Convert files in audio format toMicrosoft OfWAV Format file. Digitize audio, It's actually digitizing sound.. The most common way is through pulse code modulationPCM(Pulse
Code Modulation) .
The above passage is about what the blogger excerpted on the Internet.PCM Definition of documents. actuallyPCM To put it simply, It's a uncompressed original audio format. And what we knowMP3 Equal format audio files are compressed（ The result of no compression is that it takes up a lot of space. It will greatly increase the cost of storage and transmission, So what's stored on a computer is usuallyMP3 Files of equal compression format）. The player wants to playMP3,AAC,APE,FLAC Audio files of equal format, First we have to decode them intoPCM data（ After decoding, it is usuallyFLT Format, It is also supported by resampling to sound card.S16 format）, Then you can play it.
Two, adoptFFmpeg Command conversion resultPCM file
As shown in the figure below, There areaudio1.mp3 thisMP3 file, Let's turn it intoPCM Method of documentation.
At the command promptcmd.exe Enter the directory where the current project is located（ffmpeg.exe andaudio1.mp3 Directory）, Then enter the command：
ffmpeg -i audio1.mp3 -f s16le audio1.pcm
The function of this command is toaudio1.mp3 File tos16( The sound card supports this format）, Small end formatPCM file：audio1.pcm.
-i Indicates set input flow
-f Indicates to set the output format
After executing the command, We found that the project directory will generateaudio1.pcm file, As shown in the figure below：
Three,PCM Document analysis
We can see the generatedaudio1.pcm File size is49.5MB, thanaudio1.mp3 Documentation11.2MB Several times larger, As shown in the figure below, ProvedPCM File occupancy ratioMP3 File must be large..
As foraudio1.pcm File size is49.5MB, this49.5MB, It can be calculated according to the formula. Use another blog post from the blogger《FFmpeg Solution encapsulation（ Demultiplexing） And get information about local media files》
The program in can seeaudio1.mp3 Sampling frequency of（ Audio sampling rate） by44100Hz, The number of sampling bits is16 position（ This sample format from audio：AV_SAMPLE_FMT_S16P It can be seen that), Vocal tract（ Number of audio channels） by2. time（ Total audio duration） by294 second, As shown in the figure below：（ We use the same in both blogsaudio1.mp3 file, The following is from the blog above）
according toPCM Formula of file capacity： Storage capacity = ( sampling frequency* Sampling digit* Vocal tract)* time/8( Company： Bytes).
Calculatedaudio1.pcm Size = (44100 * 16 * 2) * 294 / 8 = 51861600（ byte） = 49.45MB
This is what we saw on the computer.audio1.pcm File size is49.5MB It is the same.. This proves thatPCM The size of the file can be calculated.
Four, Useffplay.exe playPCM file
PCM File is the original uncompressed audio format file, With the popular players on the market, we can't play them directly.. But we can get throughFFmpeg Providedffplay.exe playPCM file.
At the command promptcmd.exe Enter the directory where the current project is located（ffplay.exe andaudio1.pcm Directory）, Then enter the command：
ffplay -ar 44100 -ac 2 -f s16le -i audio1.pcm
The purpose of this command is to use theffplay The playback frequency is44100Hz, Dual channel,16 position, Small end audio fileaudio1.pcm.
-i Represents the specified input file
-f Indicates the mandatory format
-ar Represents the sampling rate of played audio data
-ac Indicates the number of channels for playing audio data
As shown in the figure below, The following pictures and music can be heard, It means the playback is successful..
Five, Encounter problems：
If usedffplay.exe Play media file encountered an error：WASAPI can't initialize audio client. As shown in the figure below：
Execute the command at the command prompt first：
Then useffplay.exe Play media files.
Six, Resource downloading
The resource download address is athttps://download.csdn.net/download/u014552102/10671629
, Include all resources for the above demonstration. Includingffmpeg.exe,ffplay.exe,mp3 file,pcm Documents, etc..