Program code: Using FFmpeg to pull streaming from the network

Source: Internet
Author: User

#include "stdafx.h"


extern "C"
{
#include "libavcodec/avcodec.h"
#include "Libavformat/avformat.h"
};


#pragma comment (lib, "Avcodec.lib")
#pragma comment (lib, "Avformat.lib")


* 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 ();


/* INPUT STREAM * *


Input_filepath = "RTMP://LIVE.HKSTV.HK.LXDNS.COM/LIVE/HKS";


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];


/* OUTPUT STREAM * *


Output_filepath = "Rtmp://127.0.0.1/live/mytest";
Output_filepath = "output.flv";


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;
}


Pvideostream_out = Avformat_new_stream (Ofmt_ctx, NULL);
if (NULL = = pvideostream_out)
{
printf ("Error:avformat_new_stream, Output video\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;
}


if (Avcodec_copy_context (Pvideostream_out->codec, Pvideostream->codec) < 0)
{
printf ("Error:avcodec_copy_context, Output video\n");
Goto end;
}


Create and initialize a aviocontext for accessing the resource.
if (Avio_open (&AMP;OFMT_CTX-&GT;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


#if USE_AACBSF
avbitstreamfiltercontext* AACBSFC = Av_bitstream_filter_init ("Aac_adtstoasc");
#endif


Initialize optional fields of a packet with default values.
Av_init_packet (&AMP;IPKT);
Ipkt.data = NULL;
ipkt.size = 0;


while (1)
{
Return the next frame of a stream.
if (Av_read_frame (Ifmt_ctx, &AMP;IPKT) < 0)
Break


if (Ipkt.stream_index = = Idx_audio)
{
#if USE_AACBSF
Filter bitstream.
Av_bitstream_filter_filter (AACBSFC, Paudiostream->codec, NULL, &ipkt.data, &ipkt.size, Ipkt.data, Ipkt.size, 0);
#endif
Write a packet to a output media file ensuring correct interleaving.
if (Av_interleaved_write_frame (Ofmt_ctx, &AMP;IPKT) < 0)
{
printf ("Error:av_interleaved_write_frame, Output audio\n");
Break
}


}
else if (Ipkt.stream_index = = Idx_video)
{
#if USE_H264BSF
Av_bitstream_filter_filter (H264BSFC, Pvideostream->codec, NULL, &ipkt.data, &ipkt.size, Ipkt.data, Ipkt.size, 0);
#endif
if (Av_interleaved_write_frame (Ofmt_ctx, &AMP;IPKT) < 0)
{
printf ("Error:av_interleaved_write_frame, Output video\n");
Break
}
}
Else
{
Continue
}


Free a packet.
Av_free_packet (&AMP;IPKT);
}


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


#if USE_AACBSF
Av_bitstream_filter_close (AACBSFC);
#endif


End:
Close a opened input avformatcontext.free it and all its contents and set *s to NULL.
Avformat_close_input (&AMP;IFMT_CTX);

Close the resource accessed by the Aviocontext s and free it.
if (NULL!= ofmt_ctx)
Avio_close (OFMT_CTX-&GT;PB);

Free a avformatcontext and all its streams.
Avformat_free_context (OFMT_CTX);


return 0;
}

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.