分类:
2012-12-10 18:30:56
10 从video.avi文件中打开视频流video_stream 20 从视频流中读取包到帧中 30 如果这个帧还不完整,跳到20 40 对这个帧进行一些操作 50 跳回到20 |
#include #include ... int main(int argc, charg *argv[]) { av_register_all(); |
AVFormatContext *pFormatCtx; // Open video file if(av_open_input_file(&pFormatCtx, argv[1],NULL, 0, NULL)!=0) return -1; // Couldn't open file |
// Retrieve stream information if(av_find_stream_info(pFormatCtx)<0) return -1; // Couldn't find streaminformation |
// Dump information about file onto standard error dump_format(pFormatCtx, 0, argv[1], 0); |
int i; AVCodecContext *pCodecCtx; // Find the first video stream videoStream=-1; for(i=0; i if(pFormatCtx->streams->codec->codec_type==CODEC_TYPE_VIDEO){ videoStream=i; break; } if(videoStream==-1) return -1; // Didn't find a video stream // Get a pointer to the codec context for the video stream pCodecCtx=pFormatCtx->streams[videoStream]->codec; |
AVCodec *pCodec; // Find the decoder for the video stream pCodec=avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec==NULL) { fprintf(stderr, "Unsupported codec!\n"); return -1; // Codec not found } // Open codec if(avcodec_open(pCodecCtx, pCodec)<0) return -1; // Could not open codec |
AVFrame *pFrame; // Allocate video frame pFrame=avcodec_alloc_frame(); |
// Allocate an AVFrame structure pFrameRGB=avcodec_alloc_frame(); if(pFrameRGB==NULL) return -1; |
uint8_t *buffer; int numBytes; // 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)); |
// Assign appropriate parts of buffer to image planes inpFrameRGB // Note that pFrameRGB is an AVFrame, but AVFrame is asuperset // of AVPicture avpicture_fill((AVPicture *)pFrameRGB, buffer,PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height); |
int frameFinished; AVPacket packet; i=0; while(av_read_frame(pFormatCtx,&packet)>=0) { // Is this a packet from the videostream? if(packet.stream_index==videoStream) { // Decodevideo frame avcodec_decode_video(pCodecCtx, pFrame,&frameFinished, packet.data,packet.size); // Did weget a video frame? if(frameFinished) { //Convert the image from its native format to RGB img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width,pCodecCtx->height); // Save the frame to disk if(++i<=5) SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i); } } // Free the packet that was allocated byav_read_frame av_free_packet(&packet); } |
关于包Packets的注释 从技术上讲一个包可以包含部分或者其它的数据,但是ffmpeg的解释器保证了我们得到的包Packets包含的要么是完整的要么是多种完整的帧。 |
void SaveFrame(AVFrame *pFrame, int width, int height, intiFrame) { FILE *pFile; char szFilename[32]; int y; // Open file sprintf(szFilename, "frame%d.ppm",iFrame); pFile=fopen(szFilename, "wb"); if(pFile==NULL) return; // Write header fprintf(pFile, "P6\n%d %d\n255\n", width,height); // Write pixel data for(y=0; y // Close file fclose(pFile); } |
// Free the RGB image av_free(buffer); av_free(pFrameRGB); // Free the YUV frame av_free(pFrame); // Close the codec avcodec_close(pCodecCtx); // Close the video file av_close_input_file(pFormatCtx); return 0; |
gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lz-lavutil -lm |
gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lz-lm |