How to get movie file from ReplayKit?

Generated on 7/30/2024

1 search

To get a movie file from ReplayKit, you can use the new recording API provided by ScreenCaptureKit. Here’s a step-by-step guide based on the information from the WWDC sessions:

  1. Create an SC Recording Output Configuration:

    • Set an output URL to specify where to save the file.
    • Optionally, customize the file type and video codec for the saved file.
  2. Create an SC Recording Output Instance:

    • Use the recording configuration created in the previous step.
    • Set the delegate for receiving events.
  3. Add the Configured Recording Output to the Stream:

    • Call startCapture to start the stream, which will also start recording.
    • To stop recording, call stopCapture. If you want to continue streaming after stopping the recording, call removeRecordingOutput instead.
  4. Handle Recording Events:

    • Use the SC Recording Output Delegate to be notified when the recording starts, if an error occurs, and when the recording has successfully finished.

Here is a code snippet to illustrate these steps:

// Step 1: Create an SC Recording Output Configuration
let recordingConfig = SCRecordingOutputConfiguration()
recordingConfig.outputURL = URL(fileURLWithPath: "path/to/save/file")
recordingConfig.fileType = .mov // Optional
recordingConfig.videoCodec = .h264 // Optional

// Step 2: Create an SC Recording Output Instance
let recordingOutput = SCRecordingOutput(configuration: recordingConfig)
recordingOutput.delegate = self // Set your delegate

// Step 3: Add the Configured Recording Output to the Stream
stream.addOutput(recordingOutput)
stream.startCapture()

// To stop recording
stream.stopCapture()

// To continue streaming after stopping the recording
stream.removeOutput(recordingOutput)

For more detailed information, you can refer to the session Capture HDR content with ScreenCaptureKit at the timestamp 08:41.