Ios:ffmpeg Compiling and using learning

Source: Internet
Author: User

FFmpeg is a multi-platform multimedia processing tool that handles video and audio functions very powerful. Currently found on the Internet on iOS using ffmpeg data are relatively old, and ffmpeg update iteration faster, and the online explanation is not enough detailed, for the first contact with FFmpeg novice (for example, I) is not very good use. To prevent forgetting, here's a summary of using ffmpeg under iOS.

1. Simple understanding of the FFmpeg hierarchy

To use FFmpeg, you first need to understand the FFMPEG code structure. According to Chi's tip, FFmpeg's code consists of two parts, part of the library, and part of the tool. API is in the library, if the direct API to manipulate the video, you need to write C or C + +. The other part is the tool, using the command line, you do not need to code to implement the video operation process. In fact, the tool simply transforms the command line into an API operation.

2. Preheat-use ffmpeg under Mac OS

Using FFmpeg in Mac OS is simple and can be manipulated directly using the command line. First install FFmpeg, where the default system is already installed brew, only need to enter on the terminal:

Brew Install FFmpeg

Wait for the installation to finish.

After the installation is complete, try the following command:

Ffmpeg-i Input.mp4 Output.avi

If the switch is successful, the installation succeeds

3. Compiling FFmpeg library libraries that can be used under iOS

This step is to compile 1 of the said library, after compiling can call FFmpeg API. There are some methods on the Internet, but they have to be compiled manually, slightly more complex and older. According to the needs of the App Store, the compiled package must also support arm64. I found a "one-click Compile" script in the Magnum GitHub address as follows:

Https://github.com/kewlbear/FFmpeg-iOS-build-script

and write this script of crooked nuts very good, updated very timely, has been updated to the latest 2.5.3 version. Download it down with only one build-ffmpeg.sh script file. In the terminal to the script directory, execute the command:

./build-ffmpeg.sh

The script will automatically ffmpeg the source code from GitHub to the local and start compiling.

After the compilation is complete, the file directory is as follows:

Some summary of using ffmpeg under "iOS development" ios

Among them, the ffmpeg-2.5.3 is the source code, the Ffmpeg-ios is compiles the library, inside has we need. A static library, a total of 7.

Execute command:

Lipo-info LIBAVCODEC.A

View. A package supported architecture, these packages support ARMv7 armv7s i386 x86_64 arm64 These architectures, this script is indeed the industry conscience AH ~ ~ ~

4. Introduction of FFmpeg library libraries in Xcode

Create a new project, drag the compiled Ffmpeg-ios onto the Xcode project, add a header file reference

#include "Avformat.h"

Add an API statement:

Av_register_all ();

Add an empty class to change the execution file. m suffix to. mm and turn on mixed mode.

Add the appropriate framework, including avfoundation and CoreMedia.

Run the project and if there is no error, the compilation is successful.

5. Using the command line in a Xcode project

Proceed to step 4th and you are ready to use the library. However, if you want to operate the video, or you need to manually write a lot of code to invoke the API, the workload is large, it is not as easy as direct writing command line. For the command line to work in the Xcode project, you need to do the following:

(1) Add tools in the source code, specific files include:

Some summary of using ffmpeg under "iOS development" ios

(2) Add header Search Paths

Search Paths in the Target--build setting and add the path to the source ffmpeg-2.5.3 and scratch below the header search Paths.

(3) Modify ffmpeg.h and FFMPEG.C source code

If you run this project at this point, you will get an error, because there are 2 main functions in the project, at this point the processing method is:

Add a function declaration in Ffmpeg.h:

int ffmpeg_main (int argc, char **argv);

Find the main function in FFMPEG.C and change the main function to Ffmpeg_main.

(4) Invoking command-line examples

Add header file: #import "Ffmpeg.h"

Invoke command line

int Numberofargs = 16;

char** arguments = calloc (Numberofargs, sizeof (char*));

Arguments[0] = "FFmpeg";

ARGUMENTS[1] = "-I";

ARGUMENTS[2] = InputPath;

ARGUMENTS[3] = "-SS";

ARGUMENTS[4] = "0";

ARGUMENTS[5] = "-T";

ARGUMENTS[6] = Durationchar;

ARGUMENTS[7] = "-vcodec";

ARGUMENTS[8] = "Copy";

ARGUMENTS[9] = "-acodec";

ARGUMENTS[10] = "AAC";

ARGUMENTS[11] = "-strict";

ARGUMENTS[12] = "2";

ARGUMENTS[13] = "-b:a";

ARGUMENTS[14] = "32k";

ARGUMENTS[15] = OutputPath;

int result = Ffmpeg_main (Numberofargs, arguments);

Where InputPath and OutputPath are file paths. After testing, these two paths do not support the ASSET-LIBRARY://protocol and the FILE://protocol, so if it is to use the album file, my current solution is to copy it into the sandbox.

