Based on the simplest ffmpeg encapsulation process: Video and audio splitter start-up (Demuxer-simple)

Source: Internet
Author: User
Tags flv file

=====================================================

A series of articles based on the simplest ffmpeg packaging process are listed:

Simplest FFmpeg-based package format processing: AV splitter Lite (demuxer-simple)

Simplest FFmpeg-based package format processing: AV Splitter (demuxer)

Simplest FFmpeg-based package format processing: AV multiplexer (muxer)

Simplest ffmpeg-based Encapsulation Format processing: Encapsulation format conversion (remuxer)

=====================================================


Brief introduction

I'm going to take a look at a sample of package format processing based on FFmpeg. Includes video-Audio separation, multiplexing, and package format conversion. A sample of the conversion of the sealed to format is already documented in the previous article: "The simplest ffmpeg-based package Format converter (no codec)". No more repeating.

Therefore, it is planned to write 3 articles that record av multiplexers (muxer) and separators (Demuxer) respectively. The video audio separator (Demuxer) records 2: A simple, a standard.

The simple version number is better for people who just start learning.

This article is the 1th chapter.

A simple version of the FFmpeg-based AV Splitter (simplest FFmpeg Demuxer simplicity) is recorded first. The AV splitter (Demuxer) is the separation of video-compressed data (such as MKV) and audio-compressed data (such as AAC) from the encapsulated format data (such as a.

What you see. Encoding and decoding are not involved in this process.



The program documented in this document separates an FLV encapsulated file (in which the video is encoded as H. Two and the audio encoding is MP3) into a file that has been encoded as a video stream file encoded by H. x, a MP3 coded audio stream file.

It is important to note that. This article describes a simple version of the AV Splitter (Demuxer).

The advantage of this splitter is that the code is very simple and very well understood.

However, the disadvantage is that it does not apply to some formats.

There is no problem with MP3 encoded audio. However, in the separation of mp4/flv/mkv and other formats in the AAC code stream, the resulting AAC stream is not playable.

The reason is that the data in the Avpacket data field that stores the AAC information is not included in the "header" data of the 7-byte Adts file header, and is not decoded directly (assuming, of course, that the data will be able to be played by manually adding a 7-byte Adts file header in front of it).

Article: Using the FFmpeg class library to isolate audio streams in multimedia files


Separating H. I in some encapsulation formats

The time to separate some of the package formats (such as MP4/FLV/MKV, etc.) from H. It is necessary to write SPS and PPS first, otherwise the separated data will not be able to be played without SPS or PPS. The SPS and PPS information for the H-stream is stored in the extradata of the avcodeccontext structure.

You need to use the Bitstream filter processing named "H264_mp4toannexb" in FFmpeg. There are two ways of handling this:

(1) Use bitstream filter to process each avpacket (simple)

The data field in each avpacket is filtered through the bitstream filter. The key function is Av_bitstream_filter_filter (). Demo sample code such as the following.

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 code above. It is possible to set the input data of av_bitstream_filter_filter () and the output data (respectively, corresponding to 4,5,6,7) to the data field of Avpacket.

It is important to note that bitstream filter needs to be initialized and destroyed, respectively, by function Av_bitstream_filter_init () and Av_bitstream_filter_close ().

After the above code is processed, the data in the Avpacket has the following changes, for example:

* Each avpacket data is added to the starting code of H. Nalu {0,0,0,1}

* SPS and PPS are added in front of each IDR frame data

(2) manually add SPS. PPS (slightly complex)

Avcodeccontext extradata data is processed by Bitstream filter to obtain SPS, PPS. Copied to each IDR frame. The following code shows a sample of the process of writing to the SPS, 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 change the avpacket data. Change the first 4 bytes to a start code.

The demo sample code looks like the following.

Char nal_start[]={0,0,0,1};memcpy (packet->data,nal_start,4);
The above two steps are also able to be able to play the code stream of H, relative to the first method is more complex.

Article: Using the FFmpeg class library to isolate the H-code stream in a multimedia file

This problem does not exist when the package format is mpeg2ts.


Process processes such as those seen in the process.

As can be seen from the flowchart, the data in each of the Avpacket obtained through Av_read_frame () is written directly to the file.
Describe the significance of each important function in the process:
Avformat_open_input (): Open the input file.
Av_read_frame (): Gets a avpacket.
Fwrite (): Varies according to the type of avpacket obtained. Write to a different file, respectively.



Code

Paste the following code:

/** * Simplest FFmpeg based AV splitter (simplified version) * Simplest FFmpeg Demuxer simple * * Lei hua Lei xiaohua * [email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program can be packaged in the format of video stream data and audio code The flow data is separated out. * In this example, the FLV file is separated to get the H. Video stream file and the MP3 * audio stream file. * * NOTE: * This is a simplified version of the AV splitter. Unlike the original, there is no initialization output * Avformatcontext of the video stream and audio stream. Instead, the decoded data in the resulting * Avpacket is written directly to the file via Fwrite (). The advantage of this is that the process is simpler than *.

The downside is that for some formats the AV stream is not applicable, for example * flv/mp4/mkv in the format of AAC stream (the data in the AAC Avpacket in the package format is missing 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. bitstream * and MP3 bitstream. * NOTE: * This was a simple version of "simplest FFmpeg Demuxer". It's * more simple because it doesn ' t init Output video/audio stream ' s * avformatcontext. It Write Avpacket ' s data to files directly. * The advantages of this method are 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 <stdio.h> #define __STDC_CONSTANT_MACROS#IFDEF _win32//windowsextern "C" {#include "libavformat/ Avformat.h "}; #else//linux ... #ifdef __cplusplusextern" C "{#endif # include <libavformat/avformat.h> #ifdef __ Cplusplus}; #endif #endif//' 1 ': Use H. bitstreamFilter #define USE_H264BSF 1int Main (int argc, char* argv[]) {Avformatcontext *ifmt_ctx = NULL; Avpacket pkt;int ret, i;int videoindex=-1,audioindex=-1;const char *in_filename = "cuc_ieschool.flv";//Input file URLcon St Char *out_filename_v = "cuc_ieschool.h264";//output file urlconst char *out_filename_a = "Cuc_ieschool.mp3"; av_ Register_all ();//inputif ((ret = Avformat_open_input (&ifmt_ctx, in_filename, 0, 0)) < 0) {printf ("Could not open I Nput file. "); return-1;} if (ret = avformat_find_stream_info (ifmt_ctx, 0)) < 0) {printf ("Failed to retrieve input stream information"); return -1;} Videoindex=-1;for (i=0; i<ifmt_ctx->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);p rintf ("\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 "H264_MP4TOANNEXB" bitstream filter (BSF) *add Sps,pps in Front of IDR frame *add start code ("0,0,0,1") in front of naluh.264 in some container (mpeg2ts) don ' t need this bsf.*/# If use_h264bsfavbitstreamfiltercontext* H264BSFC = Av_bitstream_filter_init ("H264_mp4toannexb"); #endifwhile (Av_read_frame (Ifmt_ctx, &AMP;PKT) >=0) {if (pkt.stream_index==videoindex) {#if use_h264bsfav_ Bitstream_filter_filter (H264BSFC, Ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, Pkt.data, pkt.size, 0); #endifprintf ("Write Video Packet. Size:%d\tpts:%lld\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 I n Front of Avpacket data manually. Other Audio Codec (MP3 ...) works well.*/printf ("Write Audio Packet. Size:%d\tpts:%lld\n ", pkt.size,pkt.pts); fwrite (Pkt.data,1,pkt.size,fp_audio);} Av_free_packet (&AMP;PKT);} #if use_h264bsfav_bitstream_filter_close (H264BSFC); #endiffclose (Fp_video); fclose (Fp_audio); Avformat_close_input (&AMP;IFMT_CTX); if (Ret < 0 && ret! = Averror_ EOF) {printf ("Error occurred.\n"); return-1;} return 0;}



Results

The input file is:
Cuc_ieschool.flv:FLV encapsulates the format data.

The output file is:
cuc_ieschool.h264:h.264 video bitstream data.
CUC_IESCHOOL.MP3:MP3 Audio bitstream data.


Download


Simplest ffmpeg format


Project Home

sourceforge:https://sourceforge.net/projects/simplestffmpegformat/

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_format

Open source China: Http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_format


CSDN:

http://download.csdn.net/detail/leixiaohua1020/8005317


4 examples are included in the project:

Simplest_ffmpeg_demuxer_simple: Video-Audio splitter (simplified version).

Simplest_ffmpeg_demuxer: Video-Audio splitter.

Simplest_ffmpeg_muxer: Video audio multiplexer.

Simplest_ffmpeg_remuxer: Encapsulates the format converter.


Update -1.1==================================================

Fixes the following issues:
(1) Execution issues under Release version number
(2) Simplest_ffmpeg_muxer package H/A lost sound when the bare stream

CSDN Download

http://download.csdn.net/detail/leixiaohua1020/8284309


Updated-1.2 (2015.2.13) =========================================

This time considering the cross-platform requirements, adjusted the source code. After this adjustment. Source code can be compiled on the following platform:

VC + +: Open the SLn file to compile. No configuration required.

Cl.exe: Open Compile_cl.bat can be used to compile the command line under the cl.exe, note that you may need to follow the VC installation path to adjust the number of parameters in the script. Compile commands such as the following.

:: VS2010 environmentcall "D:\Program Files\Microsoft Visual Studio 10.0\vc\vcvarsall.bat":: Include@set include= Include;%i Nclude%::lib@set Lib=lib;%lib%::compile and Linkcl simplest_ffmpeg_demuxer_simple.cpp/link avcodec.lib avformat.lib Avutil.lib ^avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib/opt:noref

The MINGW:MINGW command-line execution compile_mingw.sh can be compiled using MinGW's g++. Compile commands such as the following.

g++ simplest_ffmpeg_demuxer_simple.cpp-g-o simplest_ffmpeg_demuxer_simple.exe-i/usr/local/include-l/usr/local/ Lib-lavformat-lavcodec-lavutil

Gcc:linux or MacOS command line execution compile_gcc.sh can be compiled using GCC. Compile commands such as the following.

GCC simplest_ffmpeg_demuxer_simple.cpp-g-o simplest_ffmpeg_demuxer_simple.out-i/usr/local/include-l/usr/local/ Lib-lavformat-lavcodec-lavutil

PS: The related compilation commands have been saved in the project directory


csdn:http://download.csdn.net/detail/leixiaohua1020/8445303

SourceForge it has been updated.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Based on the simplest ffmpeg encapsulation process: Video and audio splitter start-up (Demuxer-simple)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.