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

1

1

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)
    }
    )
Striated answered 12/2, 2019 at 14:44 Comment(4)
Have you tried to create a different composition instance for the export session?Weedy
Yes, I already tried separately for both of them but did not work.Striated
Have you tried another quality preset? This question seems to be related: stackoverflow.com/questions/51036125Weedy
I tried all the preset but only passthrough exporting video (without filter) others preset did not export anything. I did not understand why this is happening?Striated
T
3

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

Taegu answered 20/4, 2020 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.