gpt4 book ai didi

ios - AVMutableVideoCompositionInstruction 在 AVMutableVideoComposition 中被忽略

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:43 27 4
gpt4 key购买 nike

当我使用 this tutorial 创建视频时我试着移动 AVAssetTrack* firstTrack (没有动画,所以我不能使用 CAAnimation s)使用 AVMutableVideoCompositionInstructionAVMutableVideoCompositionLayerInstruction包含一些 CGAffineTransform s,转换只是被忽略了。

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

提前致谢!

此信息可能有帮助:我在导出时使用 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 - AVMutableVideoCompositionInstruction 在 AVMutableVideoComposition 中被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20478505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com