6. Change the shutdown process to close the thread

If it goes to the 5th step, the app is able to use the command line to process the video, but there is a problem and the app exits. The Shaoda reminds you that the command line exits the process after it has been executed. Only one process can be started under iOS, so it is necessary to turn off the process to close the thread, or to simply drop the method of closing the process.

As you can see in ffmpeg.c, the way to execute the exit process is Exit_program, which navigates to the exit method in CMDUTILS.C that executes the C language. Here I changed it to Pthread_exit (need to add # include header file). When used in a Xcode project, you can use Nsthread to open a new thread and then close the thread after execution is complete. You can then listen to the thread exit by using the NSTHREADWILLEXITNOTIFICATION notification.

7. Fix a bug inside the FFMPEG.C

In a real-world project, you might need to invoke the command line multiple times, but in the process of invoking the command line multiple times, discovering that FFMPEG.C's code accesses an empty property causes the program to crash. Gradually debug found that many pointers have been empty, but their count is not zeroed, do not know is not a ffmpeg.c bug. The fix is as follows: Under the Ffmpeg_cleanup method, the individual counters are zeroed, including:

Nb_filtergraphs

Nb_output_files

Nb_output_streams

Nb_input_files

Nb_input_streams

After placing 0, repeat the Ffmpeg_main method for all normal.



Mr. Wen/l1 (author of Jane's book)
Original link: http://www.jianshu.com/p/52516bdc1eb5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

I. BACKGROUND

There are many ffmpeg compiling configuration information on the Internet, most of which is about the latest version of FFmpeg (2.0), I began to think of writing a 2.0 version, can be put on the project to take over, found a variety of problems (not fast forward, no sound), and then look at the code a bunch of warnings, the reason is very simple, The FFmpeg library used is too new and many interfaces have changed. Because there is not much information on the hand, do not know which version of the project using the FFmpeg library, a little bit to find, and finally know that the original use is 0.7.x. Find the target version of the FFmpeg this thought is all over, later found that this is the beginning of the pit, has undergone a series of hardships, finally the compilation problem solved.

  

Second, ffmpeg the latest version of the library compilation

FFmpeg the latest version should be 2.1, the history version is detailed in http://www.ffmpeg.org/releases/, on this site we can go down to all the historical version of the library. FFmpeg is a cross-platform in C language library, including encoding, decoding, color space conversion Library. Compiling requires a command line, which is a challenge for those of us who don't have a background or Linux development script knowledge. Fortunately, the network is so convenient now, do not ask Google, soon found a xcode5 under the next key to compile the FFmpeg library script. This script is written by a foreigner, really powerful, from downloading to compiling to build the last Fat library one go.

Script Address: https://gist.github.com/m1entus/6983547

Run this script need to rely on a library Perl write script, search the Web currently compiled FFmpeg Library Post basically will mention this script, the script address is as follows: Https://github.com/mansr/gas-preprocessor.

After downloading these two scripts, the preparation for compiling the FFmpeg library is basically complete, followed by the following steps:

1, copy the gas-preprocessor.pl file to the/usr/bin directory.

2, modify the permissions of the gas-preprocessor.pl file

Note: You need to have read, write, and execute permissions. To do this, first enter the/usr/bin directory under the command line, and then execute the chmod command, as shown in:

3. Switchbuild-ffmpeg.sh脚本的目录下,使用命令sh build-ffmpeg.sh 运行该脚本即可。

Note: 1) The name of the parent directory of the build-ffmpeg.sh script cannot include spaces, which may cause the build to fail.

2) The build-ffmpeg.sh script can be configured to compile the FFmpeg version, as well as the version using the iOS SDK, as shown in:

The default ffmpeg in this script is version 2.0, compiled with the SDK for iOS 7.0, the C language compiler uses clang, and the app can select different ffmpeg and iOS SDK versions depending on the actual project needs.

According to the above steps, there is nothing complicated in compiling the work, why do I say that I tread a lot of pits? I'll explain the problem a little bit.

Third, compile the earlier version of the FFmpeg library

The second part of our introduction of a very cool script, a key to compile, which gives us an illusion, ffmpeg compiled so! If we try to change version in the script to 0.7 try, run the script and find the compilation error. As shown in the following:

Prompt for location options--disable-iconv, as prompted us to enter./configure See all available options. Under the command line switch to the actual ffmpeg source directory, view the help such as:

We can see a lot of options, English is not difficult, is some of the options described is too concise, so when the actual use if not sure, we can ask Google.

Well, look back and see how this configure file works?

1, cutting

We know that the FFmpeg library is a very large library, including encoding, decoding and streaming media support, and so on, if you do not do a crop all compiled in, the final generated static library will be very large. In practice we may only want to use decoding (such as the player), so we can use the relevant options to specify the compile-time Disable encoding section. Of course, we can also do further cropping, such as only open some of the common format of decoding, disable the other decoding, so the compiled static library will be smaller.

