* Rei
fix:h.264 in some container format (FLV, MP4, MKV etc.) need "H264_mp4toannexb" bitstream filter (BSF)
* Add Sps,pps in front IDR frame
* Add Start code ("0,0,0,1") in front of Nalu
H.264 in some container (mpeg2ts) don ' t need this BSF.
*/
' 1 ': Use H.264 bitstream Filter
#define USE_H264BSF 1
* Rei
FIX:AAC in some container format (FLV, MP4, MKV etc.) need "AAC_ADTSTOASC" bitstream filter (BSF)
*/
' 1 ': Use AAC bitstream Filter
#define USE_AACBSF 1
int main (int argc, char* argv[])
{
Variables Definition
avformatcontext* ifmt_ctx = NULL; Format context of input stream
Const char* Input_filepath = NULL; File Path of input stream
int idx_audio =-1; Index of input audio stream
int idx_video =-1; Index of input video stream
avstream* paudiostream = NULL; Stream structure of input audio stream
avstream* pvideostream = NULL; .. of input video stream
avformatcontext* ofmt_ctx = NULL; Format context of output stream
avstream* paudiostream_out = NULL; Output Audio Stream (AAC)
avstream* pvideostream_out = NULL; Output Video Stream (H264)
Const char* Output_filepath = NULL; File Path of output stream
Avpacket ipkt = {0}; //
Variables Definition End
Initialize Libavformat and register all the muxers, Demuxers and *protocols
Av_register_all ();
Do global initialization of network components
Avformat_network_init ();
Open an input stream and read the header. The codecs are not opened.
if (Avformat_open_input (&ifmt_ctx, input_filepath, NULL, NULL) < 0)
{
printf ("error:avformat_open_input\n");
Goto end;
}
Read packets of a media file to get stream information.
if (Avformat_find_stream_info (Ifmt_ctx, NULL) < 0)
{
printf ("error:avformat_find_stream_info\n");
Goto end;
}
Find the stream in the file.
for (int i = 0; i < ifmt_ctx->nb_streams; i++)
{
Audio stream
if (Avmedia_type_audio = = Ifmt_ctx->streams[i]->codec->codec_type)
{
Idx_audio = i;
}
Video stream
else if (Avmedia_type_video = = Ifmt_ctx->streams[i]->codec->codec_type)
{
Idx_video = i;
}
Else
{
Break
}
}
if (Idx_video < 0 | | Idx_audio < 0)
{
printf ("Error:can not find no audio stream or video stream\n");
Goto end;
}
Input audio stream and input video stream
Paudiostream = ifmt_ctx->streams[idx_audio];
Pvideostream = ifmt_ctx->streams[idx_video];
Allocate an avformatcontext to an output format.
Avformat_alloc_output_context2 (&ofmt_ctx, NULL, "flv", Output_filepath);
if (NULL = = Ofmt_ctx)
{
printf ("Error:avformat_alloc_output_context2, Output stream\n");
Goto end;
}
Add a new stream to a media file.
Paudiostream_out = Avformat_new_stream (Ofmt_ctx, NULL);
if (NULL = = paudiostream_out)
{
printf ("Error:avformat_new_stream, Output audio\n");
Goto end;
}
Copy the settings of the source Avcodeccontext into the destination avcodeccontext.
if (Avcodec_copy_context (Paudiostream_out->codec, Paudiostream->codec) < 0)
{
printf ("Error:avcodec_copy_context, Output audio\n");
Goto end;
}
Create and initialize a aviocontext for accessing the resource.
if (Avio_open (&OFMT_CTX->PB, Output_filepath, Avio_flag_write) < 0)
{
printf ("Error:avio_open, Output stream\n");
Goto end;
}
Allocate the stream private data and write the stream header to a output media file.
if (Avformat_write_header (Ofmt_ctx, NULL) < 0)
{
printf ("Error:avformat_write_header, Output stream\n");
Goto end;
}
Print detailed information about the input or output format.
Av_dump_format (Ifmt_ctx,-1, Input_filepath, 0);
Av_dump_format (Ofmt_ctx,-1, Output_filepath, 1);
#if USE_H264BSF
Create and initialize a bitstream filter context given a bitstream filter name.
avbitstreamfiltercontext* H264BSFC = Av_bitstream_filter_init ("H264_mp4toannexb");
#endif
Write the stream trailer to a output media file and free the file private data.
if (0!= av_write_trailer (ofmt_ctx))
{
printf ("error:av_write_trailer\n");
Goto end;
}
#if USE_H264BSF
Release Bitstream the filter context.
Av_bitstream_filter_close (H264BSFC);
#endif
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.