FFmpeg开发环境的搭建


***【在线视频教程】***

好文章,来自【福优学苑@音视频+流媒体】

搭建环境Qt+FFmpeg

QT + FFMPEG 开发环境搭建

步骤简要

  • step1: 下载FFmpeg相应的动态库文件和程序执行所依赖的.dll文件。

  • step2:将动态库所对应的头文件和动态库文件加入工程。

  • step 3:将dll文件copy到.exe文件所对应的目录下。


 

下载FFMpeg对应的动态库和DLL文件

  • 1 下载地址

下载地址:(FFmpeg3.2.2)

链接:https://pan.baidu.com/s/1CLR7aBR3mjIt_2rgU-I8TQ 

提取码:aga5

注意:FFmpeg版本更新太快,请初学者注意用统一个版本,否则编译出错。


开发环境为win10 64bit +QT5.9.8 ,需下载window 32 bit对应的shared文件(包含dll文件)和Dev文件(包含动态库和include的文件以及examples)。

注意window-64bit的开发库时,提示 “xxx undefined  reference ”。.


 


  • step 2 :将动态库和头文件加入project

将下载的dev文件解压,文件名改为ffmpeglib,并copy到工程所在的目录


一)加入头文件

INCLUDEPATH += ../ffmpeglib/include


二) 加入动态库文件

LIBS += ../ffmpeglib/lib/avcodec.lib  \

        ../ffmpeglib/lib/avdevice.lib  \

        ../ffmpeglib/lib/avfilter.lib  \

        ../ffmpeglib/lib/avformat.lib  \

        ../ffmpeglib/lib/avutil.lib  \

        ../ffmpeglib/lib/swresample.lib  \

        ../ffmpeglib/lib/swscale.lib


具体如下截图:

image.png



三)开始写code

包含头文件,注意QT为C++开发环境,FFMpeg用的是C 开发的,在CPP导入C的特性,需加关键词“ extern  “C”“



extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
}



定义相关宏,否则就报错,如error: #error missing -D__STDC_CONSTANT_MACROS”


extern "C"{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#include <stdint.h>
#endif
}

添加如下code,若能成功输出ffmpeg version信息,则代表开发环境搭建成功。


int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<< avcodec_configuration();
    unsigned version = avcodec_version();
    QString ch = QString::number(version,10);
    qDebug()<< "version:"<<version;
    return a.exec();
}

将share文件下dll文件 copy to 该工程生成的exe文件所在的目录

否则会“程序异常退出”。

image.png





搭建环境MFC+FFmpeg

1.去FFMPEG网站上下载Dev版本的库,

里面有我们需要的头文件和lib文件,然后下载Shared版本的库,里面有我们需要的dll文件

http://ffmpeg.zeranoe.com/builds/

image.png

记得区分32位和64位的库,这里碰到一个大坑,就是我下载的是64位的库,但是创建工程的时候选的是32位的工程,导致链接的时候一直报无法解析的外部符号 _av_register_all......

 

2.把Dev库里解压出来的东西拷贝到工程中,Shared库中解压出来的东西拷贝到生成的bin文件目录(如release)

D:\source\FFmpegDemo\FFmpegDemo\ffmpeg>

├─inc
│  ├─libavcodec
│  ├─libavdevice
│  ├─libavfilter
│  ├─libavformat
│  ├─libavutil
│  ├─libpostproc
│  ├─libswresample
│  └─libswscale
└─libs
        avcodec.lib
        avdevice.lib
        avfilter.lib
        avformat.lib
        avutil.lib
        postproc.lib
        swresample.lib
        swscale.lib

3.右击工程“属性”,“C/C++”——>“附加包含目录”——>加入我们添加进来的头文件的路径

image.png



4.在源码中链接lib文件

