在我的应用程序中,我需要捕获视频并在该视频上添加水印。水印应为文本(时间和注释)。我看到使用“QTKit”Frame工作的代码。但是我读到该框架不适用于iPhone。
先谢谢。
答案 0 :(得分:43)
添加水印非常简单。您只需要使用CALayer和AVVideoCompositionCoreAnimationTool。代码可以按照相同的顺序进行复制和组装。我刚刚尝试在两者之间插入一些评论以便更好地理解。
我们假设您已经录制了视频,因此我们将首先创建AVURLAsset:
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]];
只需使用此代码,您就可以导出视频,但我们希望首先添加带有水印的图层。请注意,某些代码可能看起来多余,但一切都必须工作。
首先我们使用水印图像创建图层:
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
以下代码按正确顺序对图层进行排序:
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
现在我们正在创建合成并添加插入图层的说明:
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];
答案 1 :(得分:14)
使用AVFoundation
。我建议用AVCaptureVideoDataOutput
抓取帧,然后用水印图像覆盖捕获的帧,最后将捕获和处理的帧写入文件用户AVAssetWriter
。
搜索堆栈溢出,有很多很棒的例子详细说明了如何完成我提到的每一件事。我还没有看到任何代码示例来说明你想要的效果,但你应该能够很容易地混合搭配。
编辑:
看一下这些链接:
iPhone: AVCaptureSession capture output crashing (AVCaptureVideoDataOutput) - 这篇文章可能仅仅因为包含相关代码而有所帮助。
AVCaptureDataOutput
会将图片作为CMSampleBufferRef
返回。
使用以下代码将它们转换为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
将处理后的图像添加到输出视频。我建议实时添加它们,这样你就不会用大量的UIImages来填充内存。
How do I export UIImage array as a movie? - 这篇文章展示了如何在给定的时间内将您处理过的UIImages添加到视频中。
这样可以让您在为视频添加水印方面做得很好。请记住练习良好的内存管理,因为以20-30fps的速度泄漏图像是使应用程序崩溃的好方法。
答案 2 :(得分:2)
@Julio给出的答案在objective-c的情况下工作正常 这是 Swift 3.0 :
的相同代码库WATERMARK&生成像Instagram这样的SQUARE或CROPPED视频
从Documents Directory&获取输出文件;创建AVURLAsset
//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]
使用水印图像创建图层:
//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作为水印而不是图片创建图层:
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)
以下代码按正确顺序对图层进行排序:
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
按照正确的水印顺序在视频上添加图层
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
以方格格式裁剪视频 - 尺寸为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]
导出视频的最后一步
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
}
这将以方形大小导出视频,水印为文本或图像
谢谢
答案 3 :(得分:1)
只需下载代码并使用它。它位于Apple开发人员文档页面中。
答案 4 :(得分:0)
这是the example on swift3如何将动画(图像/幻灯片/帧数组)和静态图像水印插入录制的视频中。
它使用CAKeyframeAnimation为框架设置动画,并使用 AVMutableCompositionTrack , AVAssetExportSession 和 AVMutableVideoComposition 以及 AVMutableVideoCompositionInstruction 把所有东西组合在一起。
答案 5 :(得分:0)
通过使用mikitamanko's blog中提供的快速示例代码,将CALayer添加到视频中,我做了一些小的更改以解决以下错误:
Error Domain=AVFoundationErrorDomain Code=-11841 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The video could not be composed., NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x2830559b0 {Error Domain=NSOSStatusErrorDomain Code=-17390 "(null)"}}
解决方案是在设置图层指令时使用合成的视频轨道而不是原始视频轨道,如下面的swift 5代码所示:
static func addSketchLayer(url: URL, sketchLayer: CALayer, block: @escaping (Result<URL, VideoExportError>) -> Void) {
let composition = AVMutableComposition()
let vidAsset = AVURLAsset(url: url)
let videoTrack = vidAsset.tracks(withMediaType: AVMediaType.video)[0]
let duration = vidAsset.duration
let vid_timerange = CMTimeRangeMake(start: CMTime.zero, duration: duration)
let videoRect = CGRect(origin: .zero, size: videoTrack.naturalSize)
let transformedVideoRect = videoRect.applying(videoTrack.preferredTransform)
let size = transformedVideoRect.size
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))!
try? compositionvideoTrack.insertTimeRange(vid_timerange, of: videoTrack, at: CMTime.zero)
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
let videolayer = CALayer()
videolayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
videolayer.opacity = 1.0
sketchLayer.contentsScale = 1
let parentlayer = CALayer()
parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
sketchLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
parentlayer.addSublayer(videolayer)
parentlayer.addSublayer(sketchLayer)
let layercomposition = AVMutableVideoComposition()
layercomposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
layercomposition.renderScale = 1.0
layercomposition.renderSize = CGSize(width: size.width, height: size.height)
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayers: [videolayer], in: parentlayer)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: composition.duration)
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionvideoTrack)
layerinstruction.setTransform(compositionvideoTrack.preferredTransform, at: CMTime.zero)
instruction.layerInstructions = [layerinstruction] as [AVVideoCompositionLayerInstruction]
layercomposition.instructions = [instruction] as [AVVideoCompositionInstructionProtocol]
let compositionAudioTrack:AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
let audioTracks = vidAsset.tracks(withMediaType: AVMediaType.audio)
for audioTrack in audioTracks {
try? compositionAudioTrack?.insertTimeRange(audioTrack.timeRange, of: audioTrack, at: CMTime.zero)
}
let movieDestinationUrl = URL(fileURLWithPath: NSTemporaryDirectory() + "/exported.mp4")
try? FileManager().removeItem(at: movieDestinationUrl)
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)!
assetExport.outputFileType = AVFileType.mp4
assetExport.outputURL = movieDestinationUrl
assetExport.videoComposition = layercomposition
assetExport.exportAsynchronously(completionHandler: {
switch assetExport.status {
case AVAssetExportSessionStatus.failed:
print(assetExport.error ?? "unknown error")
block(.failure(.failed))
case AVAssetExportSessionStatus.cancelled:
print(assetExport.error ?? "unknown error")
block(.failure(.canceled))
default:
block(.success(movieDestinationUrl))
}
})
}
enum VideoExportError: Error {
case failed
case canceled
}
请注意,根据AVFoundation Crash on Exporting Video With Text Layer,此代码仅在模拟器上崩溃,但在真实设备上有效
还请注意,在应用首选视频转换后会使用宽度和高度。