                     if fileManager.fileExists(atPath: url.path) {
                         try? fileManager.removeItem(at: url)
                     }
+                    self.url = url
+
                     guard let writer = try? AVAssetWriter(url: url, fileType: AVFileTypeQuickTimeMovie) else {
                         print("failed to create a file", url)
                         return
                     }
-                    self.url = url
                     self.writer = writer
 
                     /* no need to specify the compression settings

        CVPixelBufferLockBaseAddress(pixelBuffer, [])
        defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, []) }
        let pixelBufferBytes = CVPixelBufferGetBaseAddress(pixelBuffer)!
        
        // Use the bytes per row value from the pixel buffer since its stride may be rounded up to be 16-byte aligned
        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
        let region = MTLRegionMake2D(0, 0, texture.width, texture.height)
        
        texture.getBytes(pixelBufferBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
        
        adaptor.append(pixelBuffer, withPresentationTime: time)

    private func processNext() {
        guard let reader = self.reader,
            let output = self.output,
            let writer = self.writer else {
                return
        }
        guard reader.status == .reading,
            let sampleBuffer = output.copyNextSampleBuffer(),
            let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
            print("Process Complete")
            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)
                }
            }
            return
        }
        
        let time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
        let width = CVPixelBufferGetWidth(pixelBuffer), height = CVPixelBufferGetHeight(pixelBuffer)
        var metalTextureFromPixelBuffer:CVMetalTexture? = nil
        let status = CVMetalTextureCacheCreateTextureFromImage(nil, self.textureCache, pixelBuffer, nil,
