【发布时间】:2013-12-09 18:59:18
【问题描述】:
当我使用this tutorial 创建视频并尝试使用AVMutableVideoCompositionInstruction 移动AVAssetTrack* firstTrack(没有动画,因此我不能使用CAAnimations)和包含一些AVMutableVideoCompositionLayerInstruction 的CGAffineTransforms ,转换就会被忽略。
有谁知道这个问题并知道如何解决?
提前致谢!
此信息可能会有所帮助:我在导出时使用 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*/
}];
【问题讨论】:
-
你能发布你现有的代码吗?
-
这几乎是在各种功能中拆分的代码,但我会尝试将它放在每个重要的行之后。请给我几分钟。
-
我在视频拼贴应用中有 CGAffineTransform。它工作正常。您也可以使用 CoreAnimationTool 更改 AVExportSession 的视频层位置。你能告诉我你的代码吗?
-
我在上面添加了代码
标签: ios objective-c avfoundation cgaffinetransform