<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi Everyone,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I’ve an IP Camera which gives RTSP stream on <b>rtsp://192.168.1.90/MediaInput/Mpeg4. </b>I want to archive the video and audio of this stream using libavcodec. I’ve the following code sample. This code creates a video file. But, time_base.den/time_base.num value(which is frame rate?) comes out to be 30000. I tried to set the value of time_base.num to 1000 to get frame rate as 30. I get one hour duration video for 100 frames.    <o:p></o:p></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Courier New";color:blue'>int</span><span style='font-size:12.0pt;font-family:"Courier New"'> main(<span style='color:blue'>int</span> argc, <span style='color:blue'>char</span>** argv) {</span><o:p></o:p></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    AVFormatContext* context = avformat_alloc_context();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>int</span> video_stream_index;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>int</span> audio_stream_index;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    av_register_all();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avcodec_register_all();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avformat_network_init();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:green'>//open rtsp<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avformat_open_input(&context, <span style='color:#A31515'>"rtsp://192.168.1.90/MediaInput/Mpeg4"</span>,NULL,NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-indent:.5in;text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>avformat_find_stream_info(context,NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:green'>//search video stream<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>for</span>(<span style='color:blue'>int</span> i =0;i<context->nb_streams;i++)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        <span style='color:blue'>if</span>(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          {        <o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>              video_stream_index = i;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          <span style='color:blue'>else</span> <span style='color:blue'>if</span>(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          {        <o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>              audio_stream_index = i;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    AVPacket packet;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    av_init_packet(&packet);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:green'>//open output file<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    AVOutputFormat* fmt = av_guess_format(NULL,<span style='color:#A31515'>"test2.avi"</span>,NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    AVFormatContext* oc = avformat_alloc_context();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    oc->oformat = fmt;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avio_open2(&oc->pb, <span style='color:#A31515'>"test.avi"</span>, AVIO_FLAG_WRITE,NULL,NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    AVStream* stream=NULL;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>int</span> cnt = 0;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     AVCodec *videoCodec;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     AVCodecContext *videoCodecCtx;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <span style='color:green'>// Get a pointer to the codec context for the video stream<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     videoCodecCtx=context->streams[video_stream_index]->codec;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <span style='color:green'>// Find the decoder for the video stream<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     videoCodec=avcodec_find_decoder(videoCodecCtx->codec_id);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <span style='color:green'>// Open codec<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     avcodec_open(videoCodecCtx, videoCodec);<span style='color:green'><o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New";color:green'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <span style='color:blue'>if</span>(videoCodec->capabilities & CODEC_CAP_TRUNCATED)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          videoCodecCtx->flags |= CODEC_FLAG_TRUNCATED; <span style='color:green'>/* we do not send complete frames */<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New";color:green'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <span style='color:blue'>if</span> ((videoCodecCtx->time_base.den / videoCodecCtx->time_base.num) > 1000 && videoCodecCtx->time_base.num == 1)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          videoCodecCtx->time_base.num = 1000;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>     <o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    av_read_play(context);<span style='color:green'>//play RTSP<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>while</span>(av_read_frame(context,&packet)>=0 && cnt <100){<span style='color:green'>//read 100 frames<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        <span style='color:blue'>if</span>(packet.stream_index == video_stream_index){<span style='color:green'>//packet is video  <o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>            <span style='color:blue'>if</span>(stream == NULL){<span style='color:green'>//create stream in file<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>                stream = avformat_new_stream(oc,context->streams[video_stream_index]->codec->codec);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>                avcodec_copy_context(stream->codec,context->streams[video_stream_index]->codec);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>                stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>                avformat_write_header(oc,NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>            }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>            packet.stream_index = stream->id;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>            av_write_frame(oc,&packet);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>              cnt++; <o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        <span style='color:blue'>else</span> <span style='color:blue'>if</span>(packet.stream_index == audio_stream_index)<span style='color:green'>//packet is audio<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          <span style='color:green'>//std::cout<<"Audio packet"<<std::endl;<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>          cnt++;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        av_free_packet(&packet);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>        av_init_packet(&packet);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    av_read_pause(context);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    av_write_trailer(oc);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avio_close(oc->pb);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'>    avformat_free_context(oc);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:12.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Courier New"'>    <span style='color:blue'>return</span> (EXIT_SUCCESS);</span><o:p></o:p></p><p class=MsoNormal>}<o:p></o:p></p><p class=MsoNormal>Thanks<o:p></o:p></p><p class=MsoNormal>Naresh<o:p></o:p></p><p class=MsoNormal>Ph. 8884199804<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>