1浊闪,首先我們要做視頻導(dǎo)出合并這些基本的操作
? ?這部分代碼網(wǎng)上都有基本都是比較簡單沒什么坑腻要,首先導(dǎo)入視頻地址好唯,拿到AVAsset,然后利用AVMutableComposition去配置合并就行了
NSDictionary*optDict = [NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:NO]forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVAsset*firstAsset = [[AVURLAssetalloc]initWithURL:((FetchObject*)[videoArraysobjectAtIndex:0]).fileUroptions:optDict];
AVAsset*secondAsset = [[AVURLAssetalloc]initWithURL:((FetchObject*)[videoArraysobjectAtIndex:1]).fileUroptions:optDict];
AVAsset*endedAsset;
if(videoArrays.count>=3)
{
endedAsset= [[AVURLAssetalloc]initWithURL:((FetchObject*)[videoArraysobjectAtIndex:2]).fileUroptions:optDict];
}
AVMutableComposition*composition = [AVMutableCompositioncomposition];
//為視頻類型的的Track
AVMutableCompositionTrack*compositionTrack = [compositionaddMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];
//CMTimeRangeMake 指定起去始位置
CMTimeRangefirstTimeRange =CMTimeRangeMake(kCMTimeZero, firstAsset.duration);
CMTimeRangesecondTimeRange =CMTimeRangeMake(kCMTimeZero, secondAsset.duration);
CMTimeRangeendedTimeRange=CMTimeRangeMake(kCMTimeZero, endedAsset.duration);
[compositionTrackinsertTimeRange:firstTimeRangeofTrack:[firstAssettracksWithMediaType:AVMediaTypeVideo][0]atTime:kCMTimeZeroerror:nil];
[compositionTrackinsertTimeRange:secondTimeRangeofTrack:[secondAssettracksWithMediaType:AVMediaTypeVideo][0]atTime:firstTimeRange.durationerror:nil];
if(endedAsset!=nil)
{
[compositionTrackinsertTimeRange:endedTimeRangeofTrack:[endedAssettracksWithMediaType:AVMediaTypeVideo][0]atTime:secondTimeRange.durationerror:nil];
}
//只合并視頻,導(dǎo)出后聲音會消失伙判,所以需要把聲音插入到混淆器中
//添加音頻,添加本地其他音樂也可以,與視頻一致
AVMutableCompositionTrack*audioTrack = [compositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];
if([firstAssettracksWithMediaType:AVMediaTypeAudio].count>0)
{
[audioTrackinsertTimeRange:firstTimeRangeofTrack:[firstAssettracksWithMediaType:AVMediaTypeAudio][0]atTime:kCMTimeZeroerror:nil];
}
//CMTimeRange totalTimeRange=CMTimeRangeMake(kCMTimeZero,CMTimeAdd(CMTimeAdd(secondTimeRange.duration, firstTimeRange.duration), endedTimeRange.duration));
if([secondAssettracksWithMediaType:AVMediaTypeAudio].count>0)
{
[audioTrackinsertTimeRange:secondTimeRangeofTrack:[secondAssettracksWithMediaType:AVMediaTypeAudio][0]atTime:firstAsset.durationerror:nil];
}
if(endedAsset!=nil)
{
if([endedAssettracksWithMediaType:AVMediaTypeAudio].count>0) {
[audioTrackinsertTimeRange:endedTimeRangeofTrack:[endedAssettracksWithMediaType:AVMediaTypeAudio][0]atTime:secondAsset.durationerror:nil];
}
}
拆分配置好后可以用AVAssetExportSession 導(dǎo)出
exporterSession= [[AVAssetExportSessionalloc]initWithAsset:compositionpresetName:AVAssetExportPreset1280x720];
if(videoComposition!=nil) {
exporterSession.videoComposition=videoComposition;
}
exporterSession.outputFileType=AVFileTypeQuickTimeMovie;
//混合后的視頻輸出路徑
NSString*outPutPath =MERAGVIDEOPATH;
if([[NSFileManagerdefaultManager]fileExistsAtPath:outPutPath])
{
[[NSFileManagerdefaultManager]removeItemAtPath:outPutPatherror:nil];
}
NSURL*outPutUrl = [NSURLfileURLWithPath:outPutPath];
exporterSession.outputURL= outPutUrl;//如果文件已存在尽狠,將造成導(dǎo)出失敗
exporterSession.shouldOptimizeForNetworkUse=YES;//用于互聯(lián)網(wǎng)傳輸
[exporterSessionexportAsynchronouslyWithCompletionHandler:^{
2.我們可以利用gpuimage 第三方框架做濾鏡(這里面有很多坑衔憨。。)
gpuimage 是個老外寫的濾鏡第三方庫支持圖片和視頻 https://github.com/BradLarson/GPUImage
首先第一個坑: 美工給你什么做濾鏡袄膏,最好要你們美工導(dǎo)出oringal lookup table格子圖 就是gpuimage里面有的讓你們美工按照你們需要的濾鏡調(diào)用ps導(dǎo)出
fitler=[[GPUImageFilteralloc]init];
self.filterLookup= [[GPUImageLookupFilteralloc]init];
self.lookupImageSource= [[GPUImagePicturealloc]initWithImage:[UIImageimageNamed:@"N1.png"]];
[self.lookupImageSourceaddTarget:self.filterLookupatTextureLocation:1];
[self.lookupImageSourceuseNextFrameForImageCapture];
[self.lookupImageSourceprocessImage];
fitler=self.filterLookup;
這樣子你就可以配置出一個你需要的濾鏡fitler
然后你要播放預(yù)覽加了濾鏡的視頻
我們不能用傳統(tǒng)的播放器巫财,我們需要自己寫一個播放器,需要用到avplayer 和avplayeritem,還有GPUImageMovie gpuimage的本地視頻播放
- (void)setFitlerView:(GPUImageFilter*)fitler{
_avplayer=[[AVPlayeralloc]init];
_avplayerItem=[[AVPlayerItemalloc]initWithURL:_videoUrl];
[_avplayerreplaceCurrentItemWithPlayerItem:_avplayerItem];
[selfsetFillMode:kGPUImageFillModePreserveAspectRatioAndFill];
_movieFile=[[GPUImageMoviealloc]initWithPlayerItem:_avplayerItem];
_movieFile.url=_videoUrl;
[_movieFilesetPlayAtActualSpeed:YES];
[_movieFilesetShouldRepeat:YES];
[_movieFileaddTarget:fitler];
[fitleraddTarget:self];
[_movieFilestartProcessing];
[_avplayerplay];
}
3.添加音樂哩陕,這個用原生avfoundation,
4.添加水印動畫(這里面也有很多坑平项。。)