     private func processNext() {
         guard let reader = self.reader,
             let output = self.output,
-            let input = self.input,
-            let writer = self.writer,
-            let url = self.url else {
+            let writer = self.writer else {
                 return
         }
         guard reader.status == .reading,
             let sampleBuffer = output.copyNextSampleBuffer(),
             let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
             print("Process Complete")
-            input.markAsFinished()
-            writer.finishWriting {
-                print("Finish Writing")
-                let sheet = UIActivityViewController(activityItems: [url], applicationActivities: nil)
-                if let popover = sheet.popoverPresentationController {
-                    popover.barButtonItem = self.btnCamera
+            if let url = self.url, let input = self.input {
+                input.markAsFinished()
+                writer.finishWriting {
+                    print("Finish Writing")
+                        let sheet = UIActivityViewController(activityItems: [url], applicationActivities: nil)
+                        if let popover = sheet.popoverPresentationController {
+                            popover.barButtonItem = self.btnCamera
+                        }
+                        self.present(sheet, animated: true, completion: nil)
                 }
-                self.present(sheet, animated: true, completion: nil)
-            }
-            return
-        }
-        
+            }
+            return
+        }
+        
+        let time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
         let width = CVPixelBufferGetWidth(pixelBuffer), height = CVPixelBufferGetHeight(pixelBuffer)
         var metalTextureFromPixelBuffer:CVMetalTexture? = nil
         let status = CVMetalTextureCacheCreateTextureFromImage(nil, self.textureCache, pixelBuffer, nil,

            context.pixelFormat = mtkView.colorPixelFormat
            renderer.orientation = .landscapeLeft // it means "do not transform"

            // This is an alternative way to create a script object (Beta)
            let script = VSScript()
                .mono()
            runtime = script.compile(context: context)
        }
    }
    


class SampleViewController4: UIViewController {
    @IBOutlet var btnCamera:UIBarButtonItem!

    // For Reading
    var reader:AVAssetReader?
    var output:AVAssetReaderTrackOutput?
    var texture:MTLTexture?

    // For processing
    var context:VSContext = VSContext(device: MTLCreateSystemDefaultDevice()!)
    var runtime:VSRuntime?

    // For writing
    var writer:AVAssetWriter?
    var input:AVAssetWriterInput?
    var adaptor:AVAssetWriterInputPixelBufferAdaptor?
    var url:URL?

    // For rendering
    lazy var commandQueue:MTLCommandQueue = self.context.device.makeCommandQueue()
    lazy var renderer:VSRenderer = VSRenderer(device:self.context.device, pixelFormat:self.context.pixelFormat)

    fileprivate lazy var textureCache:CVMetalTextureCache = {
        var cache:CVMetalTextureCache? = nil
        CVMetalTextureCacheCreate(nil, nil, self.context.device, nil, &cache)
