MAC搭建FFmpeg开发环境

1,365 阅读1分钟

Homebrew

安装Homebrew

/bin/zsh -c "$(curl -fsSL https://gitee.com/rong5690001/HomebrewCN/blob/master/Homebrew.sh)" 

根据提示,输⼊密码就可以⼀步⼀步装成功。 如果之前没有安装xcode,则会提示安装xcode,安装完xcode后,我们需要再次安装brew

使用Homebrew

  • 安装:brew install 软件名,例:brew install git
  • 搜索:brew search 软件名,例:brew search git
  • 卸载:brew uninstall 软件名,例:brew uninstall git
  • 更新所有:brew update
  • 注意:因为brew update会通过git命令来更新,所以必须先安装git 更新具体软件:brew upgrade 软件名 ,例:brew upgrade git

FFmpeg编译

下载FFmpeg4.2版本

1 git clone git://source.ffmpeg.org/ffmpeg.git
2 或者码云的链接
3 git clone https://gitee.com/mirrors/ffmpeg.git
4 cd ffmpeg
5 # 查看版本
6 git branch -a 7 # 选择4.2版本
8 git checkout remotes/origin/release/4.2

安装第三方库

最好⼀次只安装⼀个库⽂件,多个安装的时候可能存在冲突。

1 brew  install --build-from-source automake
2 brew  install fdk-aac 
3 brew  install lame  
4 brew  install libass  
5 brew  install libtool 
6 brew  install libvorbis 
7 brew  install libvpx  
8 brew  install libvo-aacenc  
9 brew  install opencore-amr  
10  brew  install openjpeg  
11  brew  install opus  
12  brew  install speex 
13  brew  install texi2html 
14  brew  install x264  
15  brew  install --build-from-source x265
16  brew  install xvid  
17  brew  install yasm  
18  brew  install freetype  
19  brew  install pkg-config  
20        
21  //brew install celt
22  brew install schroedinger
23  brew install shtool
24  brew install --build-from-source theora
25  brew install --build-from-source wget

下载安装⽐较慢的库

brew install x264
brew install --build-from-source x265 
brew install libass

之所以安装慢,是因为我⽤了⼀台重装系统后的mac,很多依赖⽂件都没有下载,⽐如以下是安装brew install x264 时候的打印

==> Downloading  https://ftpmirror.gnu.org/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz
​
==> Downloading  from  https://mirrors.aliyun.com/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz ######################################################################## 100.0%
​
[==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/x264- r3027_1.high_sierra.bottle.tar.gz](https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/x264-r3027_1.high_sierra.bottle.tar.gz) ######################################################################## 100.0%
​
==> Installing dependencies for x264: gmp, isl, mpfr, libmpc and gcc  ⽐如这⾥的依赖
​
==> Installing x264 dependency: gmp
​
==> Pouring  gmp-6.2.1.high_sierra.bottle.tar.gz
​
#### 🍺 /usr/local/Cellar/gmp/6.2.1: 21 files, 3.2MB
​
==> Installing x264 dependency: isl
​
==> ./configure --prefix=/usr/local/Cellar/isl/0.23 --with-gmp=system --with-gmp- prefix=/usr/local/opt/gmp
​
==> make install
​
#### 🍺 /usr/local/Cellar/isl/0.23: 72 files, 4.8MB, built in 10 minutes 9 seconds
​
==> Installing x264 dependency: mpfr
​
==> Pouring  mpfr-4.1.0.high_sierra.bottle.tar.gz
​
#### 🍺 /usr/local/Cellar/mpfr/4.1.0: 29 files, 5.1MB
​
==> Installing x264 dependency: libmpc
​
==> Pouring  libmpc-1.2.1.high_sierra.bottle.tar.gz
​
#### 🍺 /usr/local/Cellar/libmpc/1.2.1: 13 files, 381.9KB
​
==> Installing x264 dependency: gcc
​
==> ../configure  --build=x86_64-apple-darwin17  --prefix=/usr/local/Cellar/gcc/10.2.0_2  -- libdir=/usr/local/Cellar/gcc/1
​
==> make  BOOT_LDFLAGS=-Wl,-headerpad_max_install_names 

错误解决⽅法:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused的⼏种解决⽅式

1、查看⽹址

打开⽹站www.ipaddress.com/] 查询⼀下raw.githubusercontent.com 对应的IP 地址

image-20220313154717600

2、替换系统的host文件

image-20220313154759583

image-20220313154804796

image-20220313154811624

安装SDL2

由于ffplay需要sdl2的⽀持,所以我们提前安装,如下所示.

brew install --build-from-source sdl2

配置编译ffmpeg

./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-versi on3 --enable-nonfree --enable-postproc --enable-libass --enable-l ibcelt --enable-libfdk-aac --enable-libfreetype --enable-libmp3lam e --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-l ibopenjpeg --enable-openssl --enable-libopus --enable-libspeex --e nable-libtheora --enable-libvorbis --enable-libvpx --enable-libx
264 --enable-libxvid --disable-static --enable-shared

以这⾥为准:

./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --disable-libass --enable-libfdk-aac --enable-libfreetype  --enable-libopenjpeg --enable-libopus --enable-libspeex --enable-libvorbis --enable-libvpx --enable-libx264 --enable-static --enable-shared
1 make -j4
2 sudo make install

配置环境变量

编辑 ~/.base_profile,并添加ffmpeg到环境变量中

export PATH="$PATH:/usr/local/ffmpeg/bin"

如果想⽴刻⽣效,则可执⾏下⾯的语句:

source ~/.base_profile

⼀般环境变量更改后,重启后⽣效。

简单测试

播放rtmp链接

ffplay rtmp://r.ossrs.net/live/livestream 
ffplay rtmp://58.200.131.2:1935/livetv/hunantv

ffmpeg环境搭建完成。