2013-04-10 15 views
24

私はこれをどのように達成できるか教えてもらえませんか?ソリューションのカップルを考えている場合:iOSでキャプチャしたビデオにウォーターマークを追加するには

  1. が撮影した映像から個々の画像を作成し、後で画像ごとにそれらをマージし、その後に新しいAVAssetを作成...少し複雑に聞こえるあなたは思いませんか?

  2. 2つのビデオをマージします.1つは透かし(透かしを含む透かし)で、もう1つはカメラで取り込まれたアセットです。

答えて

15

このコードは、ビデオにテキストまたは文字列を追加し、ビデオを保存した後、どのプレーヤーでも再生されます。 このコードの最大の利点は、ビデオにサウンドを提供することです。そして、すべてのものが1つのコード(テキストとイメージ)にあります。

#import <AVFoundation/AVFoundation.h> 

-(void)MixVideoWithText 
{ 
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil]; 
    AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
    AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    AVAssetTrack *clipAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    //If you need audio as well add the Asset Track for audio here 

    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil]; 
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 

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

    CGSize sizeOfVideo=[videoAsset naturalSize]; 

    //TextLayer defines the text they want to add in Video 
    //Text of watermark 
    CATextLayer *textOfvideo=[[CATextLayer alloc] init]; 
    textOfvideo.string=[NSString stringWithFormat:@"%@",text];//text is shows the text that you want add in video. 
    [textOfvideo setFont:(__bridge CFTypeRef)([UIFont fontWithName:[NSString stringWithFormat:@"%@",fontUsed] size:13])];//fontUsed is the name of font 
    [textOfvideo setFrame:CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height/6)]; 
    [textOfvideo setAlignmentMode:kCAAlignmentCenter]; 
    [textOfvideo setForegroundColor:[selectedColour CGColor]]; 

    //Image of watermark 
    UIImage *myImage=[UIImage imageNamed:@"one.png"]; 
    CALayer layerCa = [CALayer layer]; 
    layerCa.contents = (id)myImage.CGImage; 
    layerCa.frame = CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
    layerCa.opacity = 1.0; 

    CALayer *optionalLayer=[CALayer layer]; 
    [optionalL addSublayer:textOfvideo]; 
    optionalL.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
    [optionalL setMasksToBounds:YES]; 

    CALayer *parentLayer=[CALayer layer]; 
    CALayer *videoLayer=[CALayer layer]; 
    parentLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
    videoLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
    [parentLayer addSublayer:videoLayer]; 
    [parentLayer addSublayer:optionalLayer]; 
    [parentLayer addSublayer:layerCa]; 

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

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

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"]; 
    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/utput_%@.mov", [dateFormatter stringFromDate:[NSDate date]]]; 

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality]; 
    exportSession.videoComposition=videoComposition; 

    exportSession.outputURL = [NSURL fileURLWithPath:destinationPath]; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
    switch (exportSession.status) 
    { 
     case AVAssetExportSessionStatusCompleted: 
      NSLog(@"Export OK"); 
      if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(destinationPath)) { 
       UISaveVideoAtPathToSavedPhotosAlbum(destinationPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); 
      } 
      break; 
     case AVAssetExportSessionStatusFailed: 
      NSLog (@"AVAssetExportSessionStatusFailed: %@", exportSession.error); 
      break; 
     case AVAssetExportSessionStatusCancelled: 
      NSLog(@"Export Cancelled"); 
      break; 
    } 
    }]; 
} 

ビデオの保存後に発生するエラーを表示します。

-(void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo 
{ 
    if(error) 
     NSLog(@"Finished saving video with error: %@", error); 
} 
+2

「videoComposition.frameDuration = CMTimeMake(1,10);」の行に注目してください。おそらく "videoComposition.frameDuration = CMTimeMake(1、30);"でなければなりません。デフォルトのフレームレートは30です。 – etayluz

+0

CATextLayerのフレームを正しく設定する方法を教えてください。私の質問を参照してくださいhttp://stackoverflow.com/questions/31780060/how-to-set-catextlayer-in-video-according-to-frame –

+2

これは、ビデオを間違った方向に保存します..そのための任意の修正? – RJiryes

関連する問題