ffmpeg官网教程(1)

来源:互联网 发布:windows屏幕旋转 编辑:程序博客网 时间:2024/05/22 12:07

http://dranger.com/ffmpeg/tutorial01.html

ffmpeg:3.1.2版

#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libswscale/swscale.h>#include <stdio.h>void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {  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<height; y++)    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);    // Close file  fclose(pFile);}int main(int argc, char *argv[]) {AVFormatContext *pFormatCtx = NULL;int i;AVCodecContext *pCodecCtxOrig = NULL;AVCodecContext *pCodecCtx = NULL;AVCodec *pCodec = NULL;AVFrame *pFrame = NULL;AVFrame *pFrameRGB = NULL;//registers all available file formats and codecs with the libraryav_register_all();// Open video fileif(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)  return -1; // Retrieve stream informationif(avformat_find_stream_info(pFormatCtx, NULL)<0)  return -1;// Dump information about file onto standard errorav_dump_format(pFormatCtx, 0, argv[1], 0);// Find the first video streamint videoStream=-1;for(i=0; i<pFormatCtx->nb_streams; i++)if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {videoStream=i;break;}if(videoStream==-1)  return -1;// Get a pointer to the codec context for the video streampCodecCtx=pFormatCtx->streams[videoStream]->codec;// Find the decoder for the video streampCodec=avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec==NULL) {fprintf(stderr, "Unsupported codec!\n");return -1; // Codec not found}// Copy context//pCodecCtx = avcodec_alloc_context3(pCodec);//if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {//fprintf(stderr, "Couldn't copy codec context");//return -1; // Error copying codec context//}// Open codecif(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  return -1;// Allocate video framepFrame=av_frame_alloc();// Allocate an AVFrame structurepFrameRGB=av_frame_alloc();if(pFrameRGB==NULL)  return -1;uint8_t *buffer = NULL;int numBytes;// Determine required buffer size and allocate buffernumBytes=avpicture_get_size(AV_PIX_FMT_RGB24,pCodecCtx->width,pCodecCtx->height);buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));// Assign appropriate parts of buffer to image planes in pFrameRGB// Note that pFrameRGB is an AVFrame, but AVFrame is a superset// of AVPictureavpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);struct SwsContext *sws_ctx = NULL;int frameFinished;AVPacket packet;// initialize SWS context for software scalingsws_ctx = sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height,AV_PIX_FMT_RGB24,SWS_BILINEAR,NULL,NULL,NULL);i=30;while(av_read_frame(pFormatCtx, &packet)>=0) {  // Is this a packet from the video stream?  if(packet.stream_index==videoStream) {// Decode video frameavcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);// Did we get a video frame?if(frameFinished) {// Convert the image from its native format to RGBsws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,  pFrame->linesize, 0, pCodecCtx->height,  pFrameRGB->data, pFrameRGB->linesize);// Save the frame to diskif(++i<=35){printf("frame finish==>start\n");  SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);printf("frame finish==>\nend");}}}  // Free the packet that was allocated by av_read_frame  av_free_packet(&packet);}// Free the RGB imageav_free(buffer);av_free(pFrameRGB);// Free the YUV frameav_free(pFrame);// Close the codecsavcodec_close(pCodecCtx);avcodec_close(pCodecCtxOrig);// Close the video fileavformat_close_input(&pFormatCtx);return 0;}

编译:

gcc -fno-stack-protector -o videoToPic -g videoToPic.c -lavutil -lavformat -lavcodec -lswscale -lz -lavutil -lm

0 0