2014-02-10 12 views
5

我發現了幾個示例,說明如何在視頻上添加文字疊加層。將具有AVMutableVideoComposition的文字疊加到特定的時間範圍

雷的教程 - http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

這SO回答 - How can I add a watermark in a captured video on iOS

有報道說,我引用以及其他幾個人。我的代碼看起來幾乎與該答案相同,我一直在試圖調整它以使文本疊加在視頻的開頭只顯示一兩秒鐘。任何幫助我如何才能做到這一點?

這是我的代碼。這樣做的目的是在整個持續時間內顯示帶有覆蓋層的視頻。

if(vA) { 
    videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [videoCompositionTrack insertTimeRange:videoTimerange ofTrack:[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:&error]; 
    [videoCompositionTrack setPreferredTransform:[[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 
    if(error) 
     NSLog(@"%@", error); 
} 

if(aA) { 
    audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [audioCompositionTrack insertTimeRange:audioTimerange ofTrack:[[aA tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error]; 
    if(error) 
     NSLog(@"%@", error); 
} 

CGSize videoSize = [videoCompositionTrack naturalSize]; 

UIImage *myImage = [UIImage imageNamed:@"cover.png"]; 
CALayer *aLayer = [CALayer layer]; 
aLayer.contents = (id)myImage.CGImage; 
aLayer.frame = CGRectMake(5, 25, 100, 56); 
aLayer.opacity = 0.7; 

CATextLayer *titleLayer = [CATextLayer layer]; 
titleLayer.string = titleText; 
titleLayer.fontSize = 18; 
titleLayer.foregroundColor = titleColor.CGColor; 
titleLayer.alignmentMode = kCAAlignmentCenter; 
titleLayer.frame = CGRectMake(20, 10, videoSize.width - 40, 20); 
[titleLayer displayIfNeeded]; 

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]; 
if(titleText && [titleText length] > 0) { 
    [parentLayer addSublayer:titleLayer]; 
} 

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

AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
[instruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [mixComposition duration])]; 
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; 
videoComp.instructions = [NSArray arrayWithObject:instruction]; 

AVAssetExportSession *_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality]; 
_assetExport.videoComposition = videoComp; 
_assetExport.outputFileType = AVFileTypeMPEG4; 
_assetExport.outputURL = outputFileUrl; 

[_assetExport exportAsynchronouslyWithCompletionHandler:^{ 
    AVAssetExportSessionStatus status = [_assetExport status]; 
    switch (status) { 
     case AVAssetExportSessionStatusFailed: 
      NSLog(@"Export Failed"); 
      NSLog(@"Export Error: %@", [_assetExport.error localizedDescription]); 
      NSLog(@"Export Error Reason: %@", [_assetExport.error localizedFailureReason]); 
      break; 
     case AVAssetExportSessionStatusCompleted: 
      NSLog(@"Export Completed"); 
      [self performSelectorOnMainThread:@selector(updateProgressIndicator:) withObject:[NSNumber numberWithFloat:2] waitUntilDone:YES]; 
      break; 
     case AVAssetExportSessionStatusUnknown: 
      NSLog(@"Export Unknown"); 
      break; 
     case AVAssetExportSessionStatusExporting: 
      NSLog(@"Export Exporting"); 
      break; 
     case AVAssetExportSessionStatusWaiting: 
      NSLog(@"Export Waiting"); 
      break; 
    } 
}]; 

回答

6

我想通了什麼,我需要做的。這真的沒什麼特別的。它只是需要更好地理解什麼是可能的。

基本上我所要做的只是添加一個基本的不透明度動畫到文本中的圖層。

// My original code for creating the text layer 
CATextLayer *titleLayer = [CATextLayer layer]; 
. 
. 
. 
[titleLayer displayIfNeeded]; 

// the code for the opacity animation which then removes the text 
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
[animation setDuration:0]; 
[animation setFromValue:[NSNumber numberWithFloat:1.0]]; 
[animation setToValue:[NSNumber numberWithFloat:0.0]]; 
[animation setBeginTime:1]; 
[animation setRemovedOnCompletion:NO]; 
[animation setFillMode:kCAFillModeForwards]; 
[titleLayer addAnimation:animation forKey:@"animateOpacity"]; 
+0

你不能直接在titleLayer上設置alpha值嗎?你爲什麼需要使用CABasicAnimation? – Crashalot

+1

@scott F如何提供特定的時間範圍? –

+0

@PayalManiyar,通過設置動畫開始時間和持續時間 –

相關問題