在 iOS 中为特定时间的视频添加文本

问题描述 投票:0回答:3

我正在创建一个基于视频的应用程序,我必须从本地图库中选择任何视频,并且必须使用 CATextLayer 在视频上添加文本。为此,我使用以下代码:

- (void) createWatermark:(UIImage*)image video:(NSURL*)videoURL
{
    if (videoURL == nil)
        return;

    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
    AVMutableComposition* mixComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack* compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo  preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack* clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:clipVideoTrack
                                    atTime:kCMTimeZero error:nil];

    [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

    //  create the layer with the watermark image
    CALayer* aLayer = [CALayer layer];
    aLayer.contents = (id)image.CGImage;
    aLayer.frame = CGRectMake(50, 100, image.size.width, image.size.height);
    aLayer.opacity = 0.9;

    //sorts the layer in proper order

    AVAssetTrack* videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    CGSize videoSize = [videoTrack naturalSize];
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:aLayer];


    // create text Layer
    CATextLayer* titleLayer = [CATextLayer layer];
    titleLayer.backgroundColor = [UIColor clearColor].CGColor;
    titleLayer.string = @"Dummy text";
    titleLayer.font = CFBridgingRetain(@"Helvetica");
    titleLayer.fontSize = 28;
    titleLayer.shadowOpacity = 0.5;
    titleLayer.alignmentMode = kCAAlignmentCenter;
    titleLayer.frame = CGRectMake(0, 50, videoSize.width, videoSize.height / 6);

    [parentLayer addSublayer:titleLayer];

    //create the composition and add the instructions to insert the layer:

    AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
    videoComp.renderSize = videoSize;
    videoComp.frameDuration = CMTimeMake(1, 30);
    videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

    /// instruction
    AVMutableVideoCompositionInstruction* instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
    AVAssetTrack* mixVideoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mixVideoTrack];
    instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
    videoComp.instructions = [NSArray arrayWithObject: instruction];

    // export video

    _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
    _assetExport.videoComposition = videoComp;

    NSLog (@"created exporter. supportedFileTypes: %@", _assetExport.supportedFileTypes);

    NSString* videoName = @"NewWatermarkedVideo.mov";

    NSString* exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
    NSURL* exportUrl = [NSURL fileURLWithPath:exportPath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];

    _assetExport.outputFileType = AVFileTypeQuickTimeMovie;
    _assetExport.outputURL = exportUrl;
    _assetExport.shouldOptimizeForNetworkUse = YES;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {

         //Final code here

         switch (_assetExport.status)
         {
             case AVAssetExportSessionStatusUnknown:
                 NSLog(@"Unknown");
                 break;
            case AVAssetExportSessionStatusWaiting:
                 NSLog(@"Waiting");
                 break;
             case AVAssetExportSessionStatusExporting:
                 NSLog(@"Exporting");
                 break;
             case AVAssetExportSessionStatusCompleted:
                 NSLog(@"Created new water mark image");
                 _playButton.hidden = NO;
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Failed- %@", _assetExport.error);
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Cancelled");
                 break;
            }
     }
     ];   
}

使用上面的代码,我可以在我的视频上设置文本。但现在我必须在 CATextLayer 上提供淡入/淡出动画或在视频上显示移动文本。请建议我。提前致谢!

ios objective-c core-animation calayer catextlayer
3个回答
2
投票

尝试使用核心动画框架中声明的 CAKeyframeAnimation 类。

例如:

//start from zero opacity
titleLayer.opacity = 0;

CAKeyframeAnimation *fadeInAndOut = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
fadeInAndOut.duration = 5.0; //5 seconds will be your fade in fade out animation.
fadeInAndOut.autoreverses = NO;
// from 0.0 * animDuration time to 0.2 * animDuration time your animation will animate opacity from zero to 1.0.
// from 0.2 * animDuration time to 0.8 * animDuration time your opacity will be 1.0 
// from 0.8 * animDuration time to 1.0 * animDuration time your animation will animate opacity from 1.0 opacity to zero.
fadeInAndOut.keyTimes = @[@(0.0), @(0.2), @(0.8), @(1.0)];
fadeInAndOut.values = @[@(0.0), @(1.0), @(1.0), @(0.0)];
fadeInAndOut.beginTime = 1.0;
fadeInAndOut.removedOnCompletion = NO;
fadeInAndOut.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeInAndOut forKey:nil];

