FFMPEG编译之Mac

Mac下FFMPEG使用

There are a few ways to get FFmpeg on OS X.

  1. One is to build it yourself. Compiling on Mac OS X is as easy as any other *nix machine, there are just a few caveats(警告). The general procedure is get the source, then ./configure ; make && sudo make install, though specific configure flags are possible.
  2. Another is to use some “build helper” tool, to install it for you. For example, homebrew or macports, see the homebrew section in this document.
  3. Alternatively, if you are unable to compile, or do not want to install homebrew, you can simply download a static build for OS X, but it may not contain the features you want. Typically this involves unzipping an FFmpeg distribution file [like .zip file], then running it from within the newly extracted files/directories.

手动编译FFMPEG

1.下载FFMPEG源码

使用git clone https://github.com/FFmpeg/FFmpeg从github下载ffmpeg源码,切换到要使用的目标分支(这里使用release/3.3):git checkout -b r3.3 origin/release/3.3,或者直接从github下载分支release/3.3的压缩包,解压.

2.准备Xcode

Starting with Lion 10.7, Xcode is available for free from the Mac App Store and is required to compile anything on your Mac. Make sure you install the Command Line Tools from Preferences > Downloads > Components. Older versions are still available with an AppleID and free Developer account at ​developer.apple.com.

3.准备HomeBrew工具

To get ffmpeg for OS X, you first have to install ​Homebrew. If you don’t want to use Homebrew, see the section below.

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then:

1
2
brew install automake fdk-aac git lame libass libtool libvorbis libvpx \
opus sdl shtool texi2html theora wget x264 x265 xvid yasm

Mac OS X Lion comes with Freetype already installed (older versions may need ‘X11’ selected during installation), but in an atypical location: /usr/X11. Running freetype-config in Terminal can give the locations of the individual folders, like headers, and libraries, so be prepared to add lines like CFLAGS=freetype-config --cflags LDFLAGS=freetype-config --libs PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig before ./configure or add them to your $HOME/.profile file.

4.编译

Once you have compiled all of the codecs/libraries you want, you can now download the FFmpeg source either with Git or the from release tarball links on the website. Study the output of ./configure –help and make sure you’ve enabled all the features you want, remembering that –enable-nonfree and –enable-gpl will be necessary for some of the dependencies above. A sample command is:

1
2
3
4
5
6
git clone http://source.ffmpeg.org/git/ffmpeg.git ffmpeg
cd ffmpeg
./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libass \
--enable-libfdk-aac --enable-libfreetype --enable-libmp3lame \
--enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libopus --enable-libxvid
make && sudo make install

--prefix指定编译完成后安装路径,这里指定到/usr/local/ffmpeg,安装完成会在/usr/local/ffmpeg下生成:bin,include,lib,share四个目录

安装环境介绍

A package consists of several related files which are installed in several directories. The configure step usually allows the user to specify the so-called install prefix, and is usually specified through the configure option configure –prefix=PREFIX, where PREFIX usually is by default /usr/local. The prefix specifies the common directory where all the components are installed.

The following directories are usually involved in the installation:

  • PREFIX/bin: contains the generated binaries (e.g. ffmpeg, ffplay, ffprobe etc. in the case of FFmpeg)
  • PREFIX/include: contains the library headers (e.g. libavutil/avstring.h, libavcodec/avcodec.h, libavformat/avformat.h etc. in case of FFmpeg) required to compile applications linked against the package libraries
  • PREFIX/lib: contains the generated libraries (e.g. libavutil, libavcodec, libavformat etc. in the case of FFmpeg)
  • PREFIX/share: contains various system-independent components; especially documentation files and examples By specifying the prefix it is possible to define the installation layout.

By using a shared prefix like /usr/local/, different packages will be installed in the same directory, so in general it will be more difficult to revert the installation.

Using a prefix like /opt/PROJECT/, the project will be installed in a dedicated directory, and to remove from the system you can simply remove the /opt/PREFIX path. On the other hand, such installation will require to edit all the environment variables to point to the custom path.

