1
我试图将字幕添加到我用应用拍摄的视频中,但我无法确定如何操作。到目前为止,我可以在一个字符串,只会增加使用此代码的整个视频:如何使用CATextLayer为视频添加字幕
CATextLayer *aLayer = [CATextLayer layer];
aLayer.frame = CGRectMake(0, 0, 320, 480);
aLayer.bounds = CGRectMake(0, 0, 320, 480);
aLayer.string = @"Text goes here";
aLayer.backgroundColor = [UIColor clearColor].CGColor;
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:urlVideo options:nil];
self.cmp = [AVMutableComposition composition];
AVMutableCompositionTrack *trackA = [self.cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[trackA insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil] ;
self.animComp = [AVMutableVideoComposition videoComposition];
self.animComp.renderSize = CGSizeMake(640, 480);
self.animComp.frameDuration = CMTimeMake(1,30);
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, 640, 480);
videoLayer.frame = CGRectMake(0, 0, 640, 480);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:aLayer];
self.animComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [asset duration]);
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:trackA];
[layerInstruction setOpacity:1.0 atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction] ;
self.animComp.instructions = [NSArray arrayWithObject:instruction];
[self exportMovie:self];
更具体地讲,我想一些字符串添加到电影的某些帧。