的AVMutableVideoCompositionInstru

的AVMutableVideoCompositionInstru

当我使用this tutorial创建视频时,我尝试使用AVAssetTrack* firstTrack和包含一些CAAnimationAVMutableVideoCompositionInstruction来移动AVMutableVideoCompositionLayerInstruction(不使用动画,因此无法使用CGAffineTransform),则转换将被忽略。

有谁知道这个问题并知道如何解决?

提前致谢!

此信息可能会有所帮助:导出时,我将AVAssetExportPreset1280x720用作预设。因此,我认为this(可能是错误的)不是问题。

编辑:代码被拆分为各种功能,但我试图将所有内容汇总为一个代码。希望我不会忘记任何东西或弄乱某些东西。

// The layer I want to move
CALayer* layerToMove = nil;
// A layer containing just the black background
CALayer* backgroundLayer = nil;
// The videos duration
double duration = 12.0;

AVMutableComposition *exportAsset = [[AVMutableComposition alloc] init];
[exportAsset setNaturalSize:layerToMove.frame.size];

AVMutableCompositionTrack* dstTrack = [self addBlackVideoTrackOfDuration:duration toComposition:exportAsset];

AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, exportAsset.duration);

CGAffineTransform Scale = CGAffineTransformMakeScale(0.1f,0.1f);
CGAffineTransform Move = CGAffineTransformMakeTranslation(230,230);
AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:dstTrack];

[FirstlayerInstruction setTransform:CGAffineTransformConcat(Scale,Move) atTime:kCMTimeZero];
[MainInstruction setLayerInstructions:@[FirstlayerInstruction]];

CALayer* compositionLayer = [CALayer layer];

compositionLayer.bounds = layerToMove.frame;
compositionLayer.anchorPoint = CGPointMake(0, 0);
compositionLayer.position = CGPointMake(0, 0);

[compositionLayer setFrame:layerToMove.frame];
[compositionLayer setBounds:layerToMove.frame];

[compositionLayer addSublayer:backgroundLayer];
[compositionLayer addSublayer:layerToMove];

AVVideoCompositionCoreAnimationTool *animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:layerToMove inLayer:compositionLayer];

CMTime frameDuration = CMTimeMakeWithSeconds(1.0 / 25,
                                             1000);

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
[videoComposition setFrameDuration:frameDuration];
[videoComposition setInstructions:@[MainInstruction]];
[videoComposition setAnimationTool:animationTool];
[videoComposition setRenderSize:layerToMove.frame.size];
[videoComposition setRenderScale:1.0];

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:exportAsset presetName:AVAssetExportPreset1280x720];

[exportSession setOutputFileType:AVFileTypeQuickTimeMovie];
[exportSession setOutputURL:nil/*Any url*/];
[exportSession setVideoComposition:videoComposition];
// The export is complete
[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     /*Whatever*/
 }];

最佳答案

我发现,每次都能使转换正常工作的唯一方法是使用AVAssetTrack对象中的preferredTransform作为起点,然后使用该路径连接所需的转换:

AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

[mutableCompositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.duration) ofTrack:track atTime:kCMTimeZero error:nil];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:track];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

CGAffineTransform transform = track.preferredTransform;

CGAffineTransform scale = CGAffineTransformMakeScale(0.1f,0.1f);
transform = CGAffineTransformConcat(transform, scale);
[layerInstruction setTransform:transform atTime:kCMTimeZero];

我有一些代码可以在视频中间重新定位图像。这似乎对我有用。我将持续时间设置为0.01,并立即进行动画处理。模拟器中有一个错误,使其看起来好像动画不起作用(有时)。如果您在设备上运行它,它将起作用:
//animate from the start to end bounds
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.fromValue = [NSValue valueWithCGRect:layerToMove.bounds];
animation.toValue = [NSValue valueWithCGRect:CGRectOffset(layerToMove.bounds, 100, 100)]; //change to the desired new position
animation.duration = 0.01;
animation.beginTime = 1e-100;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

//animate from the start to end center points.
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"position"];
animation2.fromValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(layerToMove.bounds), CGRectGetMidY(layerToMove.bounds))];
animation2.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(layerToMove.bounds) + 50, CGRectGetMidY(layerToMove.bounds) + 50)];
animation2.duration = 0.01;
animation2.beginTime = 1e-100;
animation2.removedOnCompletion = NO;
animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

//Create an animation group
CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
group.duration = 0.01;
group.beginTime = 2.0;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards; //causes the values that were animated to remain at their end values
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
group.animations = @[animation, animation2];

//Add the animation group to the layer
[layerToMove addAnimation:group forKey:@"reposition"];

关于ios - AVMutableVideoComposition中的AVMutableVideoCompositionInstruction指令将被忽略,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20478505/

10-11 19:48