Mac FFmpeg source code compilation

The installation

You can install FFMPEG in the following three ways

  1. Terminal installation (with Homebrew): FfMPEG keeps updated automatically this way.

  2. 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.

  3. Manual compilation: Download the source code, then run./configure with the desired flags (you can specify to enable the desired functionality), and finally make or make 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.

1. Install FFmpeg on a terminal

  • 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)"
Copy the code
  • Install FFmpeg
  1. To install the common version, simply type brew install ffmpeg on the command line

  2. The latest version is installed and provides minimal configuration (and library dependencies) options by typing brew install ffmpeg –HEAD on the command line

  3. Extension installation, you can customize the installation formula, as follows, can be based on their own needs to expand, 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
  • 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.


brew tap varenc/ffmpeg

brew install varenc/ffmpeg/ffmpeg

Copy the code
  • update
  1. For common version updates, simply enter the terminal: brew update && brew Upgrade ffmpeg

  2. For the latest version, enter brew upgrade –fetch-HEAD FFmpeg

  3. If you have installed FFMPEG using Brew Install FFmpeg, you can uninstall ffmpeg using Brew Uninstall FFmpeg

2. Manually compile

2.1 Compilation Environment – Xcode

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

  • Preferences > Downloads > Components

  • Xcode -select –install

2.2 Installing dependent Libraries

  1. Automatically install dependent libraries by typing:

$ brew install automake fdk-aac git lame libass libtool libvorbis libvpx \

opus sdl shtool texi2html theora wget x264 x265 xvid nasm

Copy the code
  1. 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
  1. Gettext: Edit the file stpncpy.c in the library and add #undef stpncpy before # ifNdef weak_alias.
LIBFFI_CFLAGS=-I/usr/include/ffi LIBFFI_LIBS=-lffi ./configure; make && sudo make installCopy the code
  1. Glib: the most commonly used C language function library on Linux. It has good portability and practicability.

  2. 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 libraries
  1. x264: --enable-gpl --enable-libx264

  2. fdk-aac: --enable-libfdk-aac

  3. libvpx: --enable-libvpx

  4. libvorbis: --enable-libvorbis

  5. libopus:

  6. LAME:--enable-libmp3lame

  7. libass:--enable-libass

2.3 install 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 the 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

2.4 build

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
  1. 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
  1. Simplest compilation:

$./configure && make

Copy the code
  • The installation

sudo make install

Copy the code

2.5 Manual Compilation Procedure

  • 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.

2.6 Installation Path Configuration

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.

2.7 Environment Variable Configuration

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.

3. FFmpeg parameters

  • Commonly used libraries

| | library name | purposes

| : – : | : – : |

Ffmpeg | a command-line tool, used for video file format conversion, also supports real-time encoding of TV card

Ffserver | a HTTP multimedia real-time broadcast streaming server, support time translation

Ffplay | a simple player, based on SDL and FFmpeg library

Libavcodec | contains all FFmpeg audio/video codec library

Libavformat | contain demuxers and muxer library

Libavutil | library contains some tools

Libpostproc | for video library do before processing

Libavutil | library contains some tools

libswscale | libswscale

  • The main parameters

Parameter name | | | meaning

| : – : | : – : |

-i | set the input file name.

– f | set output format.

– if y | override file output file already exists.

– more than the specified file size fs | end of conversion.

– ss | began to transform from the specified time.

– t | since – ss time transformation (such as – ss 00:00:01. 00 – t 00:00:10. 00 namely from 00:00:01. 00 began to 00:00:11. 00).

Title – the title | Settings.

Timestamp – a timestamp | Settings.

– vsync | Frame to synchronize audio increase or decrease.

| | | video parameter name meaning

| : – : | : – : |

B: v | set video traffic, defaults to 200 kbit/SEC. (Please refer to notes below for units)

R | set the frame rate value, the default is 25.

S | set wide and high of the picture.

The proportion of the aspect | Settings screen.

.vn | not handle video, on voice against doing only used when processing.

Vcodec (- c: v) | set video video codec, is used when not set the same as the input file of the codec.

| | voice parameter name | meaning

| : – : | : – : |

B: a | Settings for each Channel (a sum of all the latest SVN version for Channel) traffic. (Please refer to notes below for units)

Ar | set the sampling rate.

Ac | set voice Channel number.

Acodec | – c: (a) set up the voice codec, not set with the same video, use the same as the input file of the codec.

An | not handle voice, on video against doing only used when processing.

Vol | set the volume size, 256 for the standard volume. (To double the volume, type 512, and so on.)

  • Check out FFmpeg’s official documentation
  1. View basic information about H265

ffmpeg -i /Users/tomxiang/Desktop/h265/test_tomxiang.h265

Copy the code
  1. In turn libx265 mp4.

ffmpeg -i /Users/tomxiang/Desktop/test_tomxiang.h265  -c:v libx265 /Users/tomxiang/xxtest/test265.mp4

