在录制的video上的iPhone水印。

在我的应用程序中,我需要捕捉video并在该video上添加水印。 水印应该是文本(时间和注释)。 我看到一个使用“QTKit”框架工作的代码。 但是我读到框架不适用于iPhone。

提前致谢。

使用AVFoundation 。 我build议用AVCaptureVideoDataOutput抓取帧,然后用水印图像覆盖捕获的帧,最后将捕获和处理的帧写入文件用户AVAssetWriter

search堆栈溢出,有很多很棒的例子,详细说明如何做我提到的这些事情。 我还没有看到任何代码的例子,你想要的效果,但你应该能够很容易地混合和匹配。

编辑:

看看这些链接:

iPhone:AVCaptureSession捕获输出崩溃(AVCaptureVideoDataOutput) – 这篇文章可能是有帮助的,只是包含相关代码的性质。

AVCaptureDataOutput将以CMSampleBufferRef的forms返回图像。 使用以下代码将它们转换为CGImageRef

  - (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer // Create a CGImageRef from sample buffer data { CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(imageBuffer,0); // Lock the image buffer uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); // Get information of the image size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); size_t width = CVPixelBufferGetWidth(imageBuffer); size_t height = CVPixelBufferGetHeight(imageBuffer); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); CGImageRef newImage = CGBitmapContextCreateImage(newContext); CGContextRelease(newContext); CGColorSpaceRelease(colorSpace); CVPixelBufferUnlockBaseAddress(imageBuffer,0); /* CVBufferRelease(imageBuffer); */ // do not call this! return newImage; } 

从那里你将转换为UIImage,

  UIImage *img = [UIImage imageWithCGImage:yourCGImage]; 

然后使用

 [img drawInRect:CGRectMake(x,y,height,width)]; 

将帧绘制到上下文中,在其上绘制水印的PNG,然后使用AVAssetWriter将处理后的图像添加到输出video。 我会build议实时添加它们,所以你没有用大量的UIImage来填充内存。

如何将UIImage数组导出为电影? – 这篇文章展示了如何在给定的时间内将您处理的UIImage添加到video中。

这应该可以让您顺利地为您的video添加水印。 记住要练习良好的内存pipe理,因为以20-30fps的速度泄漏的图像是破坏应用程序的好方法。

添加水印是相当简单的。 您只需要使用CALayer和AVVideoCompositionCoreAnimationTool。 代码可以按照相同的顺序复制和组装。 我只是试图插入一些意见,以更好的理解。

我们假设你已经录制了video,所以我们先创buildAVURLAsset:

 AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:outputFileURL 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]]; 

只用这个代码就可以导出video,但是我们想要首先添加带有水印的图层。 请注意,一些代码可能看起来多余,但是一切工作都是必须的。

首先我们用水印图像创build图层:

 UIImage *myImage = [UIImage imageNamed:@"icon.png"]; CALayer *aLayer = [CALayer layer]; aLayer.contents = (id)myImage.CGImage; aLayer.frame = CGRectMake(5, 25, 57, 57); //Needed for proper display. We are using the app icon (57x57). If you use 0,0 you will not see it aLayer.opacity = 0.65; //Feel free to alter the alpha here 

如果我们不想要一个图像,而想要文字:

 CATextLayer *titleLayer = [CATextLayer layer]; titleLayer.string = @"Text goes here"; titleLayer.font = @"Helvetica"; titleLayer.fontSize = videoSize.height / 6; //?? titleLayer.shadowOpacity = 0.5; titleLayer.alignmentMode = kCAAlignmentCenter; titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6); //You may need to adjust this for proper display 

以下代码按照正确的顺序对图层进行sorting:

 CGSize videoSize = [videoAsset 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]; [parentLayer addSublayer:titleLayer]; //ONLY IF WE ADDED TEXT 

现在我们正在创build组合并添加指令来插入图层:

 AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition videoComposition] retain]; 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 *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; videoComp.instructions = [NSArray arrayWithObject: instruction]; 

现在我们准备出口:

 _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];//AVAssetExportPresetPassthrough _assetExport.videoComposition = videoComp; NSString* videoName = @"mynewwatermarkedvideo.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; [strRecordedFilename setString: exportPath]; [_assetExport exportAsynchronouslyWithCompletionHandler: ^(void ) { [_assetExport release]; //YOUR FINALIZATION CODE HERE } ]; [audioAsset release]; [videoAsset release]; 

