CABasicAnimation在生成的视频上将持续时间缩短2.5秒

时间:2017-07-14 19:41:59

标签: ios objective-c video cabasicanimation

我计算动画的持续时间,然后设置它:

[anim setDuration:dur];

animCABasicAnimationdurfloat

在日志中,我得到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];

vTrackaTrack是从AVMutableCompositionTrack获取的AVMutableComposition *comp(视频和音频)。从输入视频中获取vataat AVAssetTrack。如果视频中没有声音,则aatnil。另一段代码:

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];

我导出视频的方式。

pLayervLayerCALayer s。

因此,动画的持续时间设置为13.5秒,但在结果视频中我看到它只有11秒。

0 个答案:

没有答案