demand

Manual compilation open X264 function FFmpeg and put into the new project, you can compile successfully. For later use.

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.

installation

You can install FFMPEG in either of the following ways

  • 1. Download the iOS version of ffMPEG static library: no manual compilation, we only need to download the header file and.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.

How to choose

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

Prerequisites for reading:

  • Fundamentals of Audio and Video
  • Basic terminal command line
  • FFmpeg basis

Note: the text only compiles the arm64 environment used by the real machine, so the project cannot run under the emulator. Change the two script files if you need to add other architectures.

All installation packages required for this article are in the following links, because the official website address below is an external network, students without VPN may be slow to download. So I sorted out the latest version of all the baidu cloud disk below.

Link: https://pan.baidu.com/s/1nYBQUi8drEpkjmwlIpbetQ extraction code: ercwCopy the code

1. Downloadgas-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. Installyasm

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

brew install yasm
Copy the code

3. DownloadX264-ios compiles scriptsandThe source code

  • Download the x264 compilation script and decompress it as follows

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

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

If you want to manually set the directory of GCC to x264, an error may occur. Then run the following command:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
./build-x264.sh
Copy the code
  • Pay attention to

No working C Compiler found: An error is reported when the new MAC is compiled directly because some necessary GCC tools are not found. 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"As follows ARCHS="arm64"
Copy the code

After executing, you can see that the X264-ios folder has been generated

4. DownloadFfmpeg-ios compiles scriptsandThe 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.

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

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:

And modify the following content

CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"Modified to CFLAGS ="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
Copy the code

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"Modified to ARCHS ="arm64"
Copy the code
  • If you want to use avutil. H, you need to change the script

Note: 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

Compiling script files

./build-ffmpeg.sh 
Copy the code

5. The Xcode compilation

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

  • 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

  • Adding a dependency library

Note: The FFmpeg source code calls some iOS libraries, so we must import the dependent libraries into the project.

  • Disable Bitcode in Build Setting

  • 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

Refer to the article

  • FFmpeg compilation