Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

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

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)
    }
    )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hello, Try as for Applying filter on video File

let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let savedoutputURL = documentsDirectory.appendingPathComponent("Filterd.mov")
try? FileManager.default.removeItem(at: savedoutputURL)

let urlVideofile = Bundle.main.url(forResource: "(your video file name)", withExtension: "mov")

let videoAsset = AVAsset(url: urlVideofile!)

let filter = CIFilter(name: "CIColorInvert")!
let composition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

    let source = request.sourceImage.clampingToExtent()
    filter.setValue(source, forKey: kCIInputImageKey)

    // Crop the invert output to the bounds of the original image
    let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

    // Provide the filter output to the composition
    request.finish(with: output, context: nil)
})
// Simultaneous Video playing although you can use as your's

let playerItem = AVPlayerItem(asset: videoAsset)
playerItem.videoComposition = composition

self.player = AVPlayer(playerItem: playerItem)

self.player.volume = 0.0

self.playerController.player = self.player

 present(self.playerController, animated: true, completion: {
      self.player.play()
})

Saving Video

let export = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
    export!.outputFileType = AVFileType.mov
    export!.outputURL = savedoutputURL
    export!.videoComposition = composition

    export?.exportAsynchronously(completionHandler: {

        if export!.status.rawValue == 4 {
            print("Export failed -> Reason: (export?.error!.localizedDescription))")
            print(export?.error!)
            return
        }
        print("done: ",savedoutputURL)

    })

It is 100% tested code apply filter on 26 second video approx in 10 to 15 second

:)enjoy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...