             writer.append(sample: sample, type: type)
             if type == .video
             {
-                let time = CMSampleBufferGetPresentationTimeStamp(sample)
-                if Int(time.seconds) % 5 == 0 && (time - synctime).seconds >= 1
-                {
-                    synctime = time
-                    synchronize(timestamp: time.seconds)
+                let position = CMSampleBufferGetPresentationTimeStamp(sample) - writer.timeOffset
+                if Int(position.seconds) % 5 == 0 && (position - synctime).seconds >= 1
+                {
+                    synctime = position
+                    synchronize(timestamp: position.seconds)
                 }
             }
         }

            case .audioMic:return "audio_mic"
            case .video:return "video"
        }
    }
}


        }
    }
    
    //MARK: camera
    private func setupCamera()
    {
        guard let cameraView = RPScreenRecorder.shared().cameraPreviewView else {return}
        if cameraView.constraints.count > 0 {return}
        
        let tap = UITouchGestureRecognizer(target: self, action: #selector(switchCamera(_:)))
        cameraView.addGestureRecognizer(tap)
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(moveCameraPreview(_:)))
        pan.maximumNumberOfTouches = 1
        cameraView.addGestureRecognizer(pan)
        tap.require(toFail: pan)
    }
    
    @objc private func moveCameraPreview(_ gesture:UIPanGestureRecognizer)
    {
        guard let view = gesture.view else {return}
        switch gesture.state
        {
            case .began:
                gesture.setTranslation(CGPoint.zero, in: view)
            case .changed:
                let translation = gesture.translation(in: view)
                view.frame = view.frame.offsetBy(dx: translation.x, dy: translation.y)
                gesture.setTranslation(CGPoint.zero, in: view)
            default:break
        }
    }
    
    @objc private func switchCamera(_ gesture:UITouchGestureRecognizer)
    {
        guard gesture.state == .began else { return }
        if gesture.numberOfTouches >= 2
        {
            let position = RPScreenRecorder.shared().cameraPosition
            switch position
            {
                case .back: RPScreenRecorder.shared().cameraPosition = .front
                case .front: RPScreenRecorder.shared().cameraPosition = .back
            }
        }
        else
        {
            if let view = RPScreenRecorder.shared().cameraPreviewView
            {
                let alpha:CGFloat
                if view.alpha == 1.0
                {
                    alpha = 0.2
                }
                else
                {
                    alpha = 1.0
                }
                
                view.layer.removeAllAnimations()
                UIView.animate(withDuration: 0.2)
                {
                    view.alpha = alpha
                }
            }
        }
    }