我希望您能清楚地了解这一点。使用相同的方式您可以为标签位置设置动画。

更新

对于动画位置试试这个

CAKeyframeAnimation *fadeInAndOut = [CAKeyframeAnimation animationWithKeyPath:@"position"];
fadeInAndOut.duration = 5.0;
fadeInAndOut.autoreverses = NO;
fadeInAndOut.keyTimes = @[@(0.0), @(0.2), @(0.8), @(1.0)];

fadeInAndOut.values = @[[NSValue valueWithCGPoint:CGPointMake(20, 40)],
                        [NSValue valueWithCGPoint:CGPointMake(200, 40)],
                        [NSValue valueWithCGPoint:CGPointMake(200, 40)],
                        [NSValue valueWithCGPoint:CGPointMake(400, 40)]];
fadeInAndOut.beginTime = 1.0;
fadeInAndOut.removedOnCompletion = NO;
fadeInAndOut.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeInAndOut forKey:nil];

出于解释目的,我对点进行了硬编码。您可以自己计算点和动画持续时间。


0
投票

首先添加父图层:

let parentLayer = CALayer()

之后添加叠加层以添加淡入淡出动画:

let overlayLayer = CALayer()

以及用于添加淡入淡出动画的附加电话信息:

overlayLayer.addFadeAnimationWithTime(showTime: startTime, endTime: endTime)

并将其添加到父层:

parentLayer.addSublayer(overlayLayer)

带有 startTime 和 endTime 的淡入淡出动画扩展:

extension CALayer {
func addFadeAnimationWithTime(showTime:CGFloat, endTime:CGFloat) {
    let fadeInAnimation = CABasicAnimation.init(keyPath: "opacity")
    fadeInAnimation.duration = 1
    fadeInAnimation.fromValue = NSNumber(value: 0)
    fadeInAnimation.toValue = NSNumber(value: 1)
    fadeInAnimation.isRemovedOnCompletion = false
    fadeInAnimation.beginTime = CFTimeInterval(showTime)
    fadeInAnimation.fillMode = CAMediaTimingFillMode.backwards

    self.add(fadeInAnimation, forKey: "fadeIn")

    if endTime > 0 {
        let fadeOutAnimation = CABasicAnimation.init(keyPath: "opacity")
        fadeOutAnimation.duration = 1
        fadeOutAnimation.fromValue = NSNumber(value: 1)
        fadeOutAnimation.toValue = NSNumber(value: 0)
        fadeOutAnimation.isRemovedOnCompletion = false
        fadeOutAnimation.beginTime = CFTimeInterval(endTime)
        fadeOutAnimation.fillMode = CAMediaTimingFillMode.forwards

        self.add(fadeOutAnimation, forKey: "fadeOut")
    }
}

}


0
投票
private func showLayer(_ layer: CALayer, on superLayer: CALayer, from: CGFloat, to: CGFloat) {
    superLayer.addSublayer(layer)
    showLayer(layer, at: from)
    hideLayer(layer, at: to)
  }

  private func showLayer(_ layer: CALayer, at time: CGFloat) {
    if layer.opacity != 0 {
      layer.opacity = 0
    }
    let appearAnimation = CABasicAnimation(keyPath: "opacity")
    let duration = 0.0001
    appearAnimation.beginTime = Double(time)-duration
    appearAnimation.duration = duration
    appearAnimation.fromValue = 0
    appearAnimation.toValue = 1
    appearAnimation.fillMode = .forwards
    appearAnimation.isRemovedOnCompletion = false
    layer.add(appearAnimation, forKey: nil)
  }

  private func hideLayer(_ layer: CALayer, at time: CGFloat) {
    let disappearAnimation = CABasicAnimation(keyPath: "opacity")
    let duration = 0.0001
    disappearAnimation.beginTime = CFTimeInterval(time)
    disappearAnimation.duration = duration
    disappearAnimation.fromValue = 1
    disappearAnimation.toValue = 0
    disappearAnimation.fillMode = .forwards
    disappearAnimation.isRemovedOnCompletion = false
    layer.add(disappearAnimation, forKey: nil)
  }
© www.soinside.com 2019 - 2024. All rights reserved.