【问题标题】:What is the best way to record a video with augmented reality用增强现实录制视频的最佳方式是什么
【发布时间】:2018-03-06 22:10:02
【问题描述】:

使用增强现实录制视频的最佳方式是什么? (在 iPhone/iPad 相机的框架中添加文字、图像徽标)

以前我试图弄清楚如何绘制到CIImageHow to draw text into CIImage?)并将CIImage转换回CMSampleBufferCIImage back to CMSampleBuffer

我几乎做了所有事情,只是在AVAssetWriterInput 中使用新的CMSampleBuffer 录制视频时遇到问题

但无论如何,这个解决方案一点都不好,它在将CIImage 转换为CVPixelBuffer (ciContext.render(ciImage!, to: aBuffer)) 时会占用大量 CPU。

所以我想在这里停下来,寻找一些其他方法来录制带有增强现实的视频(例如,在将视频编码为 mp4 文件时在帧内动态添加(绘制)文本)

这是我尝试过但不想再使用的...

// convert original CMSampleBuffer to CIImage, 
// combine multiple `CIImage`s into one (adding augmented reality -  
// text or some additional images)
let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
let ciimage : CIImage = CIImage(cvPixelBuffer: pixelBuffer)
var outputImage: CIImage?
let images : Array<CIImage> = [ciimage, ciimageSec!] // add all your CIImages that you'd like to combine
for image in images {
    outputImage = outputImage == nil ? image : image.composited(over: outputImage!)
}

// allocate this class variable once         
if pixelBufferNew == nil {
    CVPixelBufferCreate(kCFAllocatorSystemDefault, CVPixelBufferGetWidth(pixelBuffer),  CVPixelBufferGetHeight(pixelBuffer), kCVPixelFormatType_32BGRA, nil, &pixelBufferNew)
}

// convert CIImage to CVPixelBuffer
let ciContext = CIContext(options: nil)
if let aBuffer = pixelBufferNew {
    ciContext.render(outputImage!, to: aBuffer) // >>> IT EATS A LOT OF <<< CPU
}

// convert new CVPixelBuffer to new CMSampleBuffer
var sampleTime = CMSampleTimingInfo()
sampleTime.duration = CMSampleBufferGetDuration(sampleBuffer)
sampleTime.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
sampleTime.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
var videoInfo: CMVideoFormatDescription? = nil
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBufferNew!, &videoInfo)
var oBuf: CMSampleBuffer?
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufferNew!, true, nil, nil, videoInfo!, &sampleTime, &oBuf)

/*
try to append new CMSampleBuffer into a file (.mp4) using 
AVAssetWriter & AVAssetWriterInput... (I met errors with it, original buffer works ok 
- "from func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)")
*/*

有没有更好的解决方案?

【问题讨论】:

    标签: ios swift augmented-reality video-recording ios-camera


    【解决方案1】:

    现在我回答我自己的问题

    最好使用Objective-C++ 类 (.mm),我们可以在其中使用 OpenCV 并轻松/快速地从 CMSampleBuffer 转换为 cv::Mat 并在处理后返回到 CMSampleBuffer

    我们可以轻松地从 Swift 调用 Objective-C++ 函数

    【讨论】:

    • 你能分享一些代码吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 2011-04-05
    • 1970-01-01
    • 2022-10-08
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多