[Switch] FFmpeg used in vs2008

Eventually adopted https://blog.csdn.net/smilestone_322/article/details/7605101 program

Transfer: http://blog.sina.com.cn/s/blog_62949ff40101egbw.html

1 to http://ffmpeg.zeranoe.com/builds/
 download ffmpeg-20131120-git-e502783- win32-dev , and ffmpeg-20131120-git-e502783- win32-shared

 

2. Open vs2008 established console program, and will include the lib ffmpeg-20131120-git-e502783-win32-dev copied to the program directory, and the directory is provided dependent header files, and libraries

 

3. compilation process:
 Error 1: Can not open include file: "inttypes.h": No such file or directory
 Solution: delete it and add the following code before:
 ! #If defined (WIN32) && defined (__ MINGW32__) &&! defined (__ CYGWIN__)
 # the DEFINE CONFIG_WIN32
 #endif
 #if defined (WIN32) &&! defined (__ MINGW32__) &&! defined (__ CYGWIN__) &&! defined (EMULATE_INTTYPES)
 # the DEFINE EMULATE_INTTYPES
 #endif
 #ifndef EMULATE_INTTYPES
 # the include
 #else
      typedef char Signed int8_t;
      typedef Signed Short int16_t;
      typedef int Signed int32_t;
      typedef unsigned char uint8_t;
      typedef unsigned Short uint16_t;
      typedef unsigned int uint32_t;
 Ifdef CONFIG_WIN32 #
          typedef Signed __int64 int64_t;
          typedef unsigned __int64 uint64_t;
 # the else
          typedef Signed Long Long int64_t;
          typedef unsigned Long Long uint64_t;
 # endif
 #endif
 
 Error two: error C3861: "UINT64_C": identifier not found
 Workaround: common.h add the following code:
 
 #ifndef INT64_C
 #define INT64_C (C) (C ## LL)
 #define UINT64_C (C) (C ## ULL)
 #endif

 

 错误三:
 注释掉
 //#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)
 //#error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS

 //#endif

 

4. Sample Code: from a video file, the five pictures removed

#include
#include

#ifdef __cplusplus
extern "C" {
#endif
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libswscale/swscale.h"
#ifdef __cplusplus
}
#endif

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
   fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
 
  // Close file
  fclose(pFile);
}

int main() {
  AVFormatContext *pFormatCtx;
 int            i, videoStream;
  AVCodecContext *pCodecCtx;
 AVCodec        *pCodec;
 AVFrame        *pFrame;
 AVFrame        *pFrameRGB;
 AVPacket       packet;
 int            frameFinished;
 int            numBytes;
 uint8_t        *buffer;
  static struct SwsContext *img_convert_ctx;
  char * filePath="test.mp4";
  // Register all formats and codecs
  av_register_all();
  // Open video file
  if(avformat_open_input(&pFormatCtx, filePath, NULL, NULL)!=0)   

     return -1; // Couldn't open file
 
  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
    return -1; // Couldn't find stream information
 
  // Dump information about file onto standard error
  av_dump_format(pFormatCtx, 0, filePath, 0);
    // Find the first video stream
  videoStream=-1;
  for(i=0; inb_streams; i++)
   if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_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;

    // 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_open2(pCodecCtx, pCodec)<0)
    return -1; // Could not open codec

    // 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));

    // 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) {
     if(packet.stream_index==videoStream) {
         // Decode video frame
    avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
      if(frameFinished) {
       // Convert the image from its native format to RGB
      img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
      // Convert the image from its native format to RGB
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
      if(++i<=5)
       SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,i);     
      }
   }
      // Free the packet that was allocated by av_read_frame
  av_free_packet(&packet);
  }
  // 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);
 
  printf("执行完毕\n");
  system("pause");
  return 0;
}

5. All DLL \ in ffmpeg-20131120-git-e502783-win32-shared bin and executable programs are copied into the same directory, run the executable program OK

Published 65 original articles · won praise 34 · views 260 000 +

Guess you like

Origin blog.csdn.net/huanggang982/article/details/103665996