                             self.texture = texture.texture
                         }
                         self.context.flush()
-                        self.processNext()
+                        
+                        if writer.status == .writing {
+                            writer.finishWriting {
+                                self.processNext()
+                            }
+                        } else {
+                            self.processNext()
+                        }
                     }
                 })
                 commandBuffer?.commit()

                if status == AVKeyValueStatus.loaded,
                   let reader = try? AVAssetReader(asset: asset) {
                    self.reader = reader
                    let tracks = asset.tracks(withMediaType: AVMediaTypeVideo)
                    let settings:[String:Any] = [kCVPixelBufferPixelFormatTypeKey as String:kCVPixelFormatType_32BGRA]
                    let output = AVAssetReaderTrackOutput(track: tracks[0], outputSettings: settings)
                    self.output = output
                    reader.add(output)
                    reader.startReading()

    
    func processNext() {
        guard let reader = self.reader,
            let writer = self.writer,
            let output = self.output else {
                return
        }
