我已经设法通过使用AVCaptureFileOutputRecordingDelegate
设置了一个基本的AVCaptureSession
,它可以录制视频并保存到设备上。我一直在搜索文档,以了解我们如何在正在录制的视频上添加统计覆盖。
即
正如您在上图中所看到的。我有多个覆盖在顶部的视频预览层。现在,当我保存我的视频输出时,我也想将这些视图合成到视频上。
到目前为止,我已经尝试了什么?
MapView
,该怎么办?另外,我不是在寻找屏幕截图。屏幕上的一些内容将不会是最终录制的一部分,所以我希望能够选择要合成的视图。我要找的是什么?
到目前为止的进展:
我已经设法理解了我需要从CMSampleBuffer
获取CVImageBuffer
并在其上绘制文本。对于我来说,仍然不清楚是否有可能以某种方式在正在录制的视频上覆盖MapView。
发布于 2018-05-28 07:23:39
帮助您实现目标的最好方法是使用Metal
框架。使用Metal
摄像头可以最大限度地减少对设备有限计算资源的影响。如果您正在尝试以最低的开销访问摄像头传感器,那么使用AVCaptureSession
将是一个非常好的开始。
您需要从CMSampleBuffer
获取每个帧数据(您是对的),然后将帧转换为MTLTexture
。AVCaptureSession
将通过委托回调从设备的摄像头连续向我们发送帧。
所有可用的覆盖也必须转换为MTLTextures
。然后你可以用over
操作合成所有的MTLTextures
层。
因此,您可以在由四部分组成的 Metal Camera 系列中找到所有必要的信息。
这里有一个到博客的链接:About Compositing in Metal。
另外,我想发布代码的摘录(使用AVCaptureSession in Metal):
import Metal
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
// Handle an error here.
}
// Texture cache for converting frame images to textures
var textureCache: CVMetalTextureCache?
// `MTLDevice` for initializing texture cache
var metalDevice = MTLCreateSystemDefaultDevice()
guard
let metalDevice = metalDevice
where CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, metalDevice, nil, &textureCache) == kCVReturnSuccess
else {
// Handle an error (failed to create texture cache)
}
let width = CVPixelBufferGetWidth(imageBuffer)
let height = CVPixelBufferGetHeight(imageBuffer)
var imageTexture: CVMetalTexture?
let result = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache.takeUnretainedValue(), imageBuffer, nil, pixelFormat, width, height, planeIndex, &imageTexture)
// `MTLTexture` is in the `texture` variable now.
guard
let unwrappedImageTexture = imageTexture,
let texture = CVMetalTextureGetTexture(unwrappedImageTexture),
result == kCVReturnSuccess
else {
throw MetalCameraSessionError.failedToCreateTextureFromImage
}
,在这里你可以找到一个GitHub上的最终项目:MetalRenderCamera
https://stackoverflow.com/questions/50497671
复制相似问题