zoukankan      html  css  js  c++  java
  • 使用 VS2015 编译并调试 ffmpeg

    导读

      ffmpeg 是音频处理方面非常强大非常有名的开源项目了,然而如 雷神 所说,“FFMPEG 难度比较大,却没有一个循序渐进,由简单到复杂的教程。现在网上的有关FFMPEG的教程多半难度比较大,不太适合刚接触 FFMPEG 的人学习;而且很多的例子程序编译通不过,极大地打消了学习的积极性,对于平时只习惯在 Windows 下开发的初学者来说,从零开始了解相关依赖,搭建起项目并调试 ffmpeg 并不是件容易的事,好在另一个非官方的 开源项目,提供了一整套 Windows 下,用 VS 来调试 ffmpeg 的解决方案——Shift Media Project

      本文使用最新版本的 ShiftMediaProject 的代码(20191015),展示在Windows10 下使用 VS2015 下载代码并成功编译的过程。在另一篇本文参考的 博文 中亦介绍了整个过程,不过由于是 20180307 写的,有些新的内容没有覆盖到,这里可当做是对其进行补充和拓展。同时也作为个人笔记分享出来,希望能帮助到更多刚好有需求的人。


    目录
    1. 工具准备
    2. 下载源代码
    3. 按项目指引下载相关依赖
    4. 使用VS编译ffmpeg
    5. 编译与运行
    1、工具准备

    本文测试使用环境:

    操作系统:Windows 10

    编译使用开发环境:Visual Studio 2015

    下载源代码工具:git 客户端

     (ffmpeg 的编译要求编译器对 C99 标准的支持。VS 中默认只有 VS2013 以及更新的版本对 C99 标准提供了支持,所以只能使用 VS2013 之后的版本,如果要使用更前版本,那么就得自己在 VS 先添加 C99 标准的支持)

    2、下载源代码

    项目地址:https://github.com/ShiftMediaProject/FFmpeg

    需要注意的是,克隆到本地的目标项目目录 至少需要有2层,因为期望的项目的结构是这样子的:

    - msvc (OutputDir)                (该项目默认的 VS 编译输出的目录)
    - source                      (这个是需要的上一级目录,待会下载的依赖项目有很多,十几二十来个依赖项目都会下载到这里)
      - FFmpeg                    (这个是 clone 到本地的项目目录)
      - ..Any other libraries source code..    (其他的十几二十个依赖的项目)

    准备好文件夹后,比如这里上面的 "source" 文件夹,在这个文件夹下,克隆代码下来

    git clone https://github.com/ShiftMediaProject/FFmpeg.git
    3、按项目指引下载相关依赖

    所有的 VS 项目相关的文件,都在 SMP 文件夹下,仔细查看该目录下的 readme 文件内容,按里面的说明进行。

    This is a small list of steps in order to build FFmpeg into a msvc DLL and lib file.
    
    The projects contain Release and Debug builds for static lib files (Debug/Release)
      as well as dynamic shared dll files (DebugDLL/ReleaseDLL).
    Choose whichever project configuration meets your requirements.
    
    Note: FFmpeg requires C99 support in order to compile. Only Visual Studio 2013 or newer supports required C99 functionality and so any 
    older version is not supported. Visual Studio 2013 or newer is required. If using an older unsupported version of Visual Studio the
    Intel compiler can be used to add in the required C99 capability.
    
    
    *** Using the Default Supplied Projects ***
    
    The supplied project files are created using default configuration options as used by the ShiftMediaProject.
    These projects use Visual Studio 2013/2015 and require certain additional dependencies to be built and available at compile time.
    Required project dependencies include:
        bzlib
        iconv
        zlib
        lzma
        libxml2
        sdl2
        libmp3lame
        libvorbis
        libspeex
        libopus
        libilbc
        libtheora
        libx264
        libx265
        libxvid
        libvpx
        libgme
        libmodplug
        libsoxr
        libfreetype
        fontconfig
        libfribidi
        libass
        gnutls
        libgcrypt
        libssh
        libcdio
        libcdio_paranoia
        libbluray
        opengl
        ffnvcodec
        libmfx
    
    Most of the above dependencies are supplied as part of the ShiftMediaProject repositories.
    These repositories can be manually downloaded or automatically cloned using the supplied
      project_get_dependencies.bat file. This file can also be used to check for and download
      any dependency updates at any point after the first clone of the library.
      
    Some of these dependency projects have additional requirements to those listed here. See the corresponding readme for each of the projects for further details.
    
    Many of the possible FFmpeg dependencies (and there dependencies) are available in the ShiftMediaProject repositories.
    However the following is a list of extra dependency options that require external downloads:
        1) opengl (requires glext)
            a) Download glext.h and wglext.h from opengl.org.
            b) Save the header files into "OutputDir/include/gl/*".
            c) Download khrplatform.h from khronos.org
            d) Save the header file into "OutputDir/include/KHR/*".
        2) ffnvcodec (requires nv-codec-headers)
            a) Download the nv-codec-headers repository from https://github.com/FFmpeg/nv-codec-headers
            b) Save the contents of the nv-codec-headers repositories "include" folder into "OutputDir/include/*".
        3) AMF (requires Advanced Media Framework (AMF) SDK headers)
            a) Download the AMF repository from https://github.com/GPUOpen-LibrariesAndSDKs/AMF
            b) Save the contents of the AMF repositories "amf/public/include" into "OutputDir/include/AMF/*".
                
    *OutputDir is the "Output Directory" specified in the project properties. 
    The default value of OutputDir is "....msvc" relative to the FFmpeg source directory. An example of the expected 
    directory structure is:
        -  msvc          (OutputDir)
        -> source
            - FFmpeg
            - ..Any other libraries source code..
        
    Any dependencies supplied by ShiftMediaProject should be downloaded next to the FFmpeg folder as they will use the same OutputDir
    location. Projects to build each dependency can be found in the respective repository ./SMP directories or all together using
    the all inclusive ffmpeg_deps.sln.
    
    Only dependencies built from supplied ShiftMediaProject repositories are tested and supported. Using compiled dependencies from
    other sources may result in version mismatch or other issues. Although these external sources generally work fine any problems associated
    with them are not covered by ShiftMediaProject and so they should be used with discretion.
        
    
    *** Building with ASM ***
    
    In order to build FFmpeg using msvc you must first download and install NASM.
    NASM is required to compile all assembly files.
    
    1) Visual Studio NASM integration can be downloaded from https://github.com/ShiftMediaProject/VSNASM/releases/latest
    
    2) Once downloaded simply follow the install instructions included in the download.
    readme

    按照 readme 说明,接下来:

    下载项目中默认提供的其他依赖的 git 项目

    ShiftMediaProject 使用默认的配置生成了一些项目文件,这些文件可以使用 VS 打开,同时需要依赖特定的额外的项目,才能进一步编译。

    依赖的项目包括:

        bzlib    iconv    zlib    lzma    libxml2    sdl2    libmp3lame    libvorbis    libspeex    libopus
    libilbc libtheora libx264 libx265 libxvid libvpx libgme libmodplug libsoxr libfreetype fontconfig libfribidi libass gnutls libgcrypt libssh libcdio libcdio_paranoia libbluray opengl ffnvcodec libmfx

    上面大部分的依赖项目都被放在了 ShiftMediaProject 用户的 git 仓库 了,这些都可以手动下载,当然强烈建议还是使用 FFmpegSMPproject_get_dependencies.bat 批处理自动clone下载,这个脚本不仅仅可在第一次用于clone项目(及其本身依赖的其他git项目),还可以在后面任何时候执行,用来自动更新各自最新的版本

    所以,双击执行 “project_get_dependencies.bat” 批处理,等待一段 较长的时间 的下载,即可完成大部分依赖库的下载

    额外下载其他依赖的文件

    大部分需要的依赖(及其自身的依赖)都可以在 ShiftMediaProject  下的仓库找到,不过编译还需要下载其他外部文件。

    下面列出了需要额外下载的文件,以及对应需要下载到的目标目录

        1) opengl (requires glext)
            a) Download glext.h and wglext.h from opengl.org.        
            b) Save the header files into "OutputDir/include/gl/*".
            c) Download khrplatform.h from khronos.org
            d) Save the header file into "OutputDir/include/KHR/*".
        2) ffnvcodec (requires nv-codec-headers)
            a) Download the nv-codec-headers repository from https://github.com/FFmpeg/nv-codec-headers
            b) Save the contents of the nv-codec-headers repositories "include" folder into "OutputDir/include/*".
        3) AMF (requires Advanced Media Framework (AMF) SDK headers)
            a) Download the AMF repository from https://github.com/GPUOpen-LibrariesAndSDKs/AMF
            b) Save the contents of the AMF repositories "amf/public/include" into "OutputDir/include/AMF/*".

    1、下载 opengl 的 glext.h 和 wglext.h  到  " OutputDir/include/gl/* "              (下载点 这里

    2、下载 opengl 的 khrplatform.h 到 " OutputDir/include/KHR/* "             (下载点 这里

    3、下载  nv-codec-headers 项目的 "include" 文件夹下的内容到  " OutputDir/include/* "   (git 项目点 这里

    4、下载 AMF 项目的  "amf/public/include" 文件夹下的内容到  " OutputDir/include/AMF/* "       (git 项目点 这里)

    OutputDir 是在项目属性里指定了的 “输出目录 ”
    默认的项目输出目录是相对于 FFmpeg 源代码目录的 “....msvc
    一个预期的目录结构如下,(这个也是前面 2、下载源代码 步骤中提到的文件结构)
    - msvc (OutputDir)                (该项目默认的 VS 编译输出的目录)
    - source                      (这个是需要的上一级目录,待会下载的依赖项目有很多,十几二十来个依赖项目都会下载到这里)
      - FFmpeg                    (这个是 clone 到本地的项目目录)
      - ..Any other libraries source code..    (其他的十几二十个依赖的项目)

    所以,如上创建 msvc 目录,按步骤创建文件夹并下载对应文件(想偷懒的可以直接点击我下载后打包的  msvc.zip

    4、使用 VS 编译 ffmpeg

    下载完了项目依赖的其他 git 项目 和 头文件之后,就可以使用 VS 打开 FFmpegSMPffmpeg_deps.sln  项目文件了

    初次打开项目,如果缺少 Win10 SDK 相关组件,会有类似下图提示

     点击 “安装” ,关闭VS后,等待其安装完毕。

     

     实际上,如上 “安装的缺少的功能” 后,重新用 VS 打开项目,还是会提示一些项目 (不可用) 或 (加载失败),还是无法 “重新加载项目

    因为使用 msvc 来编译 ffmpeg 还需要 安装 NASM 才能编译所有的汇编文件。ShiftMediaProject  为编译 ffmpeg 提供的自定义构建项【nasm / yasm】默认 VS是没有支持的,所以最后一步就是为 VS  添加自定构建项

    为 VS添加自定构建项 NASM

    ShiftMediaProject 提供了自动下载和安装 NASM 的安装脚本,下载地址为: https://github.com/ShiftMediaProject/VSNASM/releases/latest

    下载完后,注意最好 不要 直接运行  install_script.bat

    最好是先 以管理员身份,在预设好 VS 相关变量的脚本环境中 (开发者命令行 / dev command line),运行该 install_script.bat 脚本

     若执行成功,没有任何失败提示,便算是安装完成了

    如果直接运行,由于执行脚本环境没有先预设 VCINSTALLDIR 等变量,可能会出现如下错误(一开始我直接运行遇到的,后来经 BesLyric 项目 的一个合作开发的伙伴提醒才意识到先运行VS开发者命令行)。

    出错与解决 (一般在开发者命令行执行则不会有以下问题,这里保留记录以供参考)

    这一步出现的2个错误是:

    (1)无法下载 vswhere.exe 到当前目录

    (2)  在构建 .......\VCinamd64vcvars64.bat 文件的路径时, 构建出一个不存在的路径,导致脚本尝试执行 vcvars64.bat 时出错

     

    第一个错误还好解决,直接手动复制链接到浏览器下载  https://github.com/Microsoft/vswhere/releases/download/2.5.9/vswhere.exe ,然后放在  install_script.bat 同一目录即可

    第二个错误就需要手动改  install_script.bat  文件了

     经过调试发现,在如下 批处理中:

    REM Call the required vcvars file in order to setup up build locations
    if "%MSVC_VER%"=="16" (
        set VCVARS=%VSINSTANCEDIR%VCAuxiliaryBuildvcvars%SYSARCH%.bat
    ) else if "%MSVC_VER%"=="15" (
        set VCVARS=%VSINSTANCEDIR%VCAuxiliaryBuildvcvars%SYSARCH%.bat
    ) else if "%MSVC_VER%"=="14" (
        set VCVARS=%VSINSTANCEDIR%VCin%MSVCVARSDIR%vcvars%SYSARCH%.bat
    ) else if "%MSVC_VER%"=="12" (
        set VCVARS=%VSINSTANCEDIR%VCin%MSVCVARSDIR%vcvars%SYSARCH%.bat
    ) else (
        echo Error: Invalid MSVC version!
        goto Terminate
    )

    构建的变量 %VCVARS% 没有达到预期值,通过打印相关变量(使用 echo 指令),打印出该变量的结果是:

    而经过查找,我VS环境中的 vcvars64.bat 实际路径为: "C:Program Files (x86)Microsoft Visual Studio 14.0VCinamd64vcvars64.bat", 可能是之前安装VS没有按默认路径安装导致(?)

    于是针对我个人的环境情况,根据自己的 vcvars64.bat 脚本的位置,根据我只需要成功为 VS2015 安装这个自定义构建项的需要,我做了如下修改:

    //由于VS2015会经过 "%MSVC_VER%"=="14" 的分支,所以我将原先构建的逻辑注释掉(使用 REM),然后直接指定我VS2015 对应的 vcvars64.bat 文件的路径

    REM Call the required vcvars file in order to setup up build locations if "%MSVC_VER%"=="16" ( set VCVARS=%VSINSTANCEDIR%VCAuxiliaryBuildvcvars%SYSARCH%.bat ) else if "%MSVC_VER%"=="15" ( set VCVARS=%VSINSTANCEDIR%VCAuxiliaryBuildvcvars%SYSARCH%.bat ) else if "%MSVC_VER%"=="14" (
    echo [%VSINSTANCEDIR%VCin%MSVCVARSDIR%vcvars%SYSARCH%.bat] REM set VCVARS=%VSINSTANCEDIR%VCin%MSVCVARSDIR%vcvars%SYSARCH%.bat set VCVARS="C:Program Files (x86)Microsoft Visual Studio 14.0VCinamd64vcvars64.bat" ) else if "%MSVC_VER%"=="12" ( set VCVARS=%VSINSTANCEDIR%VCin%MSVCVARSDIR%vcvars%SYSARCH%.bat ) else ( echo Error: Invalid MSVC version! goto Terminate )

     重新执行,脚本的执行便没有其他错误了。

    为 VS添加自定构建项 YASM

    重新使用 VS 打开  ffmpeg_deps.sln ,还是提示报错(部分项目未能正确加载),还是没能 “ 重新加载项目

    经过一番探索,发现还需要 再安装 另一个 自定义构建项 YASM,这一点是在 ShiftMediaProject 下其他 ffmpeg 的某些依赖项目下说明的,如 https://github.com/ShiftMediaProject/libvpx/tree/master/SMP 项目下的 readme 中:

    *** Building with YASM ***
    
    In order to build libvpx using msvc you must first download and install YASM.
    YASM is required to compile all assembly files.
    
    1) Visual Studio YASM integration can be downloaded from https://github.com/ShiftMediaProject/VSYASM/releases/latest
    
    2) Once downloaded simply follow the install instructions included in the download.

    关于为什么不将 自定义构建项 YASM 的下载说明放到 FFmpeg/SMP/readme.txt 下,项目维护者表示 ffmpeg 本身编译直接依赖的项目只需要 NASM, 只是某些依赖本身编译需要 YASM,想分开来在各自的依赖项目中说明

    项目作者具体回复可查看:https://github.com/ShiftMediaProject/FFmpeg/pull/51

    ShiftMediaProject 提供了自动下载和安装 yasm 的安装脚本,下载地址为: https://github.com/ShiftMediaProject/VSYASM/releases

    安装方法 以及 可能遇到的问题 同上一节 为 VS添加自定构建项 NASM 完全一致

    其他可能问题
    1、找不到文件 '..gnuliblib*'

     这种情况为依赖项目的子项目没有自动下载导致,这里截图对应的项目为 gnutls 的 git子模块 gnulib 没有自动下载导致。

     解决方案:手动下载 gnulib 到 gnutls/gnulib 文件夹下,这里可以使用 git submodule 指令同步下载如下,

     如图,如果没有成功下载,可以尝试手动下载,下载地址为 :

    https://gitlab.com/libidn/gnulib-mirror.git

    或者

    git://git.sv.gnu.org/gnulib.git

    可使用 git clone 下载代码,然后放置到 gnutls/gnulib 文件夹下即可

    2、error LNK2038: 检测到 “RuntimeLibrary” 的不匹配项: 值 “MDd_DynamicDebug” 不匹配值 “MTd_StaticDebug”

    可能原因:

    解决方案下不同项目设置不一样  (该问题由评论区网友 天空自由 遇到,其使用的的 VS 版本为 VS2019)

    参考解决方案:

    项目属性 -> 配置属性 -> C/C++ -> 代码生成 -> 运行库
    都设置一样就行了: 多线程调试(/MTd)

    5、编译与运行

     到此,编译需要的所有事都完成了,重新打开VS,显示(不可用)的项目是还没重新加载, “ 重新加载项目 ”项目即可

     接下来,指定一个启动程序来调试,这里设置 ffmpeg.exe 为启动项,“设为启动项目” 后,开始构建编译“生成” 

     

    编译过程中,若提示如下,是正常的(可能是因为静态编译模式下 [使用的 lib文件] 在编译生成lib文件后,VS没有及时引用到临时导致的?)

    错误    LNK1181    无法打开输入文件“libgmpd.lib”      libnettle      E:openSourceGitffmpeg-vs
    ettleSMPLINK    1
    错误    LNK1181    无法打开输入文件“libnettled.lib”    libhogweed      E:openSourceGitffmpeg-vs
    ettleSMPLINK    1

     静静等待其编译结束后,再点构建 “生成” 或是 “运行” 就没报编译上的错误了。

    最后的最后,编译完运行 “本地 Windows 调试器”,大概会弹出错误如下:

     这是因为“调试”->“命令”使用的文件路径,和 实际在“链接器”->“常规”中设置的“输出文件”路径不一致导致,将前者设置和后者一致即可。

    好了,你可以用 vs 调试 ffmpeg 了 !

     

  • 相关阅读:
    今天发现之前瑞乐做的登录和注册居然都是用的get请求,瞬间出了一身冷汗.
    用grunt进行前端工程化之路
    移动端开发库zepto 之我思
    构造高度自适应的textarea
    maxlength属性在textarea里奇怪的表现
    在windows下使用linux的开发环境
    移动web开发的一些坑
    [译]开始学习webpack
    完美解决移动Web小于12px文字居中的问题
    再谈移动端Web屏幕适配
  • 原文地址:https://www.cnblogs.com/BensonLaur/p/10989115.html
Copyright © 2011-2022 走看看