如何在iOS中将一个视频叠加到另一个视频上? [英] How to overlay one video on another in iOS?

查看:571
本文介绍了如何在iOS中将一个视频叠加到另一个视频上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将已拍摄的视频裁剪为iOS中的一个圆圈.我该怎么做.我知道如何使用AVCaptureSession来做到这一点,但我不知道将已经拍摄的视频作为AVCaptureDevice传递吗?有没有一种方法可以将视频裁剪成一个圆圈.我想将其覆盖在另一个视频的顶部,因此它也必须具有透明的背景.谢谢.

I am trying to crop an already taken video into a circle in iOS. How might I go about doing this. I know how I would do it with AVCaptureSession but I don't know to pass in an already taken video as an AVCaptureDevice? Is there a way to crop a video into a circle. I want to overlay it on top of another video so it has to have a transparent background as well. Thanks.

推荐答案

我想您想生成这样的东西:

I guess you want to produce something like this:

您不想要AVCaptureSession,因为您没有捕获视频.您需要一个AVMutableComposition.您需要阅读编辑"部分.以下是您需要做什么的摘要:

You don't want an AVCaptureSession, because you're not capturing video. You want an AVMutableComposition. You need to read the "Editing" section of the AV Foundation Programming Guide. Here's a summary of what you need to do:

  1. 为视频创建AVAsset对象,然后等待它们加载曲目.

  1. Create the AVAsset objects for your videos and wait for them to load their tracks.

创建一个AVMutableComposition.

为每个输入视频的构图添加单独的AVMutableCompositionTrack.确保为每个轨道分配明确的不同轨道ID.如果让系统选择,它将为每个轨道使用轨道ID 1,以后将无法在合成器中访问这两个轨道.

Add a separate AVMutableCompositionTrack to the composition for each of the input videos. Make sure to assign explicit, different track IDs to each track. If you let the system pick, it will use track ID 1 for each and you won't be able to access both later in the compositor.

创建一个AVMutableVideoComposition.

创建一个AVMutableVideoCompositionInstruction.

对于每个输入视频,创建一个AVMutableVideoCompositionLayerInstruction并显式分配您在步骤3中使用的音轨ID.

For each input video, create an AVMutableVideoCompositionLayerInstruction and explicitly assign the track IDs you used back in step 3.

AVMutableVideoCompositionInstructionlayerInstructions设置为您在步骤6中创建的两层指令.

Set the AVMutableVideoCompositionInstruction's layerInstructions to the two layer instructions you created in step 6.

AVMutableVideoCompositioninstructions设置为您在步骤5中创建的指令.

Set the AVMutableVideoComposition's instructions to the instruction you created in step 5.

创建一个实现AVVideoCompositing协议的类.将视频合成(在步骤4中创建)的customVideoCompositorClass设置为此自定义类(例如videoComposition.customVideoCompositorClass = [CustomVideoCompositor class];).

Create a class that implements the AVVideoCompositing protocol. Set the customVideoCompositorClass of the video composition (created in step 4) to this custom class (e.g. videoComposition.customVideoCompositorClass = [CustomVideoCompositor class];).

在您的自定义合成器中,从AVAsynchronousVideoCompositionRequest获取输入像素缓冲区,并使用它们绘制复合帧(包含由前景视频帧的圆形块覆盖的背景视频帧).您可以根据需要执行此操作.我使用Core Graphics做到了这一点,因为这很容易,但是您可能想要使用OpenGL(或者也许是Metal)来提高生产应用程序的效率.如果使用OpenGL,请确保指定kCVPixelBufferOpenGLESCompatibilityKey.

In your custom compositor, get the input pixel buffers from the AVAsynchronousVideoCompositionRequest and use them to draw the composite frame (containing a background video frame overlaid by a circular chunk of the foreground video frame). You can do this however you want. I did it using Core Graphics because that's easy, but you'll probably want to use OpenGL (or maybe Metal) for efficiency in a production app. Be sure to specify kCVPixelBufferOpenGLESCompatibilityKey if you go with OpenGL.

使用步骤1中的内容创建一个AVAssetExportSession.

Create an AVAssetExportSession using your composition from step 1.

设置会话的输出URL和文件类型.

Set the session's output URL and file type.

将会话的videoComposition设置为步骤4中的视频组成.

Set the session's videoComposition to the video composition from step 4.

将会话告知exportAsynchronouslyWithCompletionHandler:.可能会很慢!

Tell the session to exportAsynchronouslyWithCompletionHandler:. It will probably be slow!

您可以找到我的测试项目在这个github存储库中.

You can find my test project in this github repository.

这篇关于如何在iOS中将一个视频叠加到另一个视频上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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