         CGImageDestinationFinalize(destination)
     }
 
-    func toCVPixelBuffer() -> CVPixelBuffer {
-        var settings = [
-            kCVPixelBufferCGImageCompatibilityKey as String: true,
-            kCVPixelBufferCGBitmapContextCompatibilityKey as String: true
-        ]
+    func toCVPixelBuffer(width: Int, _ height: Int,
+        _ pixelFormat: OSType) -> CVPixelBuffer {
 
-        var baseAddress = CFDataGetBytePtr(
-            CGDataProviderCopyData(CGImageGetDataProvider(self))
-        )
+            var settings = [
+                kCVPixelBufferCGImageCompatibilityKey as String: true,
+                kCVPixelBufferCGBitmapContextCompatibilityKey as String: true
+            ]
 
-        var buffer : Unmanaged<CVPixelBuffer>?
+            var baseAddress = UnsafeMutablePointer<Void>(CFDataGetBytePtr(
+                CGDataProviderCopyData(CGImageGetDataProvider(self)))
+            )
 
-        var status = CVPixelBufferCreateWithBytes( kCFAllocatorDefault,
-            CGImageGetWidth(self), CGImageGetHeight(self),
-            OSType(kCVPixelFormatType_32ARGB),
-            UnsafeMutablePointer<Void>(baseAddress),
-            CGImageGetBytesPerRow(self), nil, nil, settings, &buffer
-        )
+            var bytesPerRow = CGImageGetBytesPerRow(self)
 
-        return buffer!.takeRetainedValue()
+            var buffer : Unmanaged<CVPixelBuffer>?
+
+            var status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
+                width, height, pixelFormat, baseAddress, bytesPerRow, nil,
+                nil, settings, &buffer
+            )
+
+            return buffer!.takeRetainedValue()
     }
 
 }


    // ADD: async execution, completed callback
    func saveAsMPEG4<T: CGImage>(to dir: String, with name: String,
        and fps: Int) {
            let url = NSURL(fileURLWithPath: "\(dir)\(name).mp4")
            let video = AVAssetWriter(URL: url, fileType: AVFileTypeMPEG4,
                error: nil)

            // NOT HANDLED: self.count == 0
            let first = self.first! as! CGImage
            let width = CGImageGetWidth(first)
            let height = CGImageGetHeight(first)

            let settings = [
                AVVideoCodecKey: AVVideoCodecH264,

            }

            for t in self {
                let buffer = (t as! CGImage).toCVPixelBuffer(width, height,
                    osPixelFormat)
                let time = getTime()
                ExponentialBackoff(isReady, 0.1, 2.0) {
                    adaptor.appendPixelBuffer(buffer,
                        withPresentationTime: time)
                }
                count++
            }

            input.markAsFinished()
            video.endSessionAtSourceTime(getTime())
            video.finishWritingWithCompletionHandler({})
    }

}
