ffmpeg库 在vs2012中的调用

本文详细介绍了如何在VS2012中集成并使用ffmpeg进行音视频处理,包括下载文件、配置步骤、示例代码解析以及解决常见问题的方法。旨在帮助开发者快速上手ffmpeg,实现音视频格式转换、编解码等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


分类: ffmpeg vs2012   868人阅读  评论(2)  收藏  举报

一、下载文件

1、下载地址:http://ffmpeg.zeranoe.com/builds/

2、所需文件:对应自身操作系统的(32bit or 64bit)Builds(Dev)和Builds(Shared)。

      Builds(Dev):包含了所需要的.h头文件和.lib库文件

              Builds(Shared):包含了所需要的dll文件。

3、另外下载http://files.cnblogs.com/zyl910/c99int_v101.rar,下载完成后解压缩,在文件中找到auto_stdint.h文件放至vs2012的包含目录,

例如我的就放在:C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include。

       这是为了解决C99的兼容问题。

二、vs2012配置

这里需要两个文件路径:

1,)包含文件路径,例如我的是将Build(Dev)和Build(Shared)文件夹放到了D:\FFMPEG中,

我的包含文件路径为:D:\FFMPEG\ffmpeg-dev\include

        2)库文件路径为:D:\FFMPEG\ffmpeg-dev\lib

配置流程:

1、首先在vs中新建工程,然后配置工程的库文件和连接器。

2、设置ffmpeg头文件位置,左侧 属性管理器-->双击工程名-->配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加包含文件路径

D:\FFMPEG\ffmpeg-dev\include


3、设置ffmpeg的lib文件位置 ,鼠标右键点击工程名,选择属性,然后选择 配置属性 -> 链接器 -> 常规 -> 附加库目录,添加库文件路径

D:\FFMPEG\ffmpeg-dev\lib

4设置ffmpeg的所引用的lib文件 鼠标右键点击工程名,选择属性,   然后选择 配置属性 -> 链接器 -> 输入 -> 附加依赖项,添加的文件为你下载的 Builds (Dev)中的lib 文件。avcodec.lib;avformat.lib;avutil.lib;swscale.lib;swresample.lib;avfilter.lib;swscale.lib (如果需要其他库文件再对应添加)


5、配置完成。

三、简单示例

1、示例代码

// ffmpeg-example.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
 
#define inline _inline
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
   
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
 
#include <stdio.h>
static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame);
 
int main (int argc, const char * argv[])
{
char* filename = "E:\\A.mp4";
    AVFormatContext *pFormatCtx = NULL;
    int             i, videoStream;
    AVCodecContext  *pCodecCtx;
    AVCodec         *pCodec;
    AVFrame         *pFrame; 
    AVFrame         *pFrameRGB;
    AVPacket        packet;
    int             frameFinished;
    int             numBytes;
    uint8_t         *buffer;
 
    // Register all formats and codecs
    av_register_all();
 
    // Open video file
    //if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
if(avformat_open_input(&pFormatCtx, filename, 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, filename, false);
 
    // Find the first video stream
    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; // 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)
            return -1; // Codec not found
 
        // Open codec
        if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
            return -1; // Could not open codec
 
        // Hack to correct wrong frame rates that seem to be generated by some codecs
        if(pCodecCtx->time_base.num>1000 && pCodecCtx->time_base.den==1)
            pCodecCtx->time_base.den=1000;
 
        // 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=malloc(numBytes);
        buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
 
        // Assign appropriate parts of buffer to image planes in pFrameRGB
        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)
        {
            // Is this a packet from the video stream?
            if(packet.stream_index==videoStream)
            {
                // Decode video frame
                avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
 
                // Did we get a video frame?
                if(frameFinished)
                {
                    static struct SwsContext *img_convert_ctx;
 
#if 0
                    // Older removed code
                    // Convert the image from its native format to RGB swscale
                    img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
                        (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
                        pCodecCtx->height);
                    // function template, for reference
                    int sws_scale(struct SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
                        int srcSliceH, uint8_t* dst[], int dstStride[]);
#endif
                    // Convert the image into YUV format that SDL uses
                    if(img_convert_ctx == NULL) {
                        int w = pCodecCtx->width;
                        int h = pCodecCtx->height;
 
                        img_convert_ctx = sws_getContext(w, h, 
                            pCodecCtx->pix_fmt, 
                            w, h, PIX_FMT_RGB24, SWS_BICUBIC,
                            NULL, NULL, NULL);
                        if(img_convert_ctx == NULL) {
                            fprintf(stderr, "Cannot initialize the conversion context!\n");
                            exit(1);
                        }
                    }
                    int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, 
                        pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
#if 0 
                    // this use to be true, as of 1/2009, but apparently it is no longer true in 3/2009
                    if(ret) {
                        fprintf(stderr, "SWS_Scale failed [%d]!\n", ret);
                        exit(-1);
                    }
#endif
                    // Save the frame to disk
                    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
        //free(buffer);
        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;
}
 
static 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);
}

2、出现的问题:

(1) 虽然编译通过,但是并不表示就可以运行,当你运行代码时会出现以下错误


原因是,你虽然引用了LIB文件,但这并不是真正的静态库文件,而是对DLL的引用,所以当你调用ffmpeg库函数时,需要DLL文件在场。你可以用dumpbin(VS自带工具)来查看你生成的exe中引用了哪些DLL文件。你在命令行输入:
>dumpbin D:\test\test.exe /imports
这里需要拷贝的dll文件包括:swscale-2.dll; avcodec-55.dll; avformat-55.dll; avutil-52.dll。
我的拷贝源路径为:D:\FFMPEG\ffmpeg-shared\bin; 目标路径为:D:\Documents\Visual Studio 2012\Projects\ffmpeg_test\ffmpeg_test
(2)代码的第27行,filename需要改为你的视频文件对应的路径。
(3)运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值