我正在向预先录制的视频添加过滤器,但是当应用过滤器时,视频会滞后并且变慢。

非常感谢您的帮助。

func applyFilters(){
     let displayLink = CADisplayLink(target: self, selector: #selector(self.editVideo(_:)))
     displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)

let url = NSURL(fileURLWithPath: outputPath)
let asset = AVAsset(URL: url)
let item = AVPlayerItem(asset: asset)

player = AVPlayer(playerItem: item)
output = AVPlayerItemVideoOutput(pixelBufferAttributes: nil)
player?.currentItem?.addOutput(output)
player.play()
}
func editVideo(link: CADisplayLink){
let filter = CIFilter(name: "CISepiaTone")!
let itemTime = output.itemTimeForHostTime(CACurrentMediaTime())
if output.hasNewPixelBufferForItemTime(itemTime){
    if let pixelBuffer = output.copyPixelBufferForItemTime(itemTime, itemTimeForDisplay: nil){
        let image = CIImage(CVPixelBuffer: pixelBuffer)
        let context = CIContext(options: nil)
        filter.setValue(image, forKeyPath: kCIInputImageKey)
        if let output = filter.outputImage {
            let cgimg = context.createCGImage(output, fromRect: output.extent)
            let processedImage = UIImage(CGImage: cgimg)
            filtersImageView.image = processedImage
        }
    }
  }
}

最佳答案

您正在为每个帧创建一个新的CIContext,这是非常昂贵的性能。创建一个类似let context = CIContext()的属性,然后从let context = CIContext(options: nil)中删除​​editVideo。您还应该知道,默认情况下,CIContext将使用CPU进行图像处理,您可以指定OpenGL或Metal上下文以获得更好的性能,尽管在这种情况下这不是必需的。

有关您正在做的工作的完整示例,您应该查看FlexMonkey's VideoEffects repository

07-26 09:43