Record incoming stream using libavformat

How could I record a incoming stream using ffmepg’s lib?
I did the following code:

 while(!done)
   {

      // Read a frame
      th->reset(1000000);
      if(av_read_frame(pFormatCtx, &packet)<0)
         return false;                             // Frame read failed (e.g. end of stream)

      if(packet.stream_index==videoStream)
      {
          ////////////////////////////////////////////////////////////////////////
         // Is this a packet from the video stream -> decode video frame
          if (stream == NULL){//create stream in file
              stream = avformat_new_stream(oc, pFormatCtx->streams[videoStream]->codec->codec);
              avcodec_copy_context(stream->codec, pFormatCtx->streams[videoStream]->codec);
              stream->sample_aspect_ratio = pFormatCtx->streams[videoStream]->codec->sample_aspect_ratio;
              avformat_write_header(oc, NULL);
          }
          av_write_frame(oc, &packet);
       }
    }

But the recorded file is very very large, 8MB/s where the incoming stream is only 1024 kbps…
And the file is not playable. What is wrong here?


Source: c++

Leave a Reply