foreword
This article is the second part of compiling FFmpeg for iOS
integrated into engineering
Find the corresponding file
Change the suffix of Appdelegate.m to .mm to enable the hybrid mode. command+B, if no error is reported, the compilation is successful.
found in ffmpegfftools
found in scratchconfig
to the iOS project
Solve integration error (version 4.2.3)
Add library
ffmpeg.h comments
#include "libavutil/thread.h"
ffmpeg_fliter.c comments
#include "libavresample/avresample.h"
cmdutils.c comments
#include "compat/va_copy.h"
#include "libavresample/avresample.h"
#include "libpostproc/postprocess.h"
#include "libavutil/libm.h"
#include "libavformat/network.h"
PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
ffmpeg.c comments
include "libavutil/internal.h"
#include "libavutil/libm.h"
#include "libavutil/thread.h"
#include "libavcodec/mathops.h"
#include "libavformat/os_support.h"
nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
ost->last_nb0_frames[1],
ost->last_nb0_frames[2]);
ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
ost->forced_keyframes_expr_const_values[FKF_N],
ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
ost->forced_keyframes_expr_const_values[FKF_T],
ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
res);
ffmpeg.c header import
#include "pthread.h"
ffmpeg.h added
int ffmpeg_main(int argc, char **argv);
main
Change ffmpeg.c toffmpeg_main
Optimize the problem after integration
Counter zeroing problem (the code of ffmpeg.c will access empty attributes and cause the program to crash)
The function ffmpeg.c
in find is changed to the followingffmpeg_cleanup
nb_filtergraphs=0;
nb_output_files=0;
nb_output_streams=0;
nb_input_files=0;
nb_input_streams=0;
Crash problem at the end of command execution
The first method (recommended)
- FFmpeg will execute
exit_program
the method to end the process by default, but under iOS, only one process can be started. If the process is not processed by default, the app will automatically exit after executing a command, so it needs to be processed. - In
ffmpeg.c
theffmpeg_main
function, just change all callingexit_program
functions to callingffmpeg_cleanup
functions. At this point, we can use the commands provided by FFmpeg (〃'▽'〃)
The second method
- This method requires 2 files
The function in cmdutils.c
is changed to the followingexit_program
void exit_program(int ret)
{
if (program_exit)
program_exit(ret);
//标记为转换完成
stopRuning();
//这个是结束进程的方法,让ffmpeg进行完转码以后不至于退出程序
// exit(ret);
// 所以将退出进程的方法改造为退出线程
pthread_exit(NULL);
}
run
Write OC calls
#import "FFmpegTools.h"
#import "ffmpeg.h"
@implementation FFmpegTools
///执行ffmpeg指令," "为分割标记符
+ (void)runCmd:(NSString *)commandStr completionBlock:(void(^)(int result))completionBlock {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 根据 " " 将指令分割为指令数组
NSArray *argv_array = [commandStr componentsSeparatedByString:(@" ")];
// 将OC对象转换为对应的C对象
int argc = (int)argv_array.count;
char** argv = (char**)malloc(sizeof(char*)*argc);
for(int i=0; i < argc; i++) {
argv[i] = (char*)malloc(sizeof(char)*1024);
strcpy(argv[i],[[argv_array objectAtIndex:i] UTF8String]);
}
// ffmpeg_main
// 传入指令数及指令数组,result==0表示成功
int result = ffmpeg_main(argc,argv);
NSLog(@"执行FFmpeg命令:%@,result = %d",commandStr,result);
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(result);
});
});
}
@end
run code
NSString *inputPath = [[NSBundle mainBundle] pathForResource:@"123.ts" ofType:nil];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *outputPath = [docDir stringByAppendingPathComponent:@"videoConver2.mov"];
NSString *commandStr = [NSString stringWithFormat:@"ffmpeg -i %@ -b:v 2000K -y %@", inputPath, outputPath];
NSLog(@"执行1");
[FFmpegTools runCmd:commandStr completionBlock:^(int result) {
// 0表示成功
NSLog(@"结果%d",result);
}];
NSLog(@"执行2");
Original iOS integrated FFmpeg command line - Nuggets
★The business card at the end of the article can receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.
see below!