FFmpeg简介
FFmpeg(全名是Fast Forward MPEG(Moving Picture Experts Group))是全球领先的多媒体框架,能够解码(decode)、编码(encode)、转码(transcode)、复用(mux)、解复用(demux)、流化(stream)、滤波(filter)和播放几乎任何类型的多媒体文件。
FFmpeg在iOS下的编译
- 下载 gas-preprocessor
打开文件夹,会看到一个gas-preprocessor.pl
文件,把gas-preprocessor.pl文件拷贝到/usr/local/bin/ 目录下,打开终端,输入命令:
chmod 777 /usr/local/bin/gas-preprocessor.pl
2.安装yasm
终端输入命令:
brew install yasm
如果已经安装过,提示如下,没有安装,等待安装完毕。
Updating Homebrew...
- 下载FFmpeg-iOS-build-script
解压后打开文件夹,里面./build-ffmpeg.sh就是一会儿我们要用的(这个文件夹千万不能删除,放好位置,以免找不到,我们一会儿需要的ffmpeg就会被编译到这个文件夹里)。cd到./build-ffmpeg.sh所在根目录
终端输入:
./build-ffmpeg.sh
注:这个脚本默认是编译FFMpeg 2.8的版本,可以通过修改 SOURCE="ffmpeg-3.3.9" 来修改你需要编译的版本,我这里选择了3.3.9版本
在这一步的时候我遇到了xcrun -sdk iphoneos clang is unable to create an executable file.
的问题,在终端输入
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
我当时安装了两个Xcode,一个Xcode8,另一个Xcode9,只要将Xcode.app替换成本机安装的Xcode就可以,然后再执行命令
./build-ffmpeg.sh
脚本会在./build-ffmpeg.sh同级thin文件下生成arm64
,armv7
,i386
,x86_64
四个文件夹,前两个是真机使用,后两个是模拟器使用。
当终端输出 Done 说明编译完成,同时多了一个
FFmpeg-iOS
的文件夹,这个文件夹就是我们要用的编译后的FFmpeg
。
FFmpeg导入
1.导入依赖:
2.设置Header Search Paths
将Header Search Paths设置成和Library Search Paths相同的路径,将
lib
换成include
3.导入头文件
#import
#import
#import
#import
#import
#import
#import
#import
command+b,编译成功!
FFmpeg测试
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIButton *Protocol;
@property (weak, nonatomic) IBOutlet UIButton *AVFormat;
@property (weak, nonatomic) IBOutlet UIButton *AVCodec;
@property (weak, nonatomic) IBOutlet UIButton *AVFilter;
@property (weak, nonatomic) IBOutlet UIButton *Configure;
@property (weak, nonatomic) IBOutlet UITextView *displayView;
@property (weak, nonatomic) UIButton *selectButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIFont * font = [UIFont systemFontOfSize:15];
self.myLabel.text = @"FFMpeg-Test";
self.myLabel.textAlignment = NSTextAlignmentCenter;
self.myLabel.font = font;
self.displayView.text = @"";
[self.Protocol setTitle:@"Protocol" forState:UIControlStateNormal];
self.Protocol.titleLabel.font = font;
[self.Protocol addTarget:self action:@selector(protocolClick:) forControlEvents:UIControlEventTouchUpInside];
[self.AVFormat setTitle:@"AVFormat" forState:UIControlStateNormal];
self.AVFormat.titleLabel.font = font;
[self.AVFormat addTarget:self action:@selector(avformatClick:) forControlEvents:UIControlEventTouchUpInside];
[self.AVCodec setTitle:@"AVCodec" forState:UIControlStateNormal];
self.AVCodec.titleLabel.font = font;
[self.AVCodec addTarget:self action:@selector(avcodecClick:) forControlEvents:UIControlEventTouchUpInside];
[self.AVFilter setTitle:@"AVFilter" forState:UIControlStateNormal];
self.AVFilter.titleLabel.font = font;
[self.AVFilter addTarget:self action:@selector(avfilterClick:) forControlEvents:UIControlEventTouchUpInside];
[self.Configure setTitle:@"Configure" forState:UIControlStateNormal];
self.Configure.titleLabel.font = font;
[self.Configure addTarget:self action:@selector(configureClick:) forControlEvents:UIControlEventTouchUpInside];
[self setSelectColor:self.Protocol];
}
- (void)protocolClick:(UIButton *)protocol {
char info[40000]={0};
av_register_all();
struct URLProtocol *pup = NULL;
//Input
struct URLProtocol **p_temp = &pup;
avio_enum_protocols((void **)p_temp, 0);
while ((*p_temp) != NULL){
sprintf(info, "%s[In ][%10s]\n", info, avio_enum_protocols((void **)p_temp, 0));
}
pup = NULL;
//Output
avio_enum_protocols((void **)p_temp, 1);
while ((*p_temp) != NULL){
sprintf(info, "%s[Out][%10s]\n", info, avio_enum_protocols((void **)p_temp, 1));
}
NSString * info_ns = [NSString stringWithFormat:@"%s", info];
self.displayView.text=info_ns;
[self setSelectColor:protocol];
}
- (void)avformatClick:(UIButton *)avformat {
char info[40000] = { 0 };
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
//Input
while(if_temp!=NULL){
sprintf(info, "%s[In ]%10s\n", info, if_temp->name);
if_temp=if_temp->next;
}
//Output
while (of_temp != NULL){
sprintf(info, "%s[Out]%10s\n", info, of_temp->name);
of_temp = of_temp->next;
}
NSString * info_ns = [NSString stringWithFormat:@"%s", info];
self.displayView.text=info_ns;
[self setSelectColor:avformat];
}
- (void)avcodecClick:(UIButton *)avcodec {
char info[40000] = { 0 };
av_register_all();
AVCodec *c_temp = av_codec_next(NULL);
while(c_temp!=NULL){
if (c_temp->decode!=NULL){
sprintf(info, "%s[Dec]", info);
}
else{
sprintf(info, "%s[Enc]", info);
}
switch (c_temp->type){
case AVMEDIA_TYPE_VIDEO:
sprintf(info, "%s[Video]", info);
break;
case AVMEDIA_TYPE_AUDIO:
sprintf(info, "%s[Audio]", info);
break;
default:
sprintf(info, "%s[Other]", info);
break;
}
sprintf(info, "%s%10s\n", info, c_temp->name);
c_temp=c_temp->next;
}
NSString * info_ns = [NSString stringWithFormat:@"%s", info];
self.displayView.text=info_ns;
[self setSelectColor:avcodec];
}
- (void)avfilterClick:(UIButton *)avfilter {
char info[40000] = { 0 };
avfilter_register_all();
AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);
while (f_temp != NULL){
sprintf(info, "%s[%10s]\n", info, f_temp->name);
break;
}
NSString * info_ns = [NSString stringWithFormat:@"%s", info];
self.displayView.text=info_ns;
[self setSelectColor:avfilter];
}
- (void)configureClick:(UIButton *)configure {
char info[10000] = { 0 };
av_register_all();
sprintf(info, "%s\n", avcodec_configuration());
NSString * info_ns = [NSString stringWithFormat:@"%s", info];
self.displayView.text=info_ns;
[self setSelectColor:configure];
}
- (void)setSelectColor:(UIButton *)button{
[self.selectButton setTitleColor:[UIColor colorWithRed:14.0/255 green:132.0/255 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
self.selectButton = button;
[self.selectButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
@end
看到如图所示输出,证明FFmpeg导入成功。
FFmpeg在iOS开发中编译并使用
最简单的基于FFmpeg的移动端例子