#pragma comment(lib,"ffmpeg\\libs\\avutil.lib")
#pragma comment(lib,"ffmpeg\\libs\\avformat.lib")
#pragma comment(lib,"ffmpeg\\libs\\avcodec.lib")
#pragma comment(lib,"ffmpeg\\libs\\swscale.lib")
//main.cpp
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"ffmpeg\\libs\\avutil.lib")
#pragma comment(lib,"ffmpeg\\libs\\avformat.lib")
#pragma comment(lib,"ffmpeg\\libs\\avcodec.lib")
#pragma comment(lib,"ffmpeg\\libs\\swscale.lib")
extern "C" 
{
//编码
#include "libavcodec/avcodec.h"
//封装格式处理
#include "libavformat/avformat.h"
//像素处理
#include "libswscale/swscale.h"
};
int main(int argc, char* argv[])
{
    //获取输入输出文件名
    const char *input = "test.mp4";
    const char *output = "test.yuv";
    //1.注册所有组件
    av_register_all();
    //封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
    AVFormatContext *pFormatCtx = avformat_alloc_context();
    //2.打开输入视频文件
    if (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0)
    {
        printf("%s", "无法打开输入视频文件");
        return -1;
    }
    //3.获取视频文件信息
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    {
        printf("%s", "无法获取视频文件信息");
        return -1;
    }
    //获取视频流的索引位置
    //遍历所有类型的流(音频流、视频流、字幕流),找到视频流
    int v_stream_idx = -1;
    int i = 0;
    //number of streams
    for (; i < pFormatCtx->nb_streams; i++)
    {
        //流的类型
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            v_stream_idx = i;
            break;
        }
    }
    if (v_stream_idx == -1)
    {
        printf("%s", "找不到视频流\n");
        return -1;
    }
    //只有知道视频的编码方式,才能够根据编码方式去找到解码器
    //获取视频流中的编解码上下文
    AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec;
    //4.根据编解码上下文中的编码id查找对应的解码
    AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL)
    {
        printf("%s", "找不到解码器\n");
        return -1;
    }
    //5.打开解码器
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("%s", "解码器无法打开\n");
        return -1;
    }
    //输出视频信息
    printf("视频的文件格式:%s", pFormatCtx->iformat->name);
    printf("视频时长:%d", (pFormatCtx->duration) / 1000000);
    printf("视频的宽高:%d,%d", pCodecCtx->width, pCodecCtx->height);
    printf("解码器的名称:%s", pCodec->name);
    //准备读取
    //AVPacket用于存储一帧一帧的压缩数据(H264)
    //缓冲区,开辟空间
    AVPacket *packet = (AVPacket*)av_malloc(sizeof(AVPacket));
    //AVFrame用于存储解码后的像素数据(YUV)
    //内存分配
    AVFrame *pFrame = av_frame_alloc();
    //YUV420
    AVFrame *pFrameYUV = av_frame_alloc();
    //只有指定了AVFrame的像素格式、画面大小才能真正分配内存
    //缓冲区分配内存
    uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
    //初始化缓冲区
    avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
    //用于转码(缩放)的参数,转之前的宽高,转之后的宽高,格式等
    struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
        pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P,
        SWS_BICUBIC, NULL, NULL, NULL);
    int got_picture, ret;
    FILE *fp_yuv = fopen(output, "wb+");
    int frame_count = 0;
    //6.一帧一帧的读取压缩数据
    while (av_read_frame(pFormatCtx, packet) >= 0)
    {
        //只要视频压缩数据(根据流的索引位置判断)
        if (packet->stream_index == v_stream_idx)
        {
            //7.解码一帧视频压缩数据,得到视频像素数据
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
            if (ret < 0)
            {
                printf("%s", "解码错误");
                return -1;
            }
            //为0说明解码完成,非0正在解码
            if (got_picture)
            {
                //AVFrame转为像素格式YUV420,宽高
                //2 6输入、输出数据
                //3 7输入、输出画面一行的数据的大小 AVFrame 转换是一行一行转换的
                //4 输入数据第一列要转码的位置 从0开始
                //5 输入画面的高度
                sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                    pFrameYUV->data, pFrameYUV->linesize);
                //输出到YUV文件
                //AVFrame像素帧写入文件
                //data解码后的图像像素数据(音频采样数据)
                //Y 亮度 UV 色度(压缩了) 人对亮度更加敏感
                //U V 个数是Y的1/4
                int y_size = pCodecCtx->width * pCodecCtx->height;
                fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);
                fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);
                fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);
                frame_count++;
                printf("解码第%d帧\n", frame_count);
            }
        }
        //释放资源
        av_free_packet(packet);
    }
    fclose(fp_yuv);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_free_context(pFormatCtx);
    return 0;
}

好文章,来自【福优学苑@音视频+流媒体】
***【在线视频教程】***