         }
     }
     
-    private func writeNextFrame() {
-        guard let pixelBufferPool = adaptor?.pixelBufferPool else {
+    private func writeNextFrame(time:CMTime) {
+        guard let writer = self.writer,
+              let adaptor = self.adaptor,
+              let texture = self.texture else {
+                return
+        }
+        
+        guard let pixelBufferPool = adaptor.pixelBufferPool else {
             print("Pixel buffer asset writer input did not have a pixel buffer pool available; cannot retrieve frame")
             return
         }

                            self.texture = texture.texture
                        }
                        self.context.flush()
                        self.writeNextFrame()
                     }
                })
                commandBuffer?.commit()

            return
        }
        
        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)
        
        self.processNext()
    }
}
