Advertisement
Guest User

Grabframes

a guest
Mar 24th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. bool grabFrames(ffmpeg::AVFormatContext* pFormatCtx)
  2. {
  3.     videoStream=-1;
  4.     for (unsigned i = 0; i < pFormatCtx->nb_streams; i++)
  5.     {
  6.         qDebug() << "FFmpeg stream index" << i << ":";
  7.         ffmpeg::AVStream* avstream = pFormatCtx->streams[i];
  8.         if (!avstream || !avstream->codec)
  9.         {
  10.             qDebug() << "No valid stream or codec, skipping...";
  11.             continue;
  12.         }
  13.         // considering only video streams, skipping audio
  14.         if (avstream->codec->codec_type != ffmpeg::AVMEDIA_TYPE_VIDEO)
  15.         {
  16.             qDebug() << "Skipping audio";
  17.             continue;
  18.         }
  19.  
  20.         // find the codec
  21.         ffmpeg::AVCodec* videoCodec = avcodec_find_decoder(avstream->codec->codec_id);
  22.         if (videoCodec == NULL)
  23.         {
  24.             qDebug() << "Decoder not found, skipping...";
  25.             continue;
  26.         }
  27.  
  28.         qDebug() << "Encoding codec" << videoCodec->name;
  29.         // skip if the codec can't be open
  30.         if (avcodec_open2(avstream->codec, videoCodec, NULL) < 0)
  31.         {
  32.             qDebug() << "Decoder failed to open, skipping...";
  33.             continue;
  34.         }
  35.  
  36.         pFrame = ffmpeg::avcodec_alloc_frame();
  37.         pCodecCtx = avstream->codec;
  38.  
  39.         // Obtain from stream's number of frames if specified. Will be 0 if unknown.
  40.         int frames = avstream->nb_frames;
  41.  
  42.         if (frames)
  43.             qDebug() << "      Obtained from AVStream::nb_frames" << frames;
  44.  
  45.         if(videoCodec->capabilities&CODEC_CAP_TRUNCATED)
  46.                 pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; // we do not send complete frames
  47.  
  48.         // We are ready to extract the frames
  49.  
  50.         while(av_read_frame(pFormatCtx, &packet) >= 0)
  51.         {
  52.            avcodec_get_frame_defaults(pFrame);
  53.            int frameFinished;
  54.  
  55.            int data;
  56.            if(packet.stream_index==i)
  57.            {
  58.               // Is this a packet from the video stream -> decode video frame
  59.  
  60.               data = avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
  61.               if(data < -1)
  62.                   return false;
  63.               // Did we get a video frame?
  64.               if(frameFinished)
  65.               {
  66.                 qDebug() << "Codec reported frame number to be " << pCodecCtx->frame_number;
  67.               }
  68.  
  69.            }
  70.            av_free_packet(&packet);      // Free the packet that was allocated by av_read_frame
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement