我正在对视频进行转码,以将 ML Vision 的绘图嵌入到新视频中。方法是使用avassetreader 和avassetwriter。我将 cmsamplebuffer 传递给视觉并获得结果。我用结果构建了 calayer。我不确定如何将 cmsamplebuffer 与 calayer 与图纸相协调。
这是片段
func transcode(completion: @escaping (AVURLAsset) -> Void) {
var videoComposition = AVVideoComposition()
var composition = AVComposition(url: model.assetInfo.asset.url)
let tracks = composition.tracks(withMediaType: .video)
var videoSettings = [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_420YpCbCr8PlanarFullRange)]
let videoCompositionOutput = AVAssetReaderVideoCompositionOutput(videoTracks: tracks, videoSettings: videoSettings)
videoCompositionOutput.videoComposition = videoComposition
if reader.canAdd(videoCompositionOutput) {
reader.add(videoCompositionOutput)
}
else {
fatalError()
}
self.reader.startReading()
var isDone = false
while(!isDone) {
if let buffer = videoCompositionOutput.copyNextSampleBuffer(),
let imageBuffer = CMSampleBufferGetImageBuffer(buffer) {
let ciimage = CIImage(cvImageBuffer: imageBuffer)
let context = CIContext()
if let cgImage = context.createCGImage(ciimage, from: ciimage.extent) {
let uiImage = UIImage(cgImage: cgImage)
let frame = CGRect(origin: .zero, size: uiImage.size)
let visionDirector = VisionDirector()
visionDirector.createVisionProductByDrawingTypeAndBuffer(drawingType: .ML, cmSampleBuffer: buffer) {
(visionProduct) in
let model = CALayerFactoryByDrawTypeModel(frame: frame, drawingType: .ML, visionProduct: visionProduct)
let factory = CALayerFactoryByDrawType(model: model)
let layer = factory.build()
//how do we get the calayer embedded on the cmsamplebuffer?
//then we'll stream the cmsamplebuffer in the avassetwriter for a new video.
}
}
}
else {
if self.reader.status == .failed, let error = reader.error?.localizedDescription {
fatalError(error)
}
else {
isDone = true
}
}
}
}