gpt4 book ai didi

ios - 如何在不使用相机的情况下捕获不断变化的 CIImage 视频?

转载 作者:行者123 更新时间:2023-11-29 05:20:12 24 4
gpt4 key购买 nike

我已经在这里提出了一个问题但没有任何回复: How do I record changes on a CIImage to a video using AVAssetWriter?

但也许我的问题需要更简单。我的谷歌搜索毫无结果。 如何在不使用相机的情况下实时捕获不断变化的 CIImage 视频?

使用 captureOutput,我得到一个 CMSampleBuffer,我可以将其制作成 CVPixelBuffer。 AVAssetWriterInput 的 mediaType 设置为视频,但我认为它需要压缩视频。此外,我不清楚 AVAssetWriterInput ExpectsMediaDataInRealTime 属性是否应设置为 true 。

看起来应该相当简单,但我尝试的所有操作都会使我的 AVAssetWriter 状态失败。

这是我最后一次尝试完成这项工作。仍然失败:

@objc func importLivePreview(){

guard var importedImage = importedDryCIImage else { return }

DispatchQueue.main.async(){

// apply filter to camera image
// this is what makes the CIImage appear that it is changing
importedImage = self.applyFilterAndReturnImage(ciImage: importedImage, orientation: UIImage.Orientation.right, currentCameraRes:currentCameraRes!)


if self.videoIsRecording &&
self.assetWriterPixelBufferInput?.assetWriterInput.isReadyForMoreMediaData == true {

guard let writer: AVAssetWriter = self.assetWriter, writer.status == .writing else {
return
}

guard let cv:CVPixelBuffer = self.buffer(from: importedImage) else {
print("CVPixelBuffer could not be created.")
return
}

self.MTLContext?.render(_:importedImage, to:cv)

self.currentSampleTime = CMTimeMakeWithSeconds(0.1, preferredTimescale: 1000000000)

guard let currentSampleTime = self.currentSampleTime else {
return
}

let success = self.assetWriterPixelBufferInput?.append(cv, withPresentationTime: currentSampleTime)

if success == false {
print("Pixel Buffer input failed")
}

}

guard let MTLView = self.MTLCaptureView else {
print("MTLCaptureView is not found or nil.")
return
}

// update the MTKView with the changed CIImage so the user can see the changed image
MTLView.image = importedImage


}

}

最佳答案

我成功了。问题是我没有抵消 currentSampleTime。这个例子没有准确的偏移量,但它表明它需要添加到最后一次。

@objc func importLivePreview(){

guard var importedImage = importedDryCIImage else { return }

DispatchQueue.main.async(){

// apply filter to camera image
// this is what makes the CIImage appear that it is changing
importedImage = self.applyFilterAndReturnImage(ciImage: importedImage, orientation: UIImage.Orientation.right, currentCameraRes:currentCameraRes!)


if self.videoIsRecording &&
self.assetWriterPixelBufferInput?.assetWriterInput.isReadyForMoreMediaData == true {

guard let writer: AVAssetWriter = self.assetWriter, writer.status == .writing else {
return
}

guard let cv:CVPixelBuffer = self.buffer(from: importedImage) else {
print("CVPixelBuffer could not be created.")
return
}

self.MTLContext?.render(_:importedImage, to:cv)

guard let currentSampleTime = self.currentSampleTime else {
return
}

// offset currentSampleTime
let sampleTimeOffset = CMTimeMakeWithSeconds(0.1, preferredTimescale: 1000000000)

self.currentSampleTime = CMTimeAdd(currentSampleTime, sampleTimeOffset)

print("currentSampleTime = \(String(describing: currentSampleTime))")

let success = self.assetWriterPixelBufferInput?.append(cv, withPresentationTime: currentSampleTime)

if success == false {
print("Pixel Buffer input failed")
}

}

guard let MTLView = self.MTLCaptureView else {
print("MTLCaptureView is not found or nil.")
return
}

// update the MTKView with the changed CIImage so the user can see the changed image
MTLView.image = importedImage


}

}

关于ios - 如何在不使用相机的情况下捕获不断变化的 CIImage 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58739385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com