Reviewing the whole life cycle of multimedia files, it can be roughly divided into four stages: acquisition, processing, transmission and playback, and encapsulation and coding are two common processing methods.
In order to save storage space and improve transmission speed, we need to compress and encode the original video or audio. For video, the common coding method is H264(MPEG-4 Advanced Video coding) coding; For audio, the common encoding method is AAC (Advanced Audio coding). The file name extension of an H264 encoded video file is.h264. The file name extension of an aAC encoded audio file is.aac.
So why encapsulate it? Encapsulation is the consolidation of audio and video into a single file. Imagine if we had to turn on the video and audio separately while watching a movie. Encapsulation technologies can be implemented in different formats, with different implementations leading to different Container formats. MP4(MPEG-4), Mov(QuickTime Movie), and Flv(Flash Video) are all common packaging formats in daily life.
Where there is encapsulation, there is unencapsulation. As the name suggests, decapsulation is the splitting of encapsulated files into video and audio files.
I. Case description
This time we will implement an example of splitting “Titanic. Mp4” multimedia files into “Titanic. H264” and “Titanic. Aac”. The main function source code is attached below.
int ret=0; int video_index,audio_index; AVFormatContext *fmt_ctx=NULL; AVPacket pkt; FILE *video_dst_file=NULL; FILE *audio_dst_file=NULL; *1* ret=avformat_open_input(&fmt_ctx,"Titanic. //*2* avformat_find_stream_info(fmt_ctx,NULL); //*3* av_dump_format(fmt_ctx,-1,"Titanic.mp4",0); //*4* video_index=av_find_best_stream(fmt_ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0); audio_index=av_find_best_stream(fmt_ctx,AVMEDIA_TYPE_AUDIO,-1,-1,NULL,0); / / input module / / * 5 * video_dst_file = fopen (" Titanic. H264 ", "wb"); audio_dst_file=fopen("Titanic.aac","wb"); // Data transfer module av_init_packet(&pkt); While (av_read_frame(FMT_CTx, &pkT)>=0) {if(pkt.stream_index==video_index) {//*7* writes the video encoding frame to the video output file fwrite(pkt.data,1,pkt.size,video_dst_file); } else if (PKT) stream_index = = audio_index) {/ / * 7 * write audio coding frame to audio output file fwrite (PKT) data, 1 PKT. Size, audio_dst_file); } av_packet_unref(&pkt); } fclose(video_dst_file); fclose(audio_dst_file); avformat_close_input(&fmt_ctx); return 0;Copy the code
2. Operation procedure
*1. Open and input the multimedia file “Titanic. Mp4”
*2. Get stream information
*3. Find the video stream and the audio stream separately
*4. Print input file details
*5. Open output files “Titanate. h264” and “titanate. aac” respectively in “WB” mode.
*6. Get encoding frames from output files
*7. Write files “Titanic. H264” and “Titanic.
*8. Free memory
Modules and apis
I divided the whole program into three modules: input module, output module, data transmission module. The input module corresponds to Step 1-4, the output module to Step 5, and the data transmission module to Step 6-7.
4. Running results
Open the command prompt window, switch to the directory where the output files “Titanic. H264” and “Titanic. Aac” are located, execute the command “ffplay Titanic.
Execute “ffplay Titanic. H264” command, we can also see it play normally.