I plan to record examples of FFMPEG-based Encapsulation Format processing. Including separation, reuse, and Encapsulation Format Conversion of audio and video. Examples of conversion from encapsulation to format have been documented in the previous article: The simplest FFMPEG-based Encapsulation Format converter (without coding/decoding. Therefore, we plan to write three articles to record the muxer and demuxer respectively ). The demuxer record two parts: a simple and a standard one. A simple version is more suitable for beginners.
This article contains 1st articles. First, record a simple version of The FFMPEG-based video/audio Splitter (simplest FFMPEG demuxer simple ). The demuxer separates the compressed video data (for example, H.264) and compressed audio data (for example, AAC) in the encapsulated format data (for example, MKV .. Encoding and decoding are not involved in this process.
The program recorded in this article will encapsulate a FLV file (where the video is encoded as H. 264, audio encoded as MP3) is separated into two files: one H. A 264 encoded video code stream file, an MP3 encoded audio code stream file.
It should be noted that this article introduces a simple version of the video/audio Splitter (demuxer ). The advantage of this splitter is that the code is very simple and easy to understand. However, the disadvantage is that it does not apply to some formats. There is no problem with MP3-encoded audio. However, when the AAC code streams in MP4, FLV, or MKV formats are separated, the obtained AAC code streams cannot be played. The reason is that the data in the avpacket data field that stores AAC data does not contain the "headers" of the 7-byte ADTs file header, you cannot directly decode the video (if you manually add a 7-byte ADTs File Header before the data ).
Reference: Use the FFMPEG class library to separate audio streams in multimedia files
Detach H.264 from some encapsulation formats
Separates H from some encapsulation formats (such as MP4, FLV, and MKV. 264, SPS and PPS must be written first. Otherwise, the separated data cannot be played without SPs or PPS. The SPS and PPS Information of H.264 code streams are stored in extradata of the avcodeccontext structure. Use the bitstream filter named "hsf-_mp4to0000b" in FFMPEG for processing. There are two processing methods:
(1) Use bitstream filter to process each avpacket (simple)
The data field in each avpacket is filtered by bitstream filter. The key function is av_bitstream_filter_filter (). The sample code is as follows.
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb"); while(av_read_frame(ifmt_ctx, &pkt)>=0){if(pkt.stream_index==videoindex){av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);fwrite(pkt.data,1,pkt.size,fp_video);//...}av_free_packet(&pkt);}av_bitstream_filter_close(h264bsfc);
In the above Code, set the input data and output data (corresponding to the 4, 5, and 6 parameters respectively) of av_bitstream_filter_filter () to the data field of avpacket.
Note that bitstream filter needs to be initialized and destroyed by av_bitstream_filter_init () and av_bitstream_filter_close () functions ().
After the above Code processing, the data in avpacket has the following changes:
* The starting code of NALU of H.264 is added to the data of each avpacket {0, 0, 1}
* SPS and PPS are added before each IDR frame data.
(2) manually add SPS and PPS (slightly complicated)
After the extradata of avcodeccontext is processed by bitstream filter, SPS and PPS are obtained and copied to each IDR frame. The following code demonstrates the process of writing to SPS and PPS.
FILE *fp=fopen("test.264","ab");AVCodecContext *pCodecCtx=... unsigned char *dummy=NULL; int dummy_len; AVBitStreamFilterContext* bsfc = av_bitstream_filter_init("h264_mp4toannexb"); av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0); fwrite(pCodecCtx->extradata,pCodecCtx-->extradata_size,1,fp); av_bitstream_filter_close(bsfc); free(dummy); Then modify the data of avpacket. Change the first 4 bytes to the start code. The sample code is as follows.
char nal_start[]={0,0,0,1};memcpy(packet->data,nal_start,4);After the two steps above, you can obtain the H.264 stream that can be played, which is more complex than the first method.
Reference: Use the FFMPEG class library to separate H.264 code streams from multimedia files
If the Encapsulation Format is mpeg2ts, the preceding problem does not exist.
Shows the process of the process program. The flowchart shows that you can directly write data in avpacket obtained through av_read_frame () to a file.
Briefly introduce the significance of each important function in the process:
Avformat_open_input (): Open the input file.
Av_read_frame (): gets an avpacket.
Fwrite (): writes data to different files based on the avpacket type.
Paste the code below:
/*** the simplest video/audio splitter based on FFMPEG (Simplified Version) * simplest FFMPEG demuxer simple ** Lei Xiaohua * [email protected] * China Media University/Digital TV technology * Communication University of China/Digital TV technology * http://blog.csdn.net/leixiaohua1020 ** you can separate the video and audio streams in the encapsulated format. * In this example, the FLV file is separated to obtain the H.264 video stream file and the MP3 * audio stream file. ** Note: * this is a simplified video/audio splitter. Unlike the original version, the * Video Stream and the avformatcontext of the audio stream are not output at initialization. Instead, the decoded * avpacket data is directly written into the file through fwrite. The advantage of doing so is that the process is simpler. The disadvantage is that some formats of video and audio streams are not applicable, for example, the AAC code stream in * FLV/MP4/MKV format (the * Data in the avpacket of AAC in the preceding Encapsulation Format lacks the 7-byte ADTs file header ). * ** This software split a media file (in container such as * MKV, FLV, Avi ...) to video and audio bitstream. * In this example, it Demux a flv file to H. 264 bitstream * and MP3 bitstream. * Note: * this is a simple version of "simplest FFMPEG demuxer ". it is * more simple because it doesn't init output video/audio stream's * avformatcontext. it write avpacket's data to files directly. * The Adva Ntages of this method is simple. the disadvantages of * This method is it's not suitable for some kind of bitstreams. for * example, AAC bitstream in FLV/MP4/MKV container format (data in * avpacket lack of 7 bytes of ADTs header ). **/# include "stdafx. H "extern" C "{# include" libavformat/avformat. H "}; // '1': Use H. 264 bitstream filter # define use_hsf-bsf 1int main (INT argc, char * argv []) {avformatcont EXT * ifmt_ctx = NULL; avpacket Pkt; int ret, I; int videoindex =-1, audioindex =-1; char * in_filename = "cuc_ieschool.flv "; // Input File URL: char * out_filename_v = "inline"; // output file name (output file URL) char * out_filename_a = "cuc_ieschool.mp3"; av_register_all (); // input (input) if (ret = avformat_open_input (& ifmt_ctx, in_filename, 0, 0) <0) {printf ("cocould not open input file. "); Return-1;} If (R ET = avformat_find_stream_info (ifmt_ctx, 0) <0) {printf ("failed to retrieve input stream information"); Return-1;} videoindex =-1; for (I = 0; I nb_streams; I ++) {If (ifmt_ctx-> streams [I]-> codec-> codec_type = avmedia_type_video) {videoindex = I;} else if (ifmt_ctx-> streams [I]-> codec-> codec_type = avmedia_type_audio) {audioindex = I ;}} // dump format ------------------ printf ("\ ninput video ================== =============\ N "); av_dump_format (ifmt_ctx, 0, in_filename, 0 ); printf ("\ n ======================================== ===\ N "); file * fp_audio = fopen (out_filename_a, "WB +"); file * fp_video = fopen (out_filename_v, "WB +");/* fix: H. 264 in some container format (FLV, MP4, MKV etc .) need "hsf-_mp4tow. B" bitstream filter (BSF) * Add SPS, PPs in front of IDR frame * Add start code (",") in front of naluh.264 in some Container (mpeg2ts) Don't need this BSF. */# If needed * hsf-bsfc = av_bitstream_filter_init ("hsf-_mp4to1_ B"); # endifwhile (av_read_frame (ifmt_ctx, & Pkt)> = 0) {If (Pkt. stream_index = videoindex) {# If use_hsf-bsfav_bitstream_filter_filter (hsf-bsfc, ifmt_ctx-> streams [videoindex]-> codec, null, & Pkt. data, & Pkt. size, Pkt. data, Pkt. size, 0); # endifprintf ("write Video packet. size: % D \ tpts: % d \ n ", Pkt. size, Pkt. PTS); fwrite (Pkt. data, 1, Pkt. size, fp_video);} else if (Pkt. stream_index = audioindex) {/* AAC in some container format (FLV, MP4, MKV etc .) need to add 7 bytesadts header in front of avpacket data manually. other audio codec (MP3 ...) works well. */printf ("Write audio packet. size: % d \ tpts: % d \ n ", Pkt. size, Pkt. PTS); fwrite (Pkt. data, 1, Pkt. size, fp_audio) ;}av_free_packet (& Pkt) ;}# if use_h2 64bsfav_bitstream_filter_close (hsf-bsfc); # endiffclose (fp_video); fclose (fp_audio); avformat_close_input (& ifmt_ctx); If (Ret <0 & RET! = Averror_eof) {printf ("error occurred. \ n"); Return-1;} return 0 ;}
The result input file is:
Cuc_ieschool.flv: FLV encapsulates formatted data.
The output file is:
Cuc_ieschool.h264: H.264 video stream data.
Cuc_ieschool.mp3: MP3 audio stream data.
Download Resources
SourceForge project homepage:
Https://sourceforge.net/projects/simplestffmpegformat/
Download csdn
Http://download.csdn.net/detail/leixiaohua1020/8005317
The project contains four examples:
Simplest_ffmpeg_demuxer_simple: video/audio Splitter (simplified version ).
Simplest_ffmpeg_demuxer: Specifies the audio/video Splitter (later articles ).
Simplest_ffmpeg_muxer: audio and video multiplexing (later articles ).
Simplest_ffmpeg_remuxer: Encapsulation Format converter (recorded ).
The simplest FFMPEG-based Encapsulation Format processing: demuxer-simple)