     if useAudio {
       audioInput.markAsFinished()
     }
-    self.stopDisplayLink()
-
-    self.isRecording = false
-    self.isPrepared = false
-    self.videoFramesWritten = false
+
+    endingTimestamp = getCurrentCMTime()
+
+    stopDisplayLink()
+    captureSession.stopRunning()
+
+    isRecording = false
+    isPrepared = false
+    videoFramesWritten = false
+
+    initialTime = kCMTimeInvalid
+    currentTime = kCMTimeInvalid
+    videoStartTimestamp = kCMTimeZero
+    firstVideoTimestamp = kCMTimeZero
+    firstAudioTimestamp = kCMTimeInvalid
 
     writer.finishWriting(completionHandler: { [weak self] in
       completionHandler(outputUrl)


      SceneKitVideoRecorder.renderSemaphore.wait()

      guard self?.startInputPipeline() == true else {
        print("AVAssetWriter Failed:", "Unknown error")
        SceneKitVideoRecorder.renderSemaphore.signal()


  }

  private func getCurrentCMTime() -> CMTime {
    return CMTimeMakeWithSeconds(CACurrentMediaTime(), 1000000);
  }

  private func getAppendTime() -> CMTime {
    currentTime = getCurrentCMTime() - initialTime
    let time = CMTimeAdd(firstAudioTimestamp, currentTime)
    return time
  }

  private func startDisplayLink() {

    displayLink = CADisplayLink(target: self, selector: #selector(updateDisplayLink))
    displayLink?.preferredFramesPerSecond = options.fps
    displayLink?.add(to: .main, forMode: .commonModes)
