- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我使用 this tutorial 创建视频时我试着移动 AVAssetTrack* firstTrack
(没有动画,所以我不能使用 CAAnimation
s)使用 AVMutableVideoCompositionInstruction
用AVMutableVideoCompositionLayerInstruction
包含一些 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/
我设置了backgroundColor它工作正常,但我无法设置背景图像。 AVMutableVideoCompositionInstruction * mainInstruction = [AVMut
我正在使用 AVAssetExportSession 将多个视频记录连接成一个条目。 由于用户可以在录制期间在前置和后置摄像头之间切换,因此录制的方向会切换,如果录制包含一个使用前向摄像头的剪辑和第二
当我使用 this tutorial 创建视频时我试着移动 AVAssetTrack* firstTrack (没有动画,所以我不能使用 CAAnimation s)使用 AVMutableVideo
我是一名优秀的程序员,十分优秀!