# FFmpeg源码安装
鉴于新版本的FFmpeg支持更多的功能,所以推荐从源码自行编译安装FFmpeg比较好。官方GIthub仓库的源码是开发版本,而且开发的版本时不时肯定有小Bug,我还是使用目前的官方网站里发行的版本的好。
官网地址:http://ffmpeg.org/releases/
Github地址:https://github.com/FFmpeg/FFmpeg
编译选择使用 **MinGW 64-bit** 的环境。
1、配置
```bash
./configure --prefix=/mingw64/usr/local/ffmpeg
```
期间遇到第一个错误:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
原因:这是因为 FFMPEG为了提高编译速度,使用了汇编指令,如MMX和SSE等。如果系统中没有`yasm`指令的话,就会该错误。
解决方法:安装yasm
```bash
#Windows系统
#还是看看源码安装吧
#Linux系统
yum install yasm
#源码编译安装
#在http://www.tortall.net/projects/yasm/releases下面找到适合自己平台的yasm版本。
#1.下载
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
#2.解压
tar zxvf yasm-1.3.0.tar.gz
#3.切换路径
cd yasm-1.3.0
#4.配置
./configure --prefix=/mingw64/usr/local/yasm
#5.编译
make
#6.安装
make install
```
第二个错误:line 1687: cmp: command not found
原因:没有diffutils依赖库
解决方法:安装diffutils
```bash
#Debian
apt-get install diffutils
#Ubuntu
apt-get install diffutils
#Alpine
apk add diffutils
#Arch Linux
pacman -S diffutils
#Kali Linux
apt-get install diffutils
#Fedora
dnf install diffutils
#OS X
brew install diffutils
#Raspbian
apt-get install diffutils
#Docker
docker run cmd.cat/cmp cmp
```
2、编译
```bash
make
#这里gcc版本我选择使用12.1.0
```
3、安装
```bash
#安装到指定目录
make install DESTDIR=/mingw64
```
4、环境变量
```bash
#如果想要在CMD中运行,那么还需要添加一些环境变量
#C:\msys64\mingw64\bin
#C:\msys64\mingw64\usr\local\bin
```
## 添加ffplay支持
SDL2:Simple DirectMedia Layer 是一个跨平台开发库,旨在通过 OpenGL 和 Direct3D 提供对音频、键盘、鼠标、游戏杆和图形硬件的低级访问。它被视频播放软件、模拟器和流行游戏使用,包括Valve的获奖目录和许多Humble Bundle游戏。SDL 正式支持 Windows、Mac OS X、Linux、iOS 和 Android。可以在源代码中找到对其他平台的支持。
SDL官网:https://www.libsdl.org/index.php
**编译生成ffplay播放器首先需要安装SDL2的库文件**
```bash
#二进制安装
sudo apt-get install libsdl2-2.0
sudo apt-get install libsdl2-dev
apt-get install libsdl2-mixer-dev
sudo apt-get install libsdl2-image-dev
sudo apt-get install libsdl2-ttf-dev
sudo apt-get install libsdl2-gfx-dev
#还是推荐源码安装
#下载源码:
wget https://www.libsdl.org/release/SDL2-2.0.20.tar.gz
mkdir /mingw64/usr/local/SDL2
./configure --prefix=/mingw64/usr/local/SDL2
make
make install
#源码编译一直存在问题,改用pacman安装
mingw64/mingw-w64-x86_64-SDL2
mingw64/mingw-w64-x86_64-SDL2_gfx
mingw64/mingw-w64-x86_64-SDL2_image
mingw64/mingw-w64-x86_64-SDL2_mixer
mingw64/mingw-w64-x86_64-SDL2_net
mingw64/mingw-w64-x86_64-SDL2_ttf
mingw64/mingw-w64-x86_64-smpeg2
```
## 添加x264编译器支持
下载地址:http://download.videolan.org/pub/videolan/x264/snapshots/
```bash
#同样是源码安装
wget https://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20191217-2245-stable.tar.bz2
tar -jxvf x264-snapshot-20191217-2245-stable.tar.bz2
./configure --prefix=/mingw64/usr/local/x264 --disable-asm
make
make install
```
## 添加libmp3lame编码器支持
下载地址:https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/sources/
```bash
#源码安装
wget https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/sources/mingw-w64-lame-3.100-2.src.tar.gz
tar -zxvf mingw-w64-lame-3.100-2.src.tar.gz
./configure --disable-shared --enable-static --prefix=/mingw64/usr/local/lamp
make
make install
#二进制安装包
pacman -S mingw64/mingw-w64-x86_64-lame
```
## 最后编译ffmpeg
```bash
./configure \
--enable-shared \
--disable-static \
--enable-debug \
--enable-sdl2 \
--enable-gpl \
#--enable-libx264 \
--enable-libmp3lame \
--extra-cflags='-I/usr/local/SDL2/include/SDL2/ -I/usr/local/x264/include -I/usr/local/lame-3.99.5/include' \
--extra-ldflags='-L/usr/local/SDL2/lib -L/usr/local/x264/lib -L/usr/local/lame-3.99.5/lib' \
# --extra-cflags='-I/mingw64/include/SDL2/ -I/mingw64/include/lame/'
--pkgconfigdir='/usr/local/x264/lib/pkgconfig/' \
--prefix=/mingw64/usr/local/ffmpeg
make
make install
```