NatCorder
Search…
Recorder Inputs
Simplifying Common Recording Workflows
In most use cases, you will likely only want to record from a game camera (or several game cameras) and/or an audio source in your scene. Instead of writing boilerplate code to extract raw pixel buffers and sample buffers from these game objects, NatCorder provides a set of helper classes which automatically handle this process. These are called Recorder Inputs.

Recording Game Cameras

NatCorder provides the CameraInput class for recording one or more game cameras.
Single Camera
Multiple Cameras
1
// Create a recorder
2
var recorder = new MP4Recorder(...);
3
var clock = new RealtimeClock();
4
// And use a `CameraInput` to record the main game camera
5
var cameraInput = new CameraInput(recorder, clock, Camera.main);
Copied!
1
// Create a recorder
2
var recorder = new MP4Recorder(...);
3
var clock = new RealtimeClock();
4
// And use a `CameraInput` to record multiple game cameras
5
var cameraInput = new CameraInput(
6
recorder,
7
clock,
8
cameraA, cameraB, cameraC, ... // all the cameras you want recorded
9
);
Copied!
When finishing recording, dispose the CameraInput before stopping the recorder.
1
// Stop sending frames to the recorder
2
cameraInput.Dispose();
3
// Finish writing
4
var path = await recorder.FinishWriting();
Copied!
You must dispose all recorder inputs before finishing the recorder, not after.

Recording Game Audio

For recording game audio, either from a Unity AudioSource or AudioListener, NatCorder provides the AudioInput class.
Audio Source
Audio Listener
1
// Say we have an `AudioSource` we want to record
2
var audioSource = ...;
3
// Create a recorder
4
var recorder = new WAVRecorder(...);
5
var clock = new RealtimeClock();
6
// And use an `AudioInput` to record the audio source
7
var audioInput = new AudioInput(recorder, clock, audioSource);
Copied!
1
// Say we have an `AudioListener` we want to record
2
var audioListener = ...;
3
// Create a recorder
4
var recorder = new WAVRecorder(...);
5
var clock = new RealtimeClock();
6
// And use an `AudioInput` to record game audio
7
var audioInput = new AudioInput(recorder, clock, audioListener);
Copied!
Similarly, when finishing recording make sure to dispose the input before stopping the recorder.
1
// Stop recording
2
audioInput.Dispose();
3
var path = await recorder.FinishWriting();
Copied!

Best Practices

There are a few things to note when recording with recorder inputs.

Recording Timestamps

Most recorders expect audio and video frames to be committed with a corresponding timestamp, specified in nanoseconds. NatCorder provides implementations of the IClock interface to simplify the process of calculating timestamps.
All recorder inputs take in a recorder and a clock. The clock is used to generate timestamps as frames are being sent to recorder by the recorder input. When recording both video and audio frames, it is highly recommended to share the same clock between your camera input and audio input. This will guarantee that frames in the recording are synchronized in time, preventing issues like drift.

Using Multiple Inputs

It is possible to use multiple recorder inputs during a single recording session. A typical use case is when recording video from a game camera along with audio from the game.
1
// Create a recorder
2
var recorder = new HEVCRecorder(...);
3
var clock = new RealtimeClock();
4
// Create recorder inputs for committing both video and audio
5
var cameraInput = new CameraInput(recorder, clock, mainCamera);
6
var audioInput = new AudioInput(recorder, clock, audioListener);
Copied!
For a given recorder, do not create multiple recorder inputs that commit the same kind of media. For example, do not create several camera inputs.

Mixed Recording

It is possible to mix manual recording with recorder inputs. The constraint, as mentioned above, is that you must use one method for video and the other for audio. Below is a typical use case that involves mixed recording:
1
// Start the camera preview
2
var cameraTexture = new WebCamTexture(...);
3
cameraTexture.Play();
4
// Create a recorder
5
var recorder = new MP4Recorder(...);
6
var clock = new RealtimeClock();
7
// We will manually commit video frames from the `WebCamTexture`
8
// But we will use a recorder input to record audio
9
var audioInput = new AudioInput(recorder, clock, audioListener);
Copied!
With the above method, we must manually commit video frames.
1
while (recording)
2
recorder.CommitFrame(cameraTexture.GetPixels32(), clock.timestamp);
Copied!
Note that we share the same clock between our manual recording and our recorder input.
And when we're done recording, we dispose the recorder input.
1
// Stop sending media frames to the recorder
2
recording = false; // this will stop our manual recording loop above
3
audioInput.Dispose();
4
// Finish recording
5
var path = await recorder.FinishWriting();
Copied!
Last modified 24d ago