导读

  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 仓库 了,这些都可以手动下载,当然强烈建议还是使用 FFmpeg\SMP\project_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 打开 FFmpeg\SMP\ffmpeg_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 即可

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

不过在我这边的运行环境时出现了2个的错误。

出错与解决

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

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

(2)  在构建 .......\\VC\bin\amd64\vcvars64.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%"=="" (
set VCVARS=%VSINSTANCEDIR%\VC\Auxiliary\Build\vcvars%SYSARCH%.bat
) else if "%MSVC_VER%"=="" (
set VCVARS=%VSINSTANCEDIR%\VC\Auxiliary\Build\vcvars%SYSARCH%.bat
) else if "%MSVC_VER%"=="" (
set VCVARS=%VSINSTANCEDIR%\VC\bin%MSVCVARSDIR%\vcvars%SYSARCH%.bat
) else if "%MSVC_VER%"=="" (
set VCVARS=%VSINSTANCEDIR%\VC\bin%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.0\VC\bin\amd64\vcvars64.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%\VC\Auxiliary\Build\vcvars%SYSARCH%.bat
) else if "%MSVC_VER%"=="15" (
set VCVARS=%VSINSTANCEDIR%\VC\Auxiliary\Build\vcvars%SYSARCH%.bat
) else if "%MSVC_VER%"=="14" (
echo [%VSINSTANCEDIR%\VC\bin%MSVCVARSDIR%\vcvars%SYSARCH%.bat]
REM set VCVARS=%VSINSTANCEDIR%\VC\bin%MSVCVARSDIR%\vcvars%SYSARCH%.bat
set VCVARS="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat"
) else if "%MSVC_VER%"=="12" (
set VCVARS=%VSINSTANCEDIR%\VC\bin%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. ) Visual Studio YASM integration can be downloaded from https://github.com/ShiftMediaProject/VSYASM/releases/latest ) 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、找不到文件 '..\gnulib\lib\*'

这种情况为依赖项目的子项目没有自动下载导致,这里截图对应的项目为 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:\openSourceGit\ffmpeg-vs\nettle\SMP\LINK    1
错误 LNK1181 无法打开输入文件“libnettled.lib” libhogweed   E:\openSourceGit\ffmpeg-vs\nettle\SMP\LINK 1

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

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

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

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

使用 VS2015 编译并调试 ffmpeg的更多相关文章

  1. 在CentOS下利用Eclipse调试FFmpeg

    所需软件 64位软件打包下载链接:http://pan.baidu.com/s/1i3B08Up 密码:o50u https://yunpan.cn/cBKDSbrGDgBvz  访问密码 1f55 ...

  2. VS2015编译FFMPEG,修改FFmpeg缓冲区大小解决实时流解码丢包问题,FFmpeg错误rtsp流地址卡死的问题,设置超时

    之前尝试过很多网上利用Windows编译FFmpeg的文章,都没有办法编译X64位的FFmpeg,有些教程中有专门提到编译64位的FFmpeg需要下载mingw-w64-install,但是编译的过程 ...

  3. vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func

    vs2015编译ffmpeg(版本3.0.2)引用外部库文件librtmp出现以下错误: rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 __imp__st ...

  4. VS2015编译ffmpeg的问题解决

    刚装了vs2015,打开一些ffmpeg项目,发现不能编译通过,包括stdio.h都无法找到,可能是vs2015的bug吧. 现在记录一下解决方法: 我的目录是这样定义的: C:\Program Fi ...

  5. vs2015编译OBS-Studio21.1.12

    原文地址:http://www.freesion.com/article/37445100/ 参考:https://blog.csdn.net/su_vast/article/details/7498 ...

  6. 在Windows下利用Eclipse调试FFmpeg

    目录 [隐藏]  1 环境与软件 2 第一步:安装MinGW 3 第二步:配置编译环境 4 第三步:配置SDL 5 第四步:编译 5.1 编译faac 5.2 编译fdk-aac 5.3 编译x264 ...

  7. 用VS2015编译pjsip的工程pjproject-vs14

    先去官网下载最新的源码包,我用的版本是2.7.1 . 将源代码解压后,在路径:pjproject-3.7.1\pjlib\include\pj 中创建新文件config_site.h,内容如下: #i ...

  8. 多媒体开发(8):调试FFmpeg

    编译FFmpeg得到二进制文件,之后就是对二进制库的调用,这时FFmpeg就像一个黑盒子.作为程序员,难道不想研究一下FFmpeg的具体实现?比如是怎么拿到歌曲信息的.怎么解码的.怎么推流的,等等. ...

  9. [转][JSBSim]使用VS2015编译JSBSim

    转自csdn原文:https://blog.csdn.net/yu_lei_/article/details/81463187 请大家去看原文,原文有图片和资源,本文仅供本人参考 权威参考:http: ...

随机推荐

  1. python WordCloud 实现词云

    简单示例 from matplotlib import pyplot as plt from wordcloud import WordCloud filename = "text.txt& ...

  2. 在STM32上如果不使用外部晶振,OSC_IN和OSC_OUT的接法

    如果使用内部RC振荡器而不使用外部晶振,请按照下面方法处理: 1)对于100脚或144脚的产品,OSC_IN应接地,OSC_OUT应悬空.2)对于少于100脚的产品,有2种接法:  2.1)OSC_I ...

  3. uCos-II移值(二)

    os_cpu_c.c文件 该文件主要是根据处理器平台特点完成任务堆栈初始化函数OSTaskStkInit以及其他几个用户Hook函数的编写,其中必须要实现的函数是OSTaskStkInit(在创建任务 ...

  4. java-web服务器-tomcat

    HTTP服务器: 一.定义: 1)是一个安装在服务端的[资源文件调度软件] 二.作用: 1)自动解析接收的[请求协议包]内容 2)自动在服务端计算机上定位[被访问的文件] 3)自动创建[Http响应协 ...

  5. 复习rem

    在Web页面制作中,我们一般使用“px”来设置我们的文本,因为他比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,他改变了浏览器的字体大小(虽然一般人不会去改变浏览 ...

  6. mysql下优化表和修复表命令(repair table、optimize table)

    随着mysql的长期使用,肯定会出现一些问题,一般情况下mysql表无法访问,就可以修复表了,优化时减少磁盘占用空间,方便备份. repair table table_name //修复表 optim ...

  7. BZOJ 1015 并查集&连通块

    很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或 ...

  8. 《Python基础教程》第三章:使用字符串

    find方法可以在一个较长的字符串中查找子字符串.它返回子串所在位置的最左端索引.如果没有找到则返回-1 join方法用来在队列中添加元素,需要添加的队列元素都必须是字符串 >>> ...

  9. 简单的使用Gson (序列化 和 反序化)

    下载地址:http://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5 在项目导入jar包后 package com.web; i ...

  10. 【转】用win7(64位)远程桌面连接linux(Ubuntu14.04)详细教程

    转自:http://blog.csdn.net/qq754438390/article/details/50042511 亲测,确实是可以.非常感谢原博. 用win7(64位)远程桌面连接linux( ...