VS2008+ffmpeg SDK3.2調試tutorial01
最近研究ffmpeg,在ubuntu下感覺不太好調試,老是找不到函數的聲明。所以我就把他移到windows下用vs2008分析
關于環境的搭建,我參考了 http://hi.baidu.com/forever803/blog/item/ba90cdd2cca917093af3cf9e.html ,這里我把步驟整理一下,順便奉上圖文
第1步:
下載ffmpeg SDK3.2:點擊下載,并解壓。
第2步:
打開vs2008新建一個空的vc++項目
第3步:
新建一個C++源文件,test.cpp,輸入簡單代碼測試一下
[html] view plaincopy- #include<stdio.h>
- #include<stdlib.h>
- int main(){
- printf("aaaa\n");
- system("pause");
- return 0;
- }
按F5運行,打印輸出aaaa,則沒問題
第4步:
將解壓出來的sdk下的include目錄下的所有文件夾和文件拷到vc++工程目錄下的test.cpp同一個目錄。我的是(C:\Users\easou\Documents\Visual Studio 2008\Projects\testffmpeg\testffmpeg),此時,目錄結構如下圖
第5步:
將解壓出來的lib文件夾拷貝至tes.cpp同一目錄下。
然后在vs2008里,單擊工程右鍵->屬性->常規->附加庫目錄 填入$(SolutionDir)\$(ProjectName)\lib
屬性->鏈接器-> 附加依賴項 填入avcodec.lib avdevice.lib avfilter.lib avformat.lib avutil.lib swscale.lib 點擊確定
第6步:
將tutorial01.c的內容復制到test.cpp中,并修改相關引用路徑,按F7編譯。F5運行
tes.cpp代碼:
[html] view plaincopy- #include <stdio.h>
- #include <stdlib.h>
- #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<height; 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(av_open_input_file(&pFormatCtx, filePath, 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 stream information
- // Dump information about file onto standard error
- dump_format(pFormatCtx, 0, filePath, 0);
- // Find the first video stream
- videoStream=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- if(pFormatCtx->streams[i]->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;
- // 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
- // 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_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);
- 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;
- }
這里可能出現的問題比較多,主要有:
1、找不到stdint.h這個文件,將出現問題的頭文件中的“include <stdint.h>”改為“include "stdint.h"”即可
2、無法解析的外部符號 _img_convert,參考文章http://witmax.cn/ffmpeg-img-convert.html
3、運行時會出現找不到avformat.dll的對話框,將sdk下的bin文件下的dll文件都拷貝到工程目錄下的debug文件夾解決。
4、信息窗出現 testffmpeg.exe: 本機”已退出,返回值為 -1字樣。檢查一下,是否沒有將你的test.mp4拷到tes.cpp同一個目錄下,mp4文件網上隨便找一個就可以。提供我的視頻一個http://115.com/file/e7f1ylpy
最后按F5出現命令窗口如下,調試通過
到test.cpp文件的目錄下看一下,多出了5個ppm文件
可以用acd查看