如何使用AVAssetWriter向视频添加静态和动态叠加? [英] How to add static and dynamic overlays to video with AVAssetWriter?

查看:547
本文介绍了如何使用AVAssetWriter向视频添加静态和动态叠加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向使用AVAssetWriter创建的视频中添加图像叠加层的正确方法是什么?

What's the right way to add an image overlay to a video created with AVAssetWriter?

使用AVAssetExportSession可以这样做,但是这个问题是关于如何使用AVAssetWriter这样做的,这样可以更好地控制质量和输出.

It's possible to do so with AVAssetExportSession, but this question is about how to do so with AVAssetWriter so there is more control over the quality and output.

有两种情况:

1)简单:添加一个覆盖层,该覆盖层可以在视频的整个时长内显示(类似于水印).

1) Simple: Add single overlay that is present the entire duration of the video (similar to a watermark).

2)复杂:添加不同的叠加层,以在不同时间在视频中进行动画处理(类似于使用AVVideoCompositionCoreAnimationTool).

2) Complex: Add different overlays that animate in and out of the video at different times (similar to using AVVideoCompositionCoreAnimationTool).

推荐答案

对此有很多不同的方法,正确的答案将完全取决于您的用例.

There's a lot of different approaches to this and the correct answer is going to depend on exactly what your use case is.

从总体上讲,这是三种方法:

At a high level, here's three approaches:

  1. 您似乎已经熟悉AVVideoCompositionCoreAnimationTool.您可以将其与AVAssetWriter一起使用.请查看 https://github.com/rs/SDAVAssetExportSession ,它取代了AVAssetExportSession,允许您传递所需的AVAssetWriter设置(因为它在内部使用AVAssetWriter).
  2. 如果您想将类似WaterMark的内容合成到实时视频中(例如此问题使用AVFoundation(OSX)向视频添加过滤器-如何将生成的图像写回到AVWriter?
  3. 取决于您需要做的工作,您可以通过创建一个符合
  1. You appear to be already familiar with AVVideoCompositionCoreAnimationTool. You CAN use this with AVAssetWriter. Check out https://github.com/rs/SDAVAssetExportSession which is a drop in replacement for AVAssetExportSession that allows you to pass the AVAssetWriter settings you're seeking (because it uses AVAssetWriter internally).
  2. If you want to composite something like a WaterMark into live video (like in this question Simulate AVLayerVideoGravityResizeAspectFill: crop and center video to mimic preview without losing sharpness) then you can actually modify the sample buffer which is passed to the captureOutput function by the AVCaptureVideoDataOutputSampleBufferDelegate. The typical approach here is convert the CMSampleBuffer to a CIImage and then do whatever manipulation you like, finally convert the CIImage BACK to a CMSampleBuffer and write it out. In the question linked, the CMSampleBuffer is simply passed on without any manipulation. NB The step from CIImage back to CMSampleBuffer is relatively low level, there are lots of examples on StackOverflow however, although not many in Swift. Here's one implementation (for OSX however) Adding filters to video with AVFoundation (OSX) - how do I write the resulting image back to AVWriter?
  3. Depending on just HOW complex what you need to do is, you could look at implementing your own custom compositor by creating a class that complies with https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVVideoCompositing_Protocol/ and you reference in the AVVideoComposition. This is complex and (probably) overkill - if you don't know why you need to do this, then you probably don't need one. If you start struggling with problems like "how can I have multiple animation layers on different tracks in my video and not all on one track" or "how can I rotate, scale and animate moving video within an image frame - like a polaroid that spins in while the video is playing in the frame"... well this is what you need to look into.

如果您需要更多信息,那么,如果您对要执行的操作进行了说明,我也许可以扩展此答案,以添加有关适当方法的更多详细信息.

If you need some further info, then if you add some clarification on what you're trying to do, I may be able to expand this answer to add more detail on the appropriate approach.

这篇关于如何使用AVAssetWriter向视频添加静态和动态叠加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