如何在iOS中将视频裁剪为圆圈? [英] How to crop a video to a circle in iOS?

查看:194
本文介绍了如何在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 。您需要阅读 AV Foundation编程指南 的SW1>编辑部分。以下是您需要做的总结:

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 并明确指定您在步骤中使用的曲目ID 3。

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

设置 AVMutableVideoCompositionInstruction layerInstructions 到你在步骤6中创建的两层指令。

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

设置 AVMutableVideoComposition 's 说明指向您在步骤5中创建的指令。

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

创建一个实现<$ c $的类c> AVVideoCompositing 协议。将类对象设置为视频合成的 customVideoCompositorClass

Create a class that implements the AVVideoCompositing protocol. Set class object as the video composition's customVideoCompositorClass.

在自定义合成器中,获取输入像素缓冲区来自 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.

创建 AVAssetExportSession 使用步骤1中的合成。

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天全站免登陆