美文网首页GPUImage
iOS 视频合并添加音乐添加滤镜添加水印动画并导出的详解

iOS 视频合并添加音乐添加滤镜添加水印动画并导出的详解

作者: 木_风 | 来源:发表于2017-08-28 10:31 被阅读293次

    1,首先我们要做视频导出合并这些基本的操作

       这部分代码网上都有基本都是比较简单没什么坑,首先导入视频地址,拿到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];

    }

    //只合并视频,导出后声音会消失,所以需要把声音插入到混淆器中

    //添加音频,添加本地其他音乐也可以,与视频一致

    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 导出

    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;//如果文件已存在,将造成导出失败

    exporterSession.shouldOptimizeForNetworkUse=YES;//用于互联网传输

    [exporterSessionexportAsynchronouslyWithCompletionHandler:^{

    2.我们可以利用gpuimage 第三方框架做滤镜(这里面有很多坑。。)

    gpuimage 是个老外写的滤镜第三方库支持图片和视频 https://github.com/BradLarson/GPUImage

    首先第一个坑: 美工给你什么做滤镜,最好要你们美工导出oringal lookup table格子图 就是gpuimage里面有的让你们美工按照你们需要的滤镜调用ps导出

    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

    然后你要播放预览加了滤镜的视频

    我们不能用传统的播放器,我们需要自己写一个播放器,需要用到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.添加水印动画(这里面也有很多坑。。)

    相关文章

      网友评论

        本文标题:iOS 视频合并添加音乐添加滤镜添加水印动画并导出的详解

        本文链接:https://www.haomeiwen.com/subject/eipwdxtx.html