Copy the code
  1. Ffplay plays video frame by frame and displays video frame number

Use FFplay under macOS, press S 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 as follows:


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

Copy the code

Windows under FFmpeg source code compilation

1. Configure the environment

To prepare, download the following four applications.

1) MinGW MSYS Bundle

2) SDL note: the 2.0.3 version of the source code in this environment will have problems compiling, not recommended.

3) yasm

4) FFmpeg

1.1 Install the WinGW compilation environment

  1. MinGW(Minimalist GNU for Windows) : A tool chain that AIDS in compiling Windows programs. Install through minGW Installation Manager.

  2. MSYS(Minimal SYStem) : is a small GNU environment, including basic bash, make, etc. It can also be installed through minGW Installation Manager.

  3. Open minGW Installation Manager and check it as shown below:

Click Installation->Apply Changes to install the base package.

1.2 installationyasm

  1. Yasm is an assembly compiler because ffMPEG uses assembly instructions such as MMX and SSE for efficiency. So you need to install this tool.

  2. If yASM or NASM is not installed, the error “nasm/ yASM not found or too old” is encountered during compilation.

  3. Download yASM and rename the executable line file yasm.exe to the %MINGW_PATH%/bin directory. MINGW_PATH is the MINGW installation directory.

  4. Unzip the yASM source package to F: Work\ ffmPEG-2.4-yasm1.2.0. Go to the yasm directory and run the following commands:


./configure --prefix=/usr/local/yasm

make

make install

Copy the code
  1. Run the following command to import yASM environment variables

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

Copy the code

1.3 installation PKG – config

Pkg-config is an auxiliary configuration and link tool that can easily support automatic configuration of GCC.

Recommended reference: PKg-config for mingW Installation and configuration

Note that the environment variable, PKG_CONFIG_PATH (pkG-config default library dependency lookup directory), is configured.

You can also place pkg-config.exe under %MINGW_PATH%/bin.

1.4 Download and InstallationSDL

Since compiling FFPlay requires SDL, you need to install SDL first.

  1. SDL Installation 1:
  • Unzip the SDL source code into the F:\Work\ ffmPEG-2.4-sDL-1.2.15 directory. Go to the SDL directory and execute separately

./configure --prefix=/usr/local/SDL

make

make install

Copy the code

The SDL installation is complete.

  • After SDL is compiled, the generated bin, include, and lib files are stored in C:\msys\1.0\local\SDL.

  • Run the following command to import the SDL environment variables.


export PATH="$PATH:/usr/local/SDL/bin:/usr/local/SDL/include:/usr/local/SDL/lib"

Copy the code

If no environment variable is set, ffmpeg configure displays the SDL support item as no.

  1. SDL Installation 2:
  • Download SDL2, unpack it, copy the files from include/ SDL, lib, bin to the corresponding directory of %MINGW_PAH%/msys/1.0.

  • /opt/local/x86_64-w64-mingw32 = %MINGW_PAH%/msys/1.0 There are also several places where prefix is specified as /opt/local/x86_64-w64-mingw32 that need to be modified and replaced by a global search (no better method is found).

Description:

If –enable-sdl is added, an error message will be displayed if no matching SDL is found. If this parameter is not added, no error message will be displayed.

If you encounter problems, check the test SDL logs in config.log to see if there is a problem with the SDL version or the location of the specified library. I ran into a problem where the SDL specified by prefix was in the wrong location, causing the compiled header file to be in the wrong location and not finding the header file.

1.5

2. Compile FFmpeg

  1. Unzip ffmPEG source code (ffmPEg-2.4.tar. bz2) to F:\Work\ ffmPEG-2.4ffmPEG-2.4, go to ffmpeg code directory, and execute the following commands:

#Enter the FFmpeg source directory

cd FFmpeg

#Configure compilation parameters./configure --prefix=.. /buildout --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl
#compile

make

make install

Copy the code
  1. Then check the config.h file in FFmpeg compilation directory and find the HAVE_SDL and CONFIG_SDL macros. If they are both 1, it indicates that the SDL configuration is successful.

  2. Possible problems:

  • Error ffbuild/common.mak:174 missing separater.stop

Solutions:

Change git config –global core. Autocrat lf false

Then run the following command to remove the FFmpeg code and retrieve it again

  • Sdl2. DLL cannot be found when running ffplay.exe

Solutions:

Copy sdl2. DLL from bin in SDL to ffplay.exe.

Linux FFmpeg source code compilation

IOS FFmpeg source code compilation

Introduction to IOS Coding

There are two kinds of codec in iOS, hard codec and soft codec. You can refer to the following table:

Support | | hardware | | type tool background note | | | thinking

| : – : | : – : | : – : | : – : | : – : | : – : |

Hard codec | VideoToolBox | the CPU or dedicated processor | coding (iOS > = 10.0), decoding does not support | VideoVTToolBox | – |

