<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    VS2008+ffmpeg SDK3.2調試tutorial01

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    最近研究ffmpeg,在ubuntu下感覺不太好調試,老是找不到函數的聲明。所以我就把他移到windows下用vs2008分析

    關于環境的搭建,我參考了 http://hi.baidu.com/forever803/blog/item/ba90cdd2cca917093af3cf9e.html ,這里我把步驟整理一下,順便奉上圖文

    第1步:

    下載ffmpeg SDK3.2:點擊下載,并解壓。

    第2步:

    打開vs2008新建一個空的vc++項目

    RFID設備管理軟件

    RFID設備管理軟件

    RFID設備管理軟件

    第3步:

    新建一個C++源文件,test.cpp,輸入簡單代碼測試一下

    [html] view plaincopy  
    1. #include<stdio.h>  
    2. #include<stdlib.h>  
    3. int main(){  
    4.     printf("aaaa\n");  
    5.     system("pause");  
    6.     return 0;  
    7. }  

    按F5運行,打印輸出aaaa,則沒問題

    RFID設備管理軟件

    第4步:

    將解壓出來的sdk下的include目錄下的所有文件夾和文件拷到vc++工程目錄下的test.cpp同一個目錄。我的是(C:\Users\easou\Documents\Visual Studio 2008\Projects\testffmpeg\testffmpeg),此時,目錄結構如下圖

    RFID設備管理軟件

    第5步:

     將解壓出來的lib文件夾拷貝至tes.cpp同一目錄下。

    然后在vs2008里,單擊工程右鍵->屬性->常規->附加庫目錄  填入$(SolutionDir)\$(ProjectName)\lib

    RFID設備管理軟件

        
    屬性->鏈接器->  附加依賴項  填入avcodec.lib avdevice.lib avfilter.lib avformat.lib avutil.lib swscale.lib  點擊確定

    RFID設備管理軟件

    第6步:

    將tutorial01.c的內容復制到test.cpp中,并修改相關引用路徑,按F7編譯。F5運行

    tes.cpp代碼:

    [html] view plaincopy  
    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3.   
    4. #ifdef __cplusplus  
    5. extern "C" {  
    6. #endif  
    7.     #include "libavcodec/avcodec.h"  
    8.     #include "libavformat/avformat.h"  
    9.     #include "libswscale/swscale.h"  
    10. #ifdef __cplusplus  
    11. }  
    12. #endif  
    13.   
    14. void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {  
    15.   FILE *pFile;  
    16.   char szFilename[32];  
    17.   int  y;  
    18.     
    19.   // Open file  
    20.   sprintf(szFilename, "frame%d.ppm", iFrame);  
    21.   pFile=fopen(szFilename, "wb");  
    22.   if(pFile==NULL)  
    23.     return;  
    24.     
    25.   // Write header  
    26.   fprintf(pFile, "P6\n%d %d\n255\n", width, height);  
    27.     
    28.   // Write pixel data  
    29.   for(y=0; y<height; y++)  
    30.     fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);  
    31.     
    32.   // Close file  
    33.   fclose(pFile);  
    34. }  
    35.   
    36. int main() {  
    37.   AVFormatContext *pFormatCtx;  
    38.   int             i, videoStream;  
    39.   AVCodecContext  *pCodecCtx;  
    40.   AVCodec         *pCodec;  
    41.   AVFrame         *pFrame;   
    42.   AVFrame         *pFrameRGB;  
    43.   AVPacket        packet;  
    44.   int             frameFinished;  
    45.   int             numBytes;  
    46.   uint8_t         *buffer;  
    47.   static struct SwsContext *img_convert_ctx;  
    48.   char * filePath="test.mp4";  
    49.   // Register all formats and codecs  
    50.   av_register_all();  
    51.     // Open video file  
    52.   if(av_open_input_file(&pFormatCtx, filePath, NULL, 0, NULL)!=0)  
    53.     return -1; // Couldn't open file  
    54.     
    55.   // Retrieve stream information  
    56.   if(av_find_stream_info(pFormatCtx)<0)  
    57.     return -1; // Couldn't find stream information  
    58.     
    59.   // Dump information about file onto standard error  
    60.   dump_format(pFormatCtx, 0, filePath, 0);  
    61.     // Find the first video stream  
    62.   videoStream=-1;  
    63.   for(i=0; i<pFormatCtx->nb_streams; i++)  
    64.     if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {  
    65.       videoStream=i;  
    66.       break;  
    67.     }  
    68.   if(videoStream==-1)  
    69.     return -1; // Didn't find a video stream  
    70.       // Get a pointer to the codec context for the video stream  
    71.   pCodecCtx=pFormatCtx->streams[videoStream]->codec;  
    72.   
    73.     // Find the decoder for the video stream  
    74.   pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
    75.   if(pCodec==NULL) {  
    76.     fprintf(stderr, "Unsupported codec!\n");  
    77.     return -1; // Codec not found  
    78.   }  
    79.       // Open codec  
    80.   if(avcodec_open(pCodecCtx, pCodec)<0)  
    81.     return -1; // Could not open codec  
    82.   
    83.     // Allocate video frame  
    84.   pFrame=avcodec_alloc_frame();  
    85.     
    86.   // Allocate an AVFrame structure  
    87.   pFrameRGB=avcodec_alloc_frame();  
    88.   if(pFrameRGB==NULL)  
    89.     return -1;  
    90.   
    91.       
    92.   // Determine required buffer size and allocate buffer  
    93.   numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,  
    94.                   pCodecCtx->height);  
    95.   buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));  
    96.   
    97.     // Assign appropriate parts of buffer to image planes in pFrameRGB  
    98.   // Note that pFrameRGB is an AVFrame, but AVFrame is a superset  
    99.   // of AVPicture  
    100.   avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,  
    101.          pCodecCtx->width, pCodecCtx->height);  
    102.   
    103.   // Read frames and save first five frames to disk  
    104.   i=0;  
    105.   while(av_read_frame(pFormatCtx, &packet)>=0) {  
    106.             if(packet.stream_index==videoStream) {  
    107.                       // Decode video frame  
    108.                  avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);  
    109.                    if(frameFinished) {  
    110.                             // Convert the image from its native format to RGB  
    111.                         img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);  
    112.                         // Convert the image from its native format to RGB  
    113. sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);  
    114.                         if(++i<=5)  
    115.                             SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,i);                  
    116.                    }  
    117.             }  
    118.                 // Free the packet that was allocated by av_read_frame  
    119.         av_free_packet(&packet);  
    120.   }  
    121.   // Free the RGB image  
    122.   av_free(buffer);  
    123.   av_free(pFrameRGB);  
    124.     
    125.   // Free the YUV frame  
    126.   av_free(pFrame);  
    127.     
    128.   // Close the codec  
    129.   avcodec_close(pCodecCtx);  
    130.     
    131.   // Close the video file  
    132.   av_close_input_file(pFormatCtx);  
    133.     
    134.   printf("執行完畢\n");  
    135.   system("pause");  
    136.   return 0;  
    137. }  



     

    這里可能出現的問題比較多,主要有:

    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出現命令窗口如下,調試通過

    RFID設備管理軟件

    到test.cpp文件的目錄下看一下,多出了5個ppm文件

    RFID設備管理軟件

    可以用acd查看

    RFID設備管理軟件

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全