Current location - Health Preservation Learning Network - Healthy weight loss - How to call ffmpeg command with code under ios
How to call ffmpeg command with code under ios
Ffmpeg is a multi-platform multimedia processing tool, which is very powerful in processing video and audio. At present, the information about using FFMPEG on iOS found on the Internet is relatively old, but the update iteration of FFMPEG is faster. Moreover, the online explanation is not detailed enough, which is really not good for beginners (such as me) who have just come into contact with FFMPEG. In order to prevent forgetting, let's summarize the methods of using FFMPEG under iOS.

A simple understanding of 1 FFMPEG hierarchy

To use FFMPEG, we first need to understand the code structure of FFMPEG. According to Zhige's suggestion, the code of ffmpeg consists of two parts, one is a library and the other is a tool. Apis are all in the library. If you directly adjust the api to operate the video, you need to write C or c++. The other part is tool, which uses the command line and does not need to code itself to realize the video operation process. In fact, tool just converts the command line into an api operation.

2. preheating-using ffmpeg under mac os

Using ffmpeg under mac os is relatively simple, just use the command line to operate directly. First, install ffmpeg. Brew has been installed in the default system here, and you just need to enter:

Brew installs ffmpeg

Please wait for the installation to complete.

After the installation is complete, try the following command:

FFMPEG-I input.mp4 output

If the conversion is successful, the installation is successful.

3. Compile the FFMPEG library that can be used under iOS.

This step is to compile the library mentioned in 1. After the compilation, you can call the api of FFMPEG. There are some methods on the internet, but it is a bit complicated and outdated to compile them manually. According to the requirements of app store, the compiled package must also support arm64. I found a script that can be compiled with one click in the omnipotent github. The address is as follows:

/kewlbear/FFmpeg-iOS-build-script

Moreover, the crooked nut that wrote this script is very beautiful and updated in time, and it has been updated to the latest version 2.5.3. Downloaded, there is only one build-ffmpeg.sh script file. Go to the directory of the script in the terminal and execute the command:

. /build-ffmpeg.sh

The script will automatically download the ffmpeg source code from github to the local area and start compiling.

After compilation, the file directory is as follows:

Among them, ffmpeg-2.5.3 is the source code, FFmpeg-iOS is the compilation library, which contains. A static library we need, and it is seven in one * * *.

Execute the command:

lipo -info libavcodec.a

Look at the supported architectures. A package. These packages all support the architecture of armv7 armv7s i386 x86 _ 64 arm64. This script is really the conscience of the industry ~ ~ ~

4. Introduce FFMPEG library into xcode.

Create a new project, drag the FFmpeg-iOS compiled above to the xcode project, and add a header file reference.

# contains "avformat.h"

Add api statement:

av _ register _ all();

Add an empty class and set the. M suffix of executable file. Well, start mixed editing mode.

Add corresponding frameworks, including avfoundation and coremedia.

Run the project. If there are no errors, the compilation is successful.

5. Use the command line in the xcode project.

By step 4, you can already use the library. However, if you want to operate the video, you still need to manually write a lot of code to call the api, which is a lot of work, so it is naturally not as convenient as writing the command line directly. In order to use the command line in xcode project, you need to complete the following work:

(1) Add tools to the source code. Specific documents include:

(2) Add the title search path.

Search the header search path in target-build settings, and add the paths of source code ffmpeg-2.5.3 and scratch under the header search path.

(3) modify the source codes of ffmpeg.h and ffmpeg.c.

If you run this project at this time, you will get an error, because there are two main functions in the project, and the handling method at this time is:

Add a function declaration in ffmpeg.h:

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

Find the main function in ffmpeg.c and change it to ffmpeg_main.

(4) Call the command line example

Add header file: #import "ffmpeg.h "

Call command line

int number of args = 16;

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

arguments[0]= " ffmpeg ";

arguments[ 1]= "-I ";

Arguments[2] = input path;

Parameter [3] = "-ss";

Parameter [4] = "0";

arguments[5]= "-t ";

arguments[6]= duration char;

arguments[7]= "-vcodec ";

arguments[8]= " copy ";

arguments[9]= "-acodec ";

arguments[ 10]= " AAC ";

arguments[ 1 1]= "-strict ";

arguments[ 12]= "-2 ";

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

arguments[ 14]= " 32k ";

arguments[ 15]= output path;

int result = ffmpeg _ main(numberOfArgs,arguments);

Where inputpath and outputpath are file paths. After testing, these two paths do not support asset-library:// and file:// protocols, so if you want to use album files, my current solution is to copy them into the sandbox.

6. Change the closing process to close the thread

If the fifth step goes well, you can use the command line to process the video in the application, but there will be a problem and the application will exit. After a small reminder, he found that he would quit the process after the command line was executed. However, only one process can be started under iOS, so it is necessary to change the closing process to closing the thread, or directly note the method of closing the process.

As you can see in ffmpeg.c, the method of executing the exit process is exit_program, and the exit method of C language is executed in cmdutils.c Here, I changed it to pthread_exit (the #include header file needs to be added). When used in xcode project, you can use NSThread to open a new thread, and then close the thread after execution. Then use notifications from NSThreadWillExitNotification to monitor thread exits.

7. fix a bug in ffmpeg.c

In the actual project, it may be necessary to call the command line many times, but in the process of calling the command line many times, it is found that the code of ffmpeg.c will access the null attribute, which will cause the program to crash. After debugging step by step, it is found that many pointers are cleared, but their counts are not. I don't know if it's ffmpeg.c's bug fixing method is as follows: Under ffmpeg_cleanup method, all counters are set to zero, including:

Nb _ filter diagram

Nb _ output _ file

Nb _ output _ stream

Nb _ input _ file

Nb _ input _ stream

After returning to zero, it is normal to reuse the ffmpeg_main method.