Created
March 27, 2013 08:55
-
-
Save joshkunz/5252787 to your computer and use it in GitHub Desktop.
This is the important part.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Allocate video frame | |
pFrame=avcodec_alloc_frame(); | |
// Allocate an AVFrame structure | |
pFrameRGB=avcodec_alloc_frame(); | |
if(pFrameRGB==NULL) | |
return -1; | |
// Determine required buffer size and allocate buffer | |
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, | |
pCodecCtx->height); | |
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); | |
sws_ctx = | |
sws_getContext | |
( | |
pCodecCtx->width, | |
pCodecCtx->height, | |
pCodecCtx->pix_fmt, | |
pCodecCtx->width, | |
pCodecCtx->height, | |
PIX_FMT_RGB24, | |
SWS_BILINEAR, | |
NULL, | |
NULL, | |
NULL | |
); | |
// Assign appropriate parts of buffer to image planes in pFrameRGB | |
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset | |
// of AVPicture | |
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, | |
pCodecCtx->width, pCodecCtx->height); | |
// Read frames and save first five frames to disk | |
i=0; | |
while(av_read_frame(pFormatCtx, &packet)>=0) { | |
// Is this a packet from the video stream? | |
if(packet.stream_index==videoStream) { | |
// Decode video frame | |
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, | |
&packet); | |
// Did we get a video frame? | |
if(frameFinished) { | |
// Convert the image from its native format to RGB | |
sws_scale | |
( | |
sws_ctx, | |
(uint8_t const * const *)pFrame->data, | |
pFrame->linesize, | |
0, | |
pCodecCtx->height, | |
pFrameRGB->data, | |
pFrameRGB->linesize | |
); | |
// Save the frame to disk | |
if(++i<=5) | |
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, | |
i); | |
} | |
} | |
// Free the packet that was allocated by av_read_frame | |
av_free_packet(&packet); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment