Applying a CIFilter to a Video File and Saving it
<p>Is there any fast, lightweight-as-possible way to apply a <code>CIFilter</code> to a video? Before it's mentioned, I <em>have</em> looked at <a href="https://github.com/BradLarson/GPUImage" rel="noreferrer">GPUImage</a> - it looks like very powerful <s>magic</s> code, but it's really overkill for what I'm trying to do.</p>

<p>Essentially, I would like to</p>

<ol>
<li>Take a video file, say stored at <code>/tmp/myVideoFile.mp4</code></li>
<li>Apply a <code>CIFilter</code> to this video file</li>
<li>Save the video file to a different (or the same) location, say <code>/tmp/anotherVideoFile.mp4</code></li>
</ol>

<p>I've been able to apply a CIFilter to a video that's playing extremely easily and quickly using <code>AVPlayerItemVideoOutput</code></p>

<pre><code>let player = AVPlayer(playerItem: AVPlayerItem(asset: video))
let output = AVPlayerItemVideoOutput(pixelBufferAttributes: nil)
player.currentItem?.addOutput(self.output)
player.play()

let displayLink = CADisplayLink(target: self, selector: #selector(self.displayLinkDidRefresh(_:)))
displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)

func displayLinkDidRefresh(link: CADisplayLink){
    let itemTime = output.itemTimeForHostTime(CACurrentMediaTime())
    if output.hasNewPixelBufferForItemTime(itemTime){
        if let pixelBuffer = output.copyPixelBufferForItemTime(itemTime, itemTimeForDisplay: nil){
            let image = CIImage(CVPixelBuffer: pixelBuffer)
            // apply filters to image
            // display image
        }
    }
}
</code></pre>

<p>This works great, but I've been having <s>a lot</s> just the tiniest bit of trouble finding out how to apply a filter to an already saved video file. There is the option of basically just doing what I did above, using an <code>AVPlayer</code>, playing the video, and getting the pixel buffer from every frame as it is played but this won't work for video processing in the background. I don't think users would appreciate having to wait as long as their video is for the filter to be applied.</p>

<p>In way over-simplified code, I'm looking for something like this:</p>

<pre><code>var newVideo = AVMutableAsset() // We'll just pretend like this is a thing

var originalVideo = AVAsset(url: NSURL(urlString: "/example/location.mp4"))
originalVideo.getAllFrames(){(pixelBuffer: CVPixelBuffer) -&gt; Void in
    let image = CIImage(CVPixelBuffer: pixelBuffer)
        .imageByApplyingFilter("Filter", withInputParameters: [:])

    newVideo.addFrame(image)
}

newVideo.exportTo(url: NSURL(urlString: "/this/isAnother/example.mp4"))
</code></pre>

<p>Is there any way fast (again, not involving GPUImage, and ideally working in iOS 7) way to apply a filter to a video file and then save it? For example this would take a saved video, load it into an <code>AVAsset</code>, apply a <code>CIFilter</code>, and then save the new video to a different location. </p>