如何将静态图像添加到 AVComposition 中?
我有一个带有视频轨道的 AVMutableComposition
,我想将静态图像添加到视频轨道中,以便在给定时间内显示。静态图像只是一个PNG。我可以将图像作为资产加载,但仅此而已,因为生成的资产没有任何轨道,因此不能简单地使用 insertTimeRange...
方法插入。
有没有办法将静态图像添加到合成中?看起来答案就在核心动画中的某个地方,但整个事情似乎有点超出我的想象,我希望有一个代码示例或一些信息指针。
I have an AVMutableComposition
with a video track and I would like to add a still image into the video track, to be displayed for some given time. The still image is simply a PNG. I can load the image as an asset, but that’s about it, because the resulting asset does not have any tracks and therefore cannot be simply inserted using the insertTimeRange…
methods.
Is there a way to add still images to a composition? It looks like the answer is somewhere in Core Animation, but the whole thing seems to be a bit above my head and I would appreciate a code sample or some information pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的。 WWDC 有一个很棒的视频,名为使用 AV Foundation 编辑媒体,其中解释了很多内容。您无法将图像直接插入
AVComposition
时间线,至少我没有找到任何方法来做到这一点。但是,在导出或播放资产时,您可以引用AVVideoComposition
。对于该类来说,这可能不是一个完美的名称,因为它允许您在资源中的各种视频轨道之间进行混合,非常类似于AVAudioMix
对音频的作用。AVVideoComposition
有一个animationTool
属性,可让您将核心动画层 (CALayer
) 放入混合中。CALayer
有一个contents
属性,可以分配一个CGImageRef
。对我的情况没有帮助,可能对其他人有帮助。OK. There’s a great video called Editing Media with AV Foundation from WWDC that explains a lot. You can’t insert images right to the
AVComposition
timeline, at least I did not find any way to do that. But when exporting or playing an asset you can refer to anAVVideoComposition
. That’s maybe not a perfect name for the class, since it allows you to mix between various video tracks in the asset, very much likeAVAudioMix
does for audio. And theAVVideoComposition
has ananimationTool
property that lets you throw Core Animation layers (CALayer
) into the mix.CALayer
has acontents
property that can be assigned aCGImageRef
. Does not help in my case, might help somebody else.我的作品中还需要静止图像。我的思路有点不同。在应该出现图像的地方插入黑色的动态电影(可能一个这样的视频就足够了)。为每个此类插入添加字典参考,将构图时间范围链接到真正所需的图像。当正确的时间范围到达我的全职自定义合成器时,拉出所需的图像并将其绘制到输出像素缓冲区中,忽略合成中传入的黑色帧。我认为这是另一种方式。
I also need still images in my composition. My line of thinking is a little different. Insert on-the-fly movies of black in place of when images should be appearing (possibly one such video would suffice). Add a dictionary reference to each such insert, linking composition time-ranges to bona-fide desired images. When the correct time range arrives in my full-time custom compositor, pull out the desired image and paint that into the output pixel buffer, ignoring the incoming black frames from the composition. I think that'd be another way of doing it.