Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool grabFrames(ffmpeg::AVFormatContext* pFormatCtx)
- {
- videoStream=-1;
- for (unsigned i = 0; i < pFormatCtx->nb_streams; i++)
- {
- qDebug() << "FFmpeg stream index" << i << ":";
- ffmpeg::AVStream* avstream = pFormatCtx->streams[i];
- if (!avstream || !avstream->codec)
- {
- qDebug() << "No valid stream or codec, skipping...";
- continue;
- }
- // considering only video streams, skipping audio
- if (avstream->codec->codec_type != ffmpeg::AVMEDIA_TYPE_VIDEO)
- {
- qDebug() << "Skipping audio";
- continue;
- }
- // find the codec
- ffmpeg::AVCodec* videoCodec = avcodec_find_decoder(avstream->codec->codec_id);
- if (videoCodec == NULL)
- {
- qDebug() << "Decoder not found, skipping...";
- continue;
- }
- qDebug() << "Encoding codec" << videoCodec->name;
- // skip if the codec can't be open
- if (avcodec_open2(avstream->codec, videoCodec, NULL) < 0)
- {
- qDebug() << "Decoder failed to open, skipping...";
- continue;
- }
- pFrame = ffmpeg::avcodec_alloc_frame();
- pCodecCtx = avstream->codec;
- // Obtain from stream's number of frames if specified. Will be 0 if unknown.
- int frames = avstream->nb_frames;
- if (frames)
- qDebug() << " Obtained from AVStream::nb_frames" << frames;
- if(videoCodec->capabilities&CODEC_CAP_TRUNCATED)
- pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; // we do not send complete frames
- // We are ready to extract the frames
- while(av_read_frame(pFormatCtx, &packet) >= 0)
- {
- avcodec_get_frame_defaults(pFrame);
- int frameFinished;
- int data;
- if(packet.stream_index==i)
- {
- // Is this a packet from the video stream -> decode video frame
- data = avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
- if(data < -1)
- return false;
- // Did we get a video frame?
- if(frameFinished)
- {
- qDebug() << "Codec reported frame number to be " << pCodecCtx->frame_number;
- }
- }
- av_free_packet(&packet); // Free the packet that was allocated by av_read_frame
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement