iPhone 录制的视频上有水印。
在我的应用程序中,我需要捕获视频并在该视频上添加水印。水印应该是文本(时间和注释)。我看到一个使用“QTKit”框架的代码。不过我读到该框架不适用于 iPhone。
提前致谢。
In my Application I need to capture a video and Put a watermark on that video. The watermark should be Text(Time and Notes). I saw a code using "QTKit" Frame work. However I read that the framework is not available for iPhone.
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
添加水印更加简单。您只需要使用 CALayer 和 AVVideoCompositionCoreAnimationTool。代码可以按照相同的顺序复制和组装。我只是尝试在两者之间插入一些评论以便更好地理解。
假设您已经录制了视频,因此我们将首先创建 AVURLAsset:
仅使用此代码,您就可以导出视频,但我们希望首先添加带有水印的图层。请注意,有些代码可能看起来多余,但它是一切正常工作所必需的。
首先,我们创建带有水印图像的图层:
如果我们不需要图像而想要文本:
以下代码按正确的顺序对图层进行排序:
现在我们正在创建合成并添加插入图层的指令:
现在我们准备出口:
Adding a watermark is quite more simple. You just need to use a CALayer and AVVideoCompositionCoreAnimationTool. The code can be just copied and assembled in the same order. I have just tried to insert some comments in between for better understanding.
Let's assume you recorded the video already so we are going to create the AVURLAsset first:
With just this code you would be able to export the video but we want to add the layer with the watermark first. Please note that some code may seem redundant but it is necessary for everything to work.
First we create the layer with the watermark image:
If we don't want an image and want text instead:
The following code sorts the layer in proper order:
Now we are creating the composition and add the instructions to insert the layer:
And now we are ready to export:
使用
AVFoundation
。我建议使用AVCaptureVideoDataOutput
抓取帧,然后将捕获的帧与水印图像叠加,最后将捕获和处理的帧写入文件用户AVAssetWriter
。搜索堆栈溢出,有大量精彩的示例详细说明了如何执行我提到的每件事。我还没有看到任何提供代码示例来实现您想要的效果,但您应该能够非常轻松地混合和匹配。
编辑:
看看这些链接:
iPhone:AVCaptureSession捕获输出崩溃(AVCaptureVideoDataOutput) - 这篇文章可能会因为包含相关代码而有所帮助。
AVCaptureDataOutput
将返回图像作为CMSampleBufferRef
。使用以下代码将它们转换为 CGImageRef :
从那里您将转换为 UIImage,
然后使用
将框架绘制到上下文,在其上绘制水印的 PNG,然后添加处理后的图像使用
AVAssetWriter
到您的输出视频。我建议实时添加它们,这样你就不会用大量的 UIImage 填满内存。如何将 UIImage 数组导出为影片? - 这篇文章展示了如何将已处理的 UIImage 添加到给定持续时间的视频中。
这应该可以帮助您顺利地为视频添加水印。请记住实行良好的内存管理,因为以 20-30fps 传入的图像泄漏是导致应用程序崩溃的好方法。
Use
AVFoundation
. I would suggest grabbing frames withAVCaptureVideoDataOutput
, then overlaying the captured frame with the watermark image, and finally writing captured and processed frames to a file userAVAssetWriter
.Search around stack overflow, there are a ton of fantastic examples detailing how to do each of these things I have mentioned. I haven't seen any that give code examples for exactly the effect you would like, but you should be able to mix and match pretty easily.
EDIT:
Take a look at these links:
iPhone: AVCaptureSession capture output crashing (AVCaptureVideoDataOutput) - this post might be helpful just by nature of containing relevant code.
AVCaptureDataOutput
will return images asCMSampleBufferRef
s.Convert them to
CGImageRef
s using this code:From there you would convert to a UIImage,
Then use
to draw the frame to a context, draw a PNG of the watermark over it, and then add the processed images to your output video using
AVAssetWriter
. I would suggest adding them in real time so you're not filling up memory with tons of UIImages.How do I export UIImage array as a movie? - this post shows how to add the UIImages you have processed to a video for a given duration.
This should get you well on your way to watermarking your videos. Remember to practice good memory management, because leaking images that are coming in at 20-30fps is a great way to crash the app.
@Julio 给出的答案在 Objective-c 的情况下已经可以正常工作了
以下是 Swift 3.0 的相同代码库:
从文档目录和文件目录中获取输出文件创建AVURLAsset
创建带有水印图像的图层:
创建带有文本作为水印而不是图像的图层:
在视频上正确添加图层水印顺序
将视频裁剪为方形格式 - 大小为 300*300
旋转为纵向
导出视频的最后一步
这会将视频导出到带有水印的方形尺寸作为文本或图像
谢谢
Already the answer given by @Julio works fine in case of objective-c
Here's the same code base for Swift 3.0:
Getting the output file from Documents Directory & create AVURLAsset
Create the layer with the watermark image:
Create the layer with Text as watermark instead of image:
Adding the layers over the video in proper order for watermark
Cropping the video in square format - of 300*300 in size
Rotate to Portrait
Final step to export the video
This will export the video in square size with watermark as Text Or Image
Thanks
只需下载代码并使用它即可。它位于 Apple 开发人员文档页面中。
http://developer.apple.com/library/ios/ #samplecode/AVSimpleEditoriOS/Listings/AVSimpleEditor_AVSERotateCommand_m.html
Simply Download the code and Use it.It is in Apple developer documentation Page.
http://developer.apple.com/library/ios/#samplecode/AVSimpleEditoriOS/Listings/AVSimpleEditor_AVSERotateCommand_m.html
通过使用 mikitamanko 的博客 我做了一些小改动来修复出现以下错误:
解决方案是在设置图层指令时使用合成的视频轨道而不是原始视频轨道,如以下 swift 5 代码所示:
请注意,根据 AVFoundation 在导出带有文本层的视频时崩溃此代码仅在模拟器上崩溃,但在真实设备上运行
另请注意,宽度和高度是在应用首选视频转换后使用的。
Adding a CALayer to a video by working with the swift example code found in mikitamanko's blog I made a few small changes to fix the following error:
The solution is to use the composition's video track instead of the original video track when setting the layer instruction like in the following swift 5 code:
Note that according to AVFoundation Crash on Exporting Video With Text Layer this code crashes only on simulator but works on a real device
Also note that the width and height are used after applying the preferred video transform.
这是swift3 上的示例 如何将动画(图像/幻灯片/帧数组)和静态图像水印插入录制的内容 视频。
它使用 CAKeyframeAnimation 为帧设置动画,并使用 AVMutableCompositionTrack、AVAssetExportSession 和 AVMutableVideoComposition 以及 AVMutableVideoCompositionInstruction 来组合所有内容一起。
Here's the example on swift3 how to insert both animated (array of images/slides/frames) and static image watermarks into the recorded video.
It uses CAKeyframeAnimation to animate the frames, and it uses AVMutableCompositionTrack, AVAssetExportSession and AVMutableVideoComposition together with AVMutableVideoCompositionInstruction to combine everything together.