我计算动画的持续时间,然后设置它:
[anim setDuration:dur];
anim
为CABasicAnimation
,dur
为float
。
在日志中,我得到dur
&{39},anim.duration
的持续时间等于13.5秒。在结果视频中它只有11秒。可能是什么问题?
更新抱歉,我忘记了CABasicAnimation
不仅可以在AVComposition
中使用,也可以在UIView
中使用。所以我在AVMutableComposition
中使用它来移动CATextLayer
。提出更多代码:
CATextLayer* titleLayer = [CATextLayer layer];
titleLayer.string = object;
titleLayer.font = (__bridge CFTypeRef) ps.fontName;
titleLayer.fontSize = ps.fontSize;
titleLayer.opacity = ps.textOpacity;
titleLayer.foregroundColor = ps.fontColor.CGColor;
titleLayer.alignmentMode = kCAAlignmentCenter;
//titleLayer.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor; //this line was used to check adjustBoundsToFit function (see below), bounds are correct
titleLayer.frame = CGRectMake(10, 10, 10, 10);
[titleLayer adjustBoundsToFit]; //this function just resizes layer so that text inside it fills the layer (this is from Category)
stripY = ((vSize.height - titleLayer.frame.size.height - margin * 2) * ps.margin + margin);
stripHeight = titleLayer.frame.size.height;
titleLayer.frame = CGRectMake(vSize.width, stripY, titleLayer.frame.size.width, titleLayer.frame.size.height);
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setFromValue:[titleLayer valueForKey:@"position"]];
CGPoint toV = titleLayer.position;
toV.x = -titleLayer.frame.size.width;
[anim setToValue:[NSValue valueWithCGPoint:toV]];
float dur = 13.5f; // Its not constant, instead its being calculated, but in logs I see 13.5.
NSLog(@"dur: %f", dur);
[anim setDuration:dur];
anim.beginTime = AVCoreAnimationBeginTimeAtZero + beginTime;
titleLayer.position = toV;
[titleLayer addAnimation:anim forKey:@"position"];
[titleLayer displayIfNeeded];
[pLayer addSublayer:titleLayer];
我在做什么是在视频前面应用移动文字。之后我这样做:
[vTrack insertTimeRange:tr ofTrack:vat atTime:kCMTimeZero error:nil];
[vTrack setPreferredTransform:[vat preferredTransform]];
if (aat != nil)
[aTrack insertTimeRange:tr ofTrack:aat atTime:kCMTimeZero error:nil];
vTrack
和aTrack
是从AVMutableCompositionTrack
获取的AVMutableComposition *comp
(视频和音频)。从输入视频中获取vat
和aat
AVAssetTrack
。如果视频中没有声音,则aat
为nil
。另一段代码:
AVMutableVideoComposition* vComp = [AVMutableVideoComposition videoComposition];
vComp.renderSize = vSize;
vComp.frameDuration = CMTimeMake(1, (int32_t) ps.fps);
vComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:vLayer inLayer:pLayer];
AVAssetTrack *videoTrack = [[comp tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
[instruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [comp duration])];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
vComp.instructions = [NSArray arrayWithObject:instruction];
NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"vtvideo-%d.mov", arc4random() % 1000]]];
self.exportSession = [[AVAssetExportSession alloc] initWithAsset:comp presetName:AVAssetExportPresetHighestQuality];
AVAssetExportSession *assetExport = self.exportSession;
assetExport.videoComposition = vComp;
assetExport.outputFileType = AVFileTypeQuickTimeMovie;
assetExport.outputURL = url;
[assetExport exportAsynchronouslyWithCompletionHandler:^{
__block PHObjectPlaceholder *placeholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
placeholder = [createAssetRequest placeholderForCreatedAsset];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Success"
message:@"Saved to gallery"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self performSegueWithIdentifier:@"backToStart" sender:self];
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Error" message:[NSString stringWithFormat:@"%@", error] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}];
self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateExportDisplay) userInfo:nil repeats:YES];
我导出视频的方式。
pLayer
和vLayer
是CALayer
s。
因此,动画的持续时间设置为13.5秒,但在结果视频中我看到它只有11秒。