
首先我设置一个AVMutableComposition和AVMutableVIDeoComposition
AVMutableComposition *composition = [AVMutableComposition composition];AVMutableCompositionTrack *compositionVIDeoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];AVURLAsset *vIDeoAsset = [AVURLAsset URLAssetWithURL:url options:nil];CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero,[vIDeoAsset duration]);AVAssetTrack *clipVIDeoTrack = [[vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0];CGSize vIDeoSize = CGSizeApplyAffinetransform(clipVIDeoTrack.naturalSize,clipVIDeoTrack.preferredtransform);vIDeoSize.wIDth = fabs(vIDeoSize.wIDth);vIDeoSize.height = fabs(vIDeoSize.height);CMTime TitleDuration = CMTimeMakeWithSeconds(5,600);CMTimeRange TitleRange = CMTimeRangeMake(kCMTimeZero,TitleDuration);[compositionVIDeoTrack insertTimeRange:TitleRange ofTrack:nil atTime:kCMTimeZero error:nil];[compositionVIDeoTrack insertTimeRange:timeRange ofTrack:clipVIDeoTrack atTime:TitleDuration error:nil];compositionVIDeoTrack.preferredtransform = clipVIDeoTrack.preferredtransform;AVMutableVIDeoComposition *vIDeoComposition = [AVMutableVIDeoComposition vIDeoComposition];AVMutableVIDeoCompositionInstruction *passthroughInstruction = [AVMutableVIDeoCompositionInstruction vIDeoCompositionInstruction];passthroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,[composition duration]);AVAssetTrack *vIDeoTrack = [[composition tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0];AVMutableVIDeoCompositionLayerInstruction *passthroughLayer = [AVMutableVIDeoCompositionLayerInstruction vIDeoCompositionLayerInstructionWithAssetTrack:vIDeoTrack]; passthroughInstruction.layerInstructions = [NSArray arrayWithObject:passthroughLayer];vIDeoComposition.instructions = [NSArray arrayWithObject:passthroughInstruction]; vIDeoComposition.frameDuration = CMTimeMake(1,30); vIDeoComposition.renderSize = vIDeoSize;vIDeoComposition.renderScale = 1.0;
和一个标题的CALayer
CALayer *animationLayer = [CALayer layer];animationLayer.bounds = CGRectMake(0,vIDeoSize.wIDth,vIDeoSize.height);CATextLayer *TitleLayer = [CATextLayer layer];TitleLayer.string = [effect valueForKey:@"Title"];TitleLayer.Font = [effect valueForKey:@"Font"];TitleLayer.FontSize = 30;TitleLayer.alignmentMode = kCAAlignmentCenter;TitleLayer.bounds = CGRectMake(0,vIDeoSize.height / 6);[animationLayer addSublayer:TitleLayer];TitleLayer.anchorPoint = CGPointMake(0.5,0.5);TitleLayer.position = CGPointMake(CGRectGetMIDX(layer.bounds),CGRectGetMIDY(layer.bounds)); CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];fadeAnimation.fromValue = [NSNumber numberWithfloat:1.0];fadeAnimation.tovalue = [NSNumber numberWithfloat:0.0];fadeAnimation.additive = NO;fadeAnimation.removedOnCompletion = NO;fadeAnimation.beginTime = 3.5;fadeAnimation.duration = 1.0;fadeAnimation.fillMode = kCAFillModeBoth;[TitleLayer addAnimation:fadeAnimation forKey:nil];
最后,我将CALayer添加到AVMutableVIDeoComposition
CALayer *parentLayer = [CALayer layer];CALayer *vIDeolayer = [CALayer layer];parentLayer.bounds = CGRectMake(0,vIDeoSize.height);parentLayer.anchorPoint = CGPointMake(0,0);parentLayer.position = CGPointMake(0,0);vIDeolayer.bounds = CGRectMake(0,vIDeoSize.height);[parentLayer addSublayer:vIDeolayer];vIDeolayer.anchorPoint = CGPointMake(0.5,0.5);vIDeolayer.position = CGPointMake(CGRectGetMIDX(parentLayer.bounds),CGRectGetMIDY(parentLayer.bounds));[parentLayer addSublayer:layer]; animationLayer.anchorPoint = CGPointMake(0.5,0.5);animationLayer.position = CGPointMake(CGRectGetMIDX(parentLayer.bounds),CGRectGetMIDY(parentLayer.bounds));vIDeoComposition.animationTool = [AVVIDeoCompositionCoreAnimationTool vIDeoCompositionCoreAnimationToolWithPostProcessingAsVIDeolayer:vIDeolayer inLayer:parentLayer];
并出口!
AVAssetExportSession *exportSession = [[[AVAssetExportSession alloc] initWithAsset:composition presetname:AVAssetExportPresetMediumQuality] autorelease];exportSession.vIDeoComposition = vIDeoComposition; NSURL *segmentfileURL = //some local URLexportSession.outputfileType = @"com.apple.quicktime-movIE";exportSession.outputURL = segmentfileURL;[exportSession exportAsynchronouslyWithCompletionHandler:^{ switch ([exportSession status]) { case AVAssetExportSessionStatusFailed: Log(@"Export Failed: %@",[exportSession error]); break; case AVAssetExportSessionStatusCancelled: Log(@"Export canceled"); break; case AVAssetExportSessionStatusCompleted: Log(@"Export done"); break; }}]; 此代码在横向模式下工作,并且如果我删除行,也以纵向方式.Composition.animationTool = [AVVIDeoCompositionCoreAnimationTool vIDeoCompositionCoreAnimationToolWithPostProcessingAsVIDeolayer:vIDeolayer inLayer:parentLayer];
解决方法 尝试这样做,并添加您的其他说明.///turn vIDeo 90 degreesAVMutableVIDeoCompositionLayerInstruction *passthroughLayer = [AVMutableVIDeoCompositionLayerInstruction vIDeoCompositionLayerInstructionWithAssetTrack:clipVIDeoTrack];CGAffinetransform rotationtransform = CGAffinetransformMakeRotation(degreesToradians(90.0));CGAffinetransform rotateTranslate = CGAffinetransformTranslate(rotationtransform,320,0);[passthroughLayer settransform:rotateTranslate atTime:kCMTimeZero];
让我知道如何去!
MacMike
总结以上是内存溢出为你收集整理的ios – AVVideoCompositionCoreAnimationTool和CALayer在纵向模式?全部内容,希望文章能够帮你解决ios – AVVideoCompositionCoreAnimationTool和CALayer在纵向模式?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)