已经由@Julio给出的答案在objective-c的情况下工作正常这里是Swift 3.0相同的代码基础:

WATERMARK&像Instagram一样生成SQUARE或CROPPEDvideo

从文档目录获取输出文件并创buildAVURLAsset

  //output file let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let outputPath = documentsURL?.appendingPathComponent("squareVideo.mov") if FileManager.default.fileExists(atPath: (outputPath?.path)!) { do { try FileManager.default.removeItem(atPath: (outputPath?.path)!) } catch { print ("Error deleting file") } } //input file let asset = AVAsset.init(url: filePath) print (asset) let composition = AVMutableComposition.init() composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) //input clip let clipVideoTrack = asset.tracks(withMediaType: AVMediaTypeVideo)[0] 

用水印图像创build图层:

  //adding the image layer let imglogo = UIImage(named: "video_button") let watermarkLayer = CALayer() watermarkLayer.contents = imglogo?.cgImage watermarkLayer.frame = CGRect(x: 5, y: 25 ,width: 57, height: 57) watermarkLayer.opacity = 0.85 

使用Text作为水印而不是图像创build图层:

  let textLayer = CATextLayer() textLayer.string = "Nodat" textLayer.foregroundColor = UIColor.red.cgColor textLayer.font = UIFont.systemFont(ofSize: 50) textLayer.alignmentMode = kCAAlignmentCenter textLayer.bounds = CGRect(x: 5, y: 25, width: 100, height: 20) 

以下代码按照正确的顺序对图层进行sorting:

  let videoSize = clipVideoTrack.naturalSize let parentlayer = CALayer() let videoLayer = CALayer() parentlayer.frame = CGRect(x: 0, y: 0, width: videoSize.height, height: videoSize.height) videoLayer.frame = CGRect(x: 0, y: 0, width: videoSize.height, height: videoSize.height) parentlayer.addSublayer(videoLayer) parentlayer.addSublayer(watermarkLayer) parentlayer.addSublayer(textLayer) //for text layer only 

按照适当的顺序在video上添加图层以获得水印

  let videoSize = clipVideoTrack.naturalSize let parentlayer = CALayer() let videoLayer = CALayer() parentlayer.frame = CGRect(x: 0, y: 0, width: videoSize.height, height: videoSize.height) videoLayer.frame = CGRect(x: 0, y: 0, width: videoSize.height, height: videoSize.height) parentlayer.addSublayer(videoLayer) parentlayer.addSublayer(watermarkLayer) parentlayer.addSublayer(textLayer) //for text layer only 

以平方格式裁剪video – 尺寸为300 * 300

  //make it square let videoComposition = AVMutableVideoComposition() videoComposition.renderSize = CGSize(width: 300, height: 300) //change it as per your needs. videoComposition.frameDuration = CMTimeMake(1, 30) videoComposition.renderScale = 1.0 //Magic line for adding watermark to the video videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayers: [videoLayer], in: parentlayer) let instruction = AVMutableVideoCompositionInstruction() instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30)) 

旋转到肖像

 //rotate to potrait let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack) let t1 = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: -(clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2) let t2: CGAffineTransform = t1.rotated(by: .pi/2) let finalTransform: CGAffineTransform = t2 transformer.setTransform(finalTransform, at: kCMTimeZero) instruction.layerInstructions = [transformer] videoComposition.instructions = [instruction] 

导出video的最后一步

  let exporter = AVAssetExportSession.init(asset: asset, presetName: AVAssetExportPresetMediumQuality) exporter?.outputFileType = AVFileTypeQuickTimeMovie exporter?.outputURL = outputPath exporter?.videoComposition = videoComposition exporter?.exportAsynchronously() { handler -> Void in if exporter?.status == .completed { print("Export complete") DispatchQueue.main.async(execute: { completion(outputPath) }) return } else if exporter?.status == .failed { print("Export failed - \(String(describing: exporter?.error))") } completion(nil) return } 

这将导出与水印作为文本或图像的方形大小的video

谢谢

这里是swift3的例子,如何将animation(图像/幻灯片/帧数组)和静态图像水印插入录制的video。

它使用CAKeyframeAnimation为框架设置animation,并使用AVMutableCompositionTrackAVAssetExportSessionAVMutableVideoCompositionAVMutableVideoCompositionInstruction结合在一起。