最近因爲項目須要,接觸了一下ffmpeg的編譯和使用。git
因爲以前的版本ffmpeg編譯的庫比較老,對新設備,5s及5s之後的設備支持不太好,從新編譯了ffmpeg靜態庫。github
一,下載並在終端中運行腳本編譯ffmpegasync
腳本參考git上的:https://github.com/kewlbear/FFmpeg-iOS-build-script;ide
終端進入剛剛下載後的腳本文件夾下,運行sh:build-ffmpeg.sh 自動編譯,有缺乏yasm的按照提示操做,安裝yasmui
編譯的是ffmpeg2.5.3版本,Xcode6下iOS8.1。spa
按照腳本編譯完後的靜態庫目錄以下:線程
其中的.a文件爲靜態庫文件,include文件夾內的是頭文件3d
二,將編譯好的ffmpeg文件拖人工程,並設置相應的路徑code
新建工程,將編譯好後包含include和lib文件夾拖進工程orm
我這裏先將FFmpeg-iOS文件夾copy了一分放在工程目錄下,並從新命名爲ffmpegNew,路徑以下圖:
到這裏要修改工程的Header Search Paths ,要否則會報
include「libavformat/avformat.h」 file not found 錯誤
根據Library Search Paths 中的lib的路徑:
複製路徑,添加一份到Header Search Paths 中,再將lib改成include
改好以下:
三,導入其餘庫文件
其中libz.dylib libbz2.dylib libiconv.dylib 貌似是必需要導入的,其餘的按照需求配置
我的配置好後的以下供參考:
四,將第三方代碼導入工程
根據工程的定製化需求,這裏選擇了iFrameExtractor,git代碼參考:https://github.com/lajos/iFrameExtractor 或者 RTSPPlayer https://github.com/SutanKasturi/RTSPPlayer
我這裏用的後者的demo裏面的代碼,直接將(AudioStreamer RTSPPlayer Utilities)六個文件拖入工程使用
五,實現播放,實現方法能夠參考demo中的代碼
其中的self.playUrl爲視頻流的地址本工程用的是RTSP 數據流 示例:
self.playUrl = @"rtsp://xxx.xxx.xxx.xxx/xxx.sdp";
實現播放的代碼:
self.videoView = [[RTSPPlayer alloc] initWithVideo:self.playUrl usesTcp:YES];
self.videoView.outputHeight = self.playImage.frame.size.height;
self.videoView.outputWidth = self.playImage.frame.size.width;
__weak TestViewController *weakself = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakself.playTimer = [NSTimer scheduledTimerWithTimeInterval:1/30.0
target:weakself
selector:@selector(displayNextFrame:)
userInfo:nil
repeats:YES];
});
-(void)displayNextFrame:(NSTimer *)timer {
if (![self.videoView stepFrame]) {
[timer invalidate];
return;
}
if (startframecount < 48) {
startframecount++;
} else {
startframecount++;
[self playVideo];
}
}
-(void)playVideo
{
// NSLog(@"%p,%d",__FUNCTION__,__LINE__);
//主線程更改視圖
//視頻源尺寸爲352*288
__weak TestViewController *weakself = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakself.playImage.image = weakself.videoView.currentImage;
// NSLog(@"%d,%d",self.videoView.sourceWidth,self.videoView.sourceHeight);
});
}