I want to apply CIFilter on video and save that filter applied video. [export taking too much time]

别说谁变了你拦得住时间么 提交于 2019-11-28 12:39:19

问题


I want to apply CIFilter on video and save that filter applied video.

I am using AVMutableVideoComposition (also tried with AVVideoComposition) for apply filter on video and its working fine with AVPlayer but when i am exporting filtered video then it takes too much time to save locally (20sec video taking arround 6-8 min). Please guide me what i am doing wrong... Thankyou !

let item = AVPlayerItem(asset: currentAsset!)
    let videoComposition = AVMutableVideoComposition(asset: currentAsset!) { request in
        let filter = CIFilter(name: "CIColorInvert")!
        let source = request.sourceImage.clampedToExtent()
        filter.setValue(source, forKey: kCIInputImageKey)

        let output = filter.outputImage!.cropped(to: request.sourceImage.extent)

        // Provide the filter output to the composition
        request.finish(with: output, context: nil)
    }
    item.videoComposition = videoComposition
    self.player.replaceCurrentItem(with: item)

    let exporter = AVAssetExportSession(asset: item.asset, presetName: AVAssetExportPresetHighestQuality)
    exporter?.videoComposition = videoComposition

    exporter?.outputFileType = .mp4
    let filename = "filename.mp4"

    let documentsDirectory = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!
    let outputURL = documentsDirectory.appendingPathComponent(filename)
    exporter?.outputURL = outputURL
    exporter?.exportAsynchronously(completionHandler: {
        guard exporter?.status == .completed else {
            print("export failed: \(exporter?.error)")
            return
        }
        print("done: ",outputURL)
    }
    )

来源:https://stackoverflow.com/questions/54652637/i-want-to-apply-cifilter-on-video-and-save-that-filter-applied-video-export-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!