This article is from: hereFrom http://blog.sina.com.cn/s/blog_47522f7f0102vbwp.html, follow the steps to be practical. Thank!
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.
iOS configuration FFmpeg framework based on FFmpeg (ii)