title: Record RTSP stream with FFmpeg libavformat
body: <p>I'm trying to record RTSP stream from Axis camera with FFmpeg libavformat.
  I can grab video from files and then save it to another file, this is OK. But camera sends strange data, FPS is 100 and camera sends every 4th frame so result FPS is about 25. But libavformat set packets dts/pts for 90000 fps (default?) and new file stream has 100fps. Result is one hour video with only 100 frames.</p>
  
  <p>Here is my code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;libavcodec/avcodec.h&gt;
  #include &lt;libavformat/avformat.h&gt;
  #include &lt;libavformat/avio.h&gt;
  
  
  int main(int argc, char** argv) {
  
      AVFormatContext* context = avformat_alloc_context();
      int video_stream_index;
  
      av_register_all();
      avcodec_register_all();
      avformat_network_init();
  
      //open rtsp
      if(avformat_open_input(&amp;context, "rtsp://195.200.199.8/mpeg4/media.amp",NULL,NULL) != 0){
          return EXIT_FAILURE;
      }
  
      if(avformat_find_stream_info(context,NULL) &lt; 0){
          return EXIT_FAILURE;
      }
  
      //search video stream
      for(int i =0;i&lt;context-&gt;nb_streams;i++){
          if(context-&gt;streams[i]-&gt;codec-&gt;codec_type == AVMEDIA_TYPE_VIDEO)
              video_stream_index = i;
      }
  
      AVPacket packet;
      av_init_packet(&amp;packet);
  
      //open output file
      AVOutputFormat* fmt = av_guess_format(NULL,"test2.avi",NULL);
      AVFormatContext* oc = avformat_alloc_context();
      oc-&gt;oformat = fmt;
      avio_open2(&amp;oc-&gt;pb, "test.avi", AVIO_FLAG_WRITE,NULL,NULL);
  
      AVStream* stream=NULL;
      int cnt = 0;
      //start reading packets from stream and write them to file
  
      av_read_play(context);//play RTSP
      while(av_read_frame(context,&amp;packet)&gt;=0 &amp;&amp; cnt &lt;100){//read 100 frames
          if(packet.stream_index == video_stream_index){//packet is video               
              if(stream == NULL){//create stream in file
                  stream = avformat_new_stream(oc,context-&gt;streams[video_stream_index]-&gt;codec-&gt;codec);
                  avcodec_copy_context(stream-&gt;codec,context-&gt;streams[video_stream_index]-&gt;codec);
                  stream-&gt;sample_aspect_ratio = context-&gt;streams[video_stream_index]-&gt;codec-&gt;sample_aspect_ratio;
                  avformat_write_header(oc,NULL);
              }
              packet.stream_index = stream-&gt;id;
  
              av_write_frame(oc,&amp;packet);
              cnt++;
          }
          av_free_packet(&amp;packet);
          av_init_packet(&amp;packet);
      }
      av_read_pause(context);
      av_write_trailer(oc);
      avio_close(oc-&gt;pb);
      avformat_free_context(oc);
  
      return (EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>Result file is here: <a href="http://dl.dropbox.com/u/1243577/test.avi">http://dl.dropbox.com/u/1243577/test.avi</a></p>
  
  <p>Thanks for any advice</p>