To crop, we know what the parts are, using the following command to view the list of components supported by the FFmpeg library.

1234567891011 --list-decoders          show all available decoders--list-encoders          show all available encoders--list-hwaccels          show all available hardware accelerators--list-muxers            show all available muxers--list-demuxers          show all available demuxers--list-parsers           show all available parsers--list-protocols         show all available protocols--list-bsfs              show all available bitstream filters--list-indevs            show all available input devices--list-outdevs           show all available output devices--list-filters           show all available filters

We can according to the actual needs of the unused parts are disabled, so that the compilation is fast, the package will be relatively small, commonly used cropping options are as follows:

123456789101112 --disable-doc            donot build documentation--disable-ffmpeg         disable ffmpeg build--disable-ffplay         disable ffplay build--disable-ffserver       disable ffserver build--disable-network        disable network support [no]--disable-encoder=NAME   disable encoder NAME--enable-encoder=NAME    enable encoder NAME--disable-encoders       disable all encoders--disable-decoder=NAME   disable decoder NAME--enable-decoder=NAME    enable decoder NAME--disable-decoders       disable all decoders--disable-hwaccel=NAME   disable hwaccel

For example, if we need to make a local video player, we can use the following configuration:

  

Of course, you can also make finer-grained cropping based on the Help list, such as decoding only which formats are supported, and so on.

2. Specify the compilation environment

FFmpeg as a cross-platform library, different platforms, the paths of compilers on different people's computers may vary, so we need to specify the compiler's path for the compilation script. Colleagues we can also specify other compilation options, such as whether to cross-compile, target platform system, CPU architecture, and other libraries that need to depend on the path that has been specified to disable assembly optimizations.

1234567891011 --enable-cross-compile   assume a cross-compiler is used--sysroot=PATH           root of cross-build tree--sysinclude=PATH        location of cross-build system headers--target-os=OS           compiler targets OS []--cc=CC                  use C compiler CC [gcc]--extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS []--extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS []--arch=ARCH              select architecture []--cpu=CPU                select the minimum required CPU (affects                         instruction selection, may crash on older CPUs)--disable-asm            disable all assembler optimizations

Sysroot is the path to the iOS SDK, note that the path to the SDK in Iphoneos.platform is required to compile the true machine version of the library, and the library that compiles the emulator version uses the path of the SDK in Iphonesimulator.platform. Target-os fills Darwin (the kernel of the Apple system), Arch can add i386 (simulator), ARMV6,ARMV7, etc. according to the specific situation. The CPU can fill cortex-a8,cortox-a9,i386 and so on according to the specific type.

3. Specify the installation path for the static library

Specifies the location where the compiled static library and related header files are copied when the make install command is executed, that is, the path to the FFmpeg library's compiled output. Usually we just need to set the "--prefix=prefix" option. For example, we need to point the path of the last generation of the static library to "Build/armv7", then set--prefix= "Build/armv7";

   

Iv. FFmpeg0.7 Repository One-click compilation script

Through the third part of the introduction, I believe we should have a preliminary understanding of the FFmpeg configuration, We go back to the beginning of the third part of the problem we ran build-ffmpeg.sh, after looking at configure's help, we found that 0.7 this version of the FFmpeg library is no "--disable-iconv" option. This awesome script is written for the newer FFmpeg library, and there are no configuration options that are normal in the low version.

A modified script is given below, which streamlines the original script and removes the code from the download section.

build-ffmpeg0.7

Note: Because the FFmpeg library is older, the script uses xcode4.6 and the compiler is GCC, compiled with the 6.1 SDK. If the colleagues on your machine install xcode4.x and XCODE5, you can switch the current default compilation environment to xcode4.6 at the command line by using the following command:

After setting up Xcode's compilation environment, you only need to copy the script to the FFmpeg source file path to run one click to generate the armv7,armv7s,i386 and the full platform library after the composition.

V. How to use and compile links may encounter problems

In the forth part, we have modified and streamlined the build-ffmpeg.sh script, we have build-ffmpeg0.7.sh, we only need to run the script to complete the FFmpeg 0.7 repository compile work. After compiling we get the Lib directory (which contains all the generated static libraries) and the Include directory (including the corresponding header file), we only need to add these files to the project.

The problem here seems to be all solved, if smooth, congratulations, you can use directly.

If you're as unlucky as I am, you may encounter some other problems. Here are the problems and solutions I have encountered:

1, time.h repeat the problem

We know that the general static library is used with the header file, in order to use the FFmpeg library in the project, we need to add a static library in the Xcode build phases, but also need to import the library corresponding header file. There are a number of header files for the FFmpeg library, which typically uses the header search path to import headers, which has two benefits: the first avoids interfering with our engineering structure. The second can reduce the header file conflicts on certain programs.