Environment variables

Several variables defined in the environment affect your package install. In particular, depending on your installation prefix, you may need to update some of these variables in order to make sure that the installed components can be found by the system tools.

The list of environment variables can be shown through the command env.

A list of the affected variables follows:

  • PATH: defines the list of :-separated paths where the system looks for binaries. For example if you install your package in /usr/local/, you should update the PATH so that it will contain /usr/local/bin. This can be done for example through the command export PATH=/usr/local/bin:$PATH.
  • LD_LIBRARY_PATH: contains the :-separated paths where the system looks for libraries. For example if you install your package in /usr/local/, you should update the LD_LIBRARY_PATH so that it will contain /usr/local/lib. This can be done for example through the command export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH. This variable is sometimes deprecated in favor of the use of ldconfig.
  • CFLAGS: contains flags used by the C compiler, and usually includes preprocessing directives like -IPREFIX/include or compilation flags. Custom CFLAGS are usually prefixed to the source package compiler flags by the source package build system. Alternatively many build systems allow to specify the configure option -extra-cflags.
  • LDFLAGS: these are directives used by the linker, and usually include linking directives like -LPREFIX/lib needed to find libraries installed in custom paths. Custom LDFLAGS are usually prefixed to the source package linker flags by the source package build system. Alternatively, many build systems allow to specify the configure option -extra-ldflags.
  • PKG_CONFIG_PATH: contains the :-separated paths used by pkg-config to detect the pkg-config files used by many build systems to detect the custom CFLAGS/LDFLAGS used by a specific library. In case you installed a package in a non standard path, you need to update these environment libraries so that system tools will be able to detect the package components. This is especially required when running a configure script for a package relying on other installed libraries/headers/tools.

Environment variables are usually defined in the profile file, for example .profile defined in the user directory for sh/bash users, and in /etc/profile. This file can be edited to permanently set the custom environment. Alternatively, the variables can be set in a script or in a particular shell session.

Remember to export the variables to the child process, e.g. using the export command. Read the fine documentation of your shell for more detailed information.

MAC下的动态链接库

扩展名

Windows下.DLL,Linux下.so,Mac OS X下的扩展名是.dylib。 .dylib是Mach-O格式,也就是Mac OS X下的二进制文件格式。Mac OS X提供了一系列 工具,用于创建和访问动态链接库。

  • 编译器/usr/bin/cc,也就是gcc了,Apple改过的。这个主要还是一个壳,去调用其他 的一些部件。当然同时还有/usr/bin/c++,等等。
  • 汇编器/usr/bin/as
  • 链接器/usr/bin/ld

MAC下创建动态链接库步骤:

  1. 首先是生成module文件,也就是.o文件。这跟一般的unix没什么区别。例如cc -c a.c b.c,就得到a.o和b.o
  2. 可以用ld来合并.o文件,比如ld -r -o c.o a.o b.o
  3. 然后可以用libtool来创建动态链接库:libtool -dynamic -o c.dylib a.o b.o.( 这里也可以用libtool -static -o c.a a.o b.o就创建静态库)

如果用gcc直接编译,我记得linux下一般是可以: gcc -shared -o c.so a.c b.c 而在Mac OS X下需要: gcc -dynamiclib -o c.dylib a.c b.c

动态链接库的工具

nm是最常用的,这个用法跟linux下差不多:nm c.dylib,可以看到导出符号表,等等。 另一个常用的工具是otool,这个是Mac OS X独有的。比如想看看c.dylib的依赖关系otool -L c.dylib

官网方法

编译ffmpeg3.3结果没有ffplay

因为系统没有sdl环境或sdl版本不匹配,ffmpeg3.3需要sdl2

http://www.libsdl.org/download-2.0.php 下载Source Code SDL2-2.0.5.zip - GPG signed,解压缩,执行命令:

1
2
3
./configure  
make
sudo make install

进行编译

坚持原创技术分享,您的支持将鼓励我继续创作!