Hard codec | | AVAssetWriter | the CPU or dedicated processor | | support coding needs to write video into a local file, and then by the changes of real-time monitoring the file content, read a file and deal with the packet | – |

Soft decoding | | FFmpeg | – | – | | | CPU support

The IOS build FFmpeg

  • Ffmpeg can be installed on IOS in one of the following ways:
  1. Download the iOS version of ffMPEG static library: instead of compiling manually, we only need to download the header file and the. A library file.

  2. Manual compilation: download the source code, you can change some flag or source code and then compile the script, more flexible.

If you simply want to use FFMEG directly, you can download a stable version of the static library. The first method is recommended.

If you need to customize the use of FFMPEG in an iOS project and modify some of the source code in FFMPEG to fit the project, use the second approach.

  • Resources for IOS compilation can be downloaded from this Baidu cloud:FFmpeg for IOSExtraction code: 5400

Baidu cloud link: pan.baidu.com/s/1xeHNgHHG… Password: 5400

1. Download the gas – preprocessor

Download the gas – preprocessor

This file is required for compiling FFmpeg. Run the following command to copy it to bin


cp -f /xxx/gas-preprocessor.pl /usr/local/bin/

Copy the code

2. Install yasm

Download the yasm

Yasm is a fully rewritten NASM assembly and supports x86 and AMD64 instruction sets.


brew install yasm

Copy the code

3. Download x264-ios compilation script and source code

Download x264-ios compilation script and source code

  1. Download the x264 compilation script and decompress it as follows

  1. Then download the latest version of the source code after decompression as follows

  1. Rename the source folder to x264 and put it in the compile scripts folder (x264-ios-master)

After modification, see the following figure:

The file directory specified in the compilation script is X264, so you need to change the name, or you can change the compilation script

It is best to manually force the lower GCC location, otherwise an error may be reported, and then execute the following command:

CD to your x264-ios-master folder and run:


sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/

./build-x264.sh

Copy the code
  1. After executing, you can see that the X264-ios folder has been generated

  1. Possible compilation errors:
  • No working C compiler found: An error is reported when the new MAC is compiled directly because some necessary tools for GCC are missing. Maybe the reason is that the position of Xcode is changed, because we mainly use the real machine for debugging, so we can only keep ARM64 in the compilation script, after the following modification, it can be directly compiled. However, a setup like the emulator won’t work with X264 because we’ve compiled only the libraries needed for the real machine.
ARCHS="arm64 x86_64 i386 armv7 armv7s" instead of ARCHS="arm64"Copy the code

4. Download ffMPEG-ios compilation script and source code

Download FFMPEG-ios compilation script and source code

Note: here you can only download the FFMPEG-ios compilation script, do not download the source code, the execution script will automatically download the source code, if you do not want to automatically download the source code, you can manually download the source code, slightly modify the FFmpeg compilation script. I won’t say too much here.

  1. Modify the content of the script (build-ffmpeg.sh)

  1. The x264 compiled folder must be in the current directory and named fat-x264, so the x264-ios folder generated in step 3 was renamed fat-264 and placed in ffmpeg-ios-build-script. The directory structure is as follows:

  1. Modify the following:
Change CFLAGS="$CFLAGS -mios-version-min=$deployment_target-fembed-bitcode "to CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"Copy the code

Find line 104 in build-ffmpeg.sh and change to CFLAGS=”$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET” as shown below:

  1. Since we only compiled ARM64 x264 in the previous step, we will only compile ARM64 FFmpeg here. Modify the script file slightly.
ARCHS="arm64 armv7 x86_64 i386" changed to ARCHS="arm64"Copy the code

ARCHS=”arm64 armv7 x86_64 i386″ change to: ARCHS=”arm64″ in build-ffmpeg.sh As shown below:

  1. If you want to use avutil. H, you need to change the script

A structure named “AVMediaType” in the FFmpeg framework conflicts with apple’s own framework, so we have to modify the compilation script to use “FFmpegAVMediaType” instead of “AVMediaType”. Here you need to add the following command line to the script file, replacing AVMediaType with FFmpegAVMediaType. Note: $SOURCE is the root of ffmpeg.


grep -rl AVMediaType ./$SOURCE | xargs sed -i .bak s@AVMediaType@FFmpegAVMediaType@g

Copy the code
  1. Compiling script files

./build-ffmpeg.sh

Copy the code

After executing the script, download the source code first:

5. The Xcode compilation

  1. Xcode creates a new iOS project

Create a new iOS project and rename viewController.m to viewController.mm. This is required because FFmpeg involves C++ coprogramming.

  1. Put the FFmpeg compiled headers and culls into the project and test the code in the master controller. A bunch of errors will be thrown, one at a time.

  2. Adding a dependency library

  1. Disable Bitcode in Build Setting

  1. Set the location of the header file and library in Build Setting

This is especially important because in most projects and in FFmpeg’s own source code, the imported header file is in the following format

Android FFmpeg source code compilation