background

Mobile learning audio and video development,FFmpeg can be said to be a must learn the framework,FFmpeg in Linux platform development, but it can also be compiled and run in other operating system environment, including Windows, Mac OS X, etc.. FFmpeg is an open source computer program for recording, converting, and streaming digital audio and video. It includes libavCodec, the leading audio/video coding library.

FFmpeg has very powerful functions, including video capture function, video format conversion, video capture, video watermark and so on. At the same time, it also supports RTP to transmit videos to RTSP supported streaming media servers and live streaming applications.

Applicable scenario

This article describes the Mac terminal automatic, manual compilation of FFmpeg library, for Mac terminal, the final generation of binary files (ex: FFmpeg, FFplay…

installation

You can install FFMPEG in the following three ways

  • Terminal installation (with Homebrew): FfMPEG keeps updated automatically this way.
  • Download the FFMPEG static library: instead of compiling it manually, we simply run the downloaded binaries. One disadvantage of static generation is that updates must be performed manually. In addition, they may not contain all the encoders or filters required.
  • Manual compilation: Download the source code and run it using the desired flags (you can specify which functions to turn on)./configureAnd finally usemakeormake install. However, configuration options must be set manually, and you need to install third-party libraries yourself.

How to choose

  • If you simply want to use ffmepg from the command line, either the first or the second method is recommended.
  • If you need to use FFMPEG in a Mac OS project and modify some of the source code in FFMPEG to fit the project, use the third method.

Specific steps

1. Install FFmpeg on a terminal

1.1. With the help ofHomebrewThe installationFFmpeg

  • The installationHomebrew

Homebrew is a package installer on the command line. Most well-known software packages or plug-ins can be installed with it. If you haven’t already installed it, use the following command to install it

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

  • Install FFmpeg

    • Generic version
    brew install ffmpeg
    Copy the code
    • Latest version, with minimal configuration (and library dependencies) options.
    brew install ffmpeg --HEAD
    Copy the code
  • Extend the installation

You can also customize the installation formula, as shown below, which can be extended according to your own needs, but may fail because some rely on third-party libraries

brew install ffmpeg --with-fdk-aac --with-tools --with-ffplay --with-freetype --with-libass --with-libvorbis --with-opus  --with-libvpx --with-x265Copy the code

1.2. Install FFMPEG in third-party libraries

After v 2.0,Homebrew no longer provides options for its core formulas. Users who want to build FFMPEG using other libraries, including non-free ones, need to use FFMPEG from a third-party repository. These libraries are not maintained by Homebrew.

Such as:

brew tap varenc/ffmpeg
brew install varenc/ffmpeg/ffmpeg
Copy the code

Update 1.3.

The update needs to be based on whether the generic version or the latest version is used in the installation steps above

  • Generic version
brew update && brew upgrade ffmpeg
Copy the code
  • The latest version
brew upgrade --fetch-HEAD ffmpeg
Copy the code

Note: If you have installed FFmPEG using Brew Install FFmpeg, you can uninstall ffmpeg using Brew Uninstall FFmpeg

2. Manually compile

2.2. Build environment – Xcode

To compile manually on a Mac, you must install Xcode and then the command line tool

  • Interface installation:Preferences > Downloads > Components
  • Command line installation:xcode-select --install

2.1. Install the dependency libraries

2.1.1. Automatic installation of dependent libraries
$ brew install automake fdk-aac git lame libass libtool libvorbis libvpx \
opus sdl shtool texi2html theora wget x264 x265 xvid nasm
Copy the code
2.1.2. Manually install the dependency libraries

FFmpeg’s compilation relies on PKG-config, which in turn relies on GLib, which in turn relies on Gettext, so the dependencies must be installed and compiled first.

  • Pkg-config & GLib & gettext
    • gettext: Edits files in the librarystpncpy.cAnd, in#ifndef weak_aliasBefore you add#undef stpncpy.
    LIBFFI_CFLAGS=-I/usr/include/ffi LIBFFI_LIBS=-lffi ./configure; make && sudo make installCopy the code
    • Glib: the most commonly used C language function library on Linux. It has good portability and practicability.
    • Pkg-config: Maintains a database that holds paths to individual code bases. Of course, this “database” is very simple, but in fact a special directory, this directory contains a series of files with the suffix “. PC “.
    GLIB_CFLAGS="-i/usr/local/include/glib - 2.0 - I/usr/local/lib/glib - 2.0 / include" GLIB_LIBS="- lglib - 2.0 - lgio - 2.0" ./configure --with-pc-path="/usr/X11/lib/pkgconfig:/usr/X11/share/pkgconfig:/usr/lib/pkgconfig:/usr/local/lib/pkgconfig"
    Copy the code

Mac OS X Lion has its own stpncpy function, which conflicts with gettext repeatedly.

  • Nasm

Nasm is the assembler required for X264. The latest version is available at nasm.us.

  • Additional library
    • x264: –enable-gpl –enable-libx264
    • fdk-aac: –enable-libfdk-aac
    • libvpx: –enable-libvpx
    • libvorbis: –enable-libvorbis
    • libopus:
    • LAME:–enable-libmp3lame
    • libass:–enable-libass

2.3. The Freetype

Freetype has been installed on macOS (older versions may require X11 during installation), but not in a typical location

Add the following command to the./configure file of freetype

CFLAGS=`freetype-config --cflags`
LDFLAGS=`freetype-config --libs` PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/opt/X11/lib/pkgconfig
Copy the code

Compile 2.4.

With all of the above dependencies, you can link to and download the FFmpeg source code. Detailed compilation steps can be found in the Generic Compilation Guide.

Run./configure –help to see what options are available.

  • download
$ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
$ cd ffmpeg
Copy the code
  • compile
    • A complete compilation
    
    $ ./configure  --prefix=/usr/local --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 \
    --samples=fate-suite/
    make
    Copy the code
    • The minimalist compilation
    
    $ ./configure  && make
    Copy the code
  • The installation
sudo make install
Copy the code

At this point,FFmpeg is installed successfully.

Note: If the above process error, need to be solved separately, because the FFMPEG version and its dependent library version is constantly upgrading, so there may be some unexpected problems, please follow the prompts to solve separately.

3. Manual compilation details

3.1. Why

Ffmpeg libraries can come from a variety of sources, such as official auto-compiled libraries or compiled libraries from personal or third-party software on the Internet. But these libraries have the following disadvantages

  • Binary packages from older versions are out of date, containing serious bugs or missing required features that are fixed in the new version.
  • You cannot customize compilations, such as getting platform-specific optimizations or linking specific libraries that are not supported in binary packages.
  • You cannot change the source code, for example if you need to change the source code so that FFMPEG can be customized.

3.2. Step

  • Configure script (edit script file./configure)
  • Compile (make)
  • Installation (make install)

1>. Configuration: Allows the creation of the necessary files for the compilation step, and is done through the configuration scripts typically provided with the source package. During configuration, you can define installation prefixes and enabled components.

2>. Compilation: Compilation usually involves running make after the configuration steps are complete. At this stage, the required libraries and binaries are generated.

3> Installation: Installation will install the binaries and libraries in the path specified in the configuration step. Note that since you can use the compiled binaries in the compiled path, this step is not required.

./configure
make
make install
Copy the code

Note: Compile the files in the source directory and install the library in /usr/local. The third step may require superuser permissions (and therefore may need to be replaced by sudo make install) because /usr/local cannot be modified by ordinary users.

3.3. Installation path

The configuration step typically allows the user to specify what is called an install prefix, and is usually specified through the configuration option configure –prefix = prefix, where prefix usually defaults to /usr/local. The prefix specifies the public directory where all components are installed.

The following directories are usually involved in the installation:

  • PREFIX/bin: contains generated binaries (e.g. ffmpeg, ffplay, ffprobe etc. In the case of ffmpeg)
  • PREFIX/include: Libavutil /avstring.h, libavcodec/avcodec.h, libavformat/avformat.h etc. In case of FFmpeg
  • PREFIX/lib: contains generated libraries (e.g. libavutil, libavcodec, libavformat etc. In the case of FFmpeg)
  • PREFIX/share: contains various components that are independent of the system; Especially documentation files and examples

It is generally recommended to use the default path, but if the quantity uses a PREFIX like/opt/PROJECT /, the PROJECT will be installed in a dedicated directory. To remove it from the system, simply delete the/opt/PREFIX path. However, such an installation would require editing all environment variables to point to a custom path.

3.4. Environment variables

Several variables defined in the environment affect your package installation. In particular, depending on your install prefix, you may need to update some of these variables to ensure that system tools can find installed components. You can use the env command to display a list of environment variables.

Here is a list of affected variables:

  • PATH: Defines the simplified PATH of variables. The system searches the PATH of binary files. For example, if you install packages in /usr/local/, update PATH to include /usr/local/bin. You can do this by running the export PATH = /usr/local/bin: $PATH command.
  • LD_LIBRARY_PATH: the path where the system looks up libraries. For example, if you install packages in /usr/local/, LD_LIBRARY_PATH should be updated to include /usr/local/lib. This can be done by running the export LD_LIBRARY_PATH = / usr/local/lib: $LD_LIBRARY_PATH command. Sometimes this variable is not recommended and ldConfig is used instead.
  • CFLAGS: Contains flags used by the C compiler, usually including preprocessing directives such as -iprefix/include or compile flags. Custom CFLAGS are usually prefixed by the source package build system as the source package compiler flags. Alternatively, many build systems allow you to specify the configuration option -extra-cflags.
  • LDFLAGS: These are the directives used by the linker and usually include linking directives, such as -lprefix/lib, needed to find libraries installed in a custom path. Custom LDFLAGS are usually prefixed by the source package builder system as the source package linker flag. Alternatively, many build systems allow you to specify the configure option -extra-LDFlags.
  • PKG_CONFIG_PATH: the path used by PKg-config to detect pkg-config files used by many compilation systems to detect custom CFLAGS/LDFLAGS used by specific libraries.

If you install packages in a non-standard path, you need to update these environment libraries so that system tools can detect package components. Be sure to do this when running configuration scripts for packages that depend on other installed libraries/headers/tools.

Brief Instructions

  • Commonly used libraries
The library use
ffmpeg A command line tool for converting video files to formats, also supports real-time encoding of TV cards
ffserver An HTTP multimedia real-time broadcast streaming server that supports time panning
ffplay A simple player, based on SDL and FFmpeg library
libavcodec Contains all FFmpeg audio/video codec libraries
libavformat Contains demuxers and Muxer libraries
libavutil Contains several tool libraries
libpostproc A library for video pre-processing
libavutil Contains several tool libraries
libswscale libswscale
  • The main parameters
Parameter names meaning
-i Set the input file name.
-f Set the output format.
-y Overwrite the output file if it already exists.
-fs If the specified file size is exceeded, the conversion ends.
-ss The conversion starts at the specified time.
-t The conversion starts from -ss time (e.g. -ss 00:00:01.00 -t 00:00:10.00 that is, from 00:00:01.00 to 00:00:11.00).
-title Set the title.
-timestamp Set the timestamp.
-vsync Adding or subtracting frames synchronizes audio and video.
Video parameter name meaning
b:v Set the video traffic. The default value is 200Kbit/ SEC. (Please refer to notes below for units)
r Set the frame rate value. The default is 25.
s Set the width and height of the screen.
aspect Set the scale of the screen.
vn Do not process video, used only for sound processing.
vcodec( -c:v ) Set video Video codec. If not set, use the same codec as the input file.
Sound parameter name meaning
b:a Set the traffic of each Channel (the latest SVN version is the sum of all channels). (Please refer to notes below for units)
ar Set the sampling rate.
ac Set the number of sound channels.
acodec ( -c:a ) Set the audio codec. If not, use the same codec as the input file.
an Does not process sound, used only for video processing.
vol Set the volume to 256 as the standard volume. (To double the volume, type 512, and so on.)
  • ffprobe

To check if an encoded file is correct, use the following command, and print a bunch of errors if the format is wrong.

$ ffprobe -show_frames /xx.h265

$ ffprobe -print_format json -show_format -show_streams -i /xx.h265

Copy the code
  • ffmpeg

View basic file information

$ ffmpeg -i /xx.h265

Copy the code
  • ffplay

Ffplay plays video frame by frame and displays video frame number

This feature is currently used to check the cause of some incorrect videos. Use FFplay under macOS, press S key to play the video in a single frame, and then display the frame number of the current picture with a video filter that displays text. The command example is shown below.

$ ffplay -vf "drawtext=fontfile=/Library/Fonts/Arial.ttf:text=%{n}:box=1:x=(w-tw)/2:y=h-(2*lh)" xx.mp4
Copy the code

Reference documentation

  • Official Installation Tutorial