NatCorder
Search…
Offline Recording
Record in a Loop
NatCorder is designed to support offline recording. Offline recording includes any form of recording that is not performed in realtime. This could mean recording frames in quick succession, like in video transcoding applications; or recording frames in a deferred fashion, like when pausing and resuming recording.

Accelerated Recording

When recording frames in quick succession, you would typically record manually instead of using recorder inputs. In such cases, it is usually good practice to record in a background thread so as to not block the application while recording is going on.
All recorders are thread-safe, so they can be used from any thread.
We will illustrate accelerated recording with a typical use case. Suppose we had a bunch of textures sitting in memory which we wanted to turn into a video:
1
// We've collected a bunch of frames we want to turn into a video
2
Texture2D[] frames = new [] { ... };
Copied!
Because we are recording these frames manually, we will need to synthesize timestamps which we will use to space out these frames in the resulting video. For this, we use the FixedIntervalClock:
1
// Create a fixed interval clock that generates timestamps for 30FPS
2
var clock = new FixedIntervalClock(30);
Copied!
Now, we can simply record in a loop. In order to free the main thread and not block the app, we'll use a background thread to record:
1
// Create a recorder
2
var recorder = new MP4Recorder(1280, 720, 30);
3
// We use `Task.Run` to record in a background thread
4
var path = await Task.Run(() => {
5
// Record all the frames in quick succession
6
foreach (var frame in frames)
7
recorder.CommitFrame(frame.GetPixels32(), clock.timestamp);
8
// All done :)
9
return recorder.FinishWriting();
10
});
Copied!

Pausing Recording

Fitting loosely with the offline recording paradigm is the task of pausing recording. Due to NatCorder's architecture, there isn't an explicit process for pausing recording. Instead, you pause recording by simply not sending any frames to the recorder.
If you are recording frames with recorder inputs, you can simply Dispose them to pause recording:
1
// This will effectively pause recording
2
cameraInput.Dispose();
3
audioInput.Dispose();
Copied!
The final detail to keep in mind is pausing time. From the perspective of the recorder, no time must have passed between a frame before pausing and a frame after pausing. As a result, you have to make sure to pause any RealtimeClock:
1
// Stop the realtime clock
2
realtimeClock.paused = true;
Copied!
To resume recording, simply un-pause your clock and recreate your recorder inputs:
1
// Un-pause the clock
2
realtimeClock.paused = false;
3
// Recreate recorder inputs
4
cameraInput = new CameraInput(recorder, realtimeClock, ...);
5
audioInput = new AudioInput(recorder, realtimeClock, ...);
Copied!
And that's it!
Note that you must use the same clock from before recording was paused. Not doing so will cause recording to fail and produce a corrupted media file.