Time.h conflict problem is a header file conflict, the System standard library has time.h file, FFmpeg should be after 1.1 also added a time.h file, the path is libavutil/time.h. So if you are using a FFmpeg1.1 version, you may encounter a problem with the header file conflict in use. To solve this problem, one way to spread the internet is to modify the name of the Time.h file in the FFmpeg library, which I think is too cumbersome and error prone. Later to view the FFmpeg source code when the occasional discovery of its own internal reference to this time.h when there is a parent directory, such as # include "Libavutil/time.h". Therefore, it is possible to solve this problem by specifying the path of the header file.

Open the Project Settings page, and search for the header, searching for path as shown:

If your FFmpeg library is placed exactly under the current path, and you set a recursive include header file for laziness, you are likely to encounter time.h conflicts. Because the Xcode project defaults to finding the user path, the time.h under Libavutil in FFmpeg will be linked first, resulting in no more link to the system time.h file, resulting in a compilation failure.

There are two ways to solve this problem:

  A. Cancel the recursive reference in the header Search paths.

b, set always Search User paths to No.

2. GCC C compiletest Error problem
XCODE5 The following compiled ffmpeg are clang, similar problems will be encountered. This problem usually occurs in the case of a configuration file error, which is usually a GCC path error and, of course, other compilation parameter error issues.

This is the problem we should first check the GCC path is correct, if the specified path is confirmed to have a GCC program, but still error, we check the current platform to compile and the specified GCC path is consistent, If you use GCC below iphoneos.platform to compile the library for the i386 platform, it will certainly not be tested.

3, C compiler test failed problem
There are different parameters that may be used to compile the i386 version of the FFmpeg library and the ARMV repository, for example, I have this problem, and one of my compilation options is as follows:

--extra-cflags= '-arch i386-mfloat-abi=softfp-miphoneos-version-min=5.0 '

When I confirm that other parameters (such as cpu,arch) are correct, we are still prompted with "C compiler test failed." Followed by a sentence to see config.log you can get more detailed information, so open the file, you can see in the beginning of your configuration statement, if the script, this will show the final explanation (replace the parameter is the real value) of the configuration statement. Then followed by a bunch of specific configurations, the usual cry error message will be at the very end of the file. The information I encountered about the problem is as follows:

  

See the area marked Red No, the prompt "-MFLOAT-ABI=SOFTFP" option is not supported, after deleting the option, the runtime configuration is passed. Other configuration issues can be obtained by looking at Config.log for more detailed error messages.

4, because the problem of not importing LIBBZ dynamic library

If you imported the FFmpeg library and configured the header file search path, you encounter "Undefined Symbols for Architecture armv7s: _bz2_bzdecompressinit" as shown in:

The problem is that importing a library solves the problem because there is no reason to import the "Libbz2.dylib" library.

5, libavcodec/audioconvert.h header file missing problem

Do not know why do make Install when the Libavcodec in the audioconvert.h did not copy to the Include directory in the Libavcodec, see found the original Libavutil directory has a audioconvert.h already. To solve this problem, you only need to copy the Audioconvert.h header file from the libavcodec of the FFmpeg library to the Libavcodec directory of the include.

Six, talk

Thanks to the "misfortune" I had encountered, if I had accepted the project using the latest version of the FFmpeg library, I might have run that awesome script directly, and then everything would have been smooth. If that were the case, I would probably not have taken the time to learn basic scripting knowledge, to understand the configuration of the FFmpeg library, and the result was that the next time I won the FFmpeg library compile link, I could do nothing.

Said so much, when we use a technology, should not just stay in the level of use, take a little time to understand the underlying principle will let you have a deeper understanding of the technology, more learning, more to see, more thinking, and eventually there will be some gains.

VII. compilation scripts and references

1. Compiling scripts

  Gas-preprocessor script address : https://github.com/mansr/gas-preprocessor   

FFmpeg 2.x One-click compilation script: https://gist.github.com/m1entus/6983547

FFmpeg0.7 One-click compilation script: https://gist.github.com/smileEvday/7565260

2. References

The method of compiling the ffmpeg of simulator and Real Machine (summary version)

Http://www.cocoachina.com/iphonedev/toolthain/2011/1020/3395.html

Compiling the FFmpeg library (RPM) used in ios4.3

Http://www.cocoachina.com/bbs/simple/?t70887.html

Installing FFmpeg iOS Libraries ARMv7, armv7s, i386 and Universal on Mac with 10.8

http://stackoverflow.com/questions/18003034/ installing-ffmpeg-ios-libraries-armv7-armv7s-i386-and-universal-on-mac-with-10/19370679#19370679

Ios:ffmpeg Compiling and using learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.