                 }
                 */
                 
-                input.append(sampleBuffer)
+                if !input.append(sampleBuffer) {
+                    print(String(format:"Failed appending sample at pts:%2.3f", pts.seconds))
+                } else {
+                    if mediaType == kCMMediaType_Video {
+                        lastVideoPTS = pts
+                    } else if mediaType == kCMMediaType_Audio {
+                        lastAudioPTS = pts
+                    }
+                }
             }
         }
     }
     
     public func finish(silent:Bool = false) {
-        if let writer = self.writer {
+        if let writer = self.writer, !finishing {
+            finishing = true
             writer.finishWriting(completionHandler: {
                 if !silent {
                     self.on(self,.finished, [:])


    
    public func append(sbuf sampleBuffer: CMSampleBuffer) {
        guard let writer = self.writer else {
            print("writer not initialized")
            return

    public let statusInfo:[InfoKey: Any]
    
    fileprivate var firstAudioSampleHandled: Bool = false
    fileprivate var finishing = false
    fileprivate var lastAudioPTS: CMTime?
    fileprivate var lastVideoPTS: CMTime?
    
    static func writerInputVideo(settings: AVCWriterVideoSettings?) -> AVAssetWriterInput? {
        guard let settings = settings else {
