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:

using System.Linq;

// We've collected a bunch of frames we want to turn into a video
Texture2D[] frames = new [] { ... };
// First, we need to collect the pixel data that will be sent to the recorder
Color32[] pixelBuffers = frames.Select(tex => tex.GetPixels32()).ToArray();

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:

// Create a fixed interval clock that generates timestamps for 30FPS
var clock = new FixedIntervalClock(30);

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:

// Create a recorder
var recorder = new MP4Recorder(1280, 720, 30);
// We use `Task.Run` to record in a background thread
var path = await Task.Run(() => {
    // Record all the frames in quick succession
    foreach (var pixelBuffer in pixelBuffers)
        recorder.CommitFrame(pixelBuffer, clock.timestamp);
    // All done :)
    return recorder.FinishWriting();
});

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:

// This will effectively pause recording
cameraInput.Dispose();
audioInput.Dispose();

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:

// Stop the realtime clock
realtimeClock.paused = true;

To resume recording, simply un-pause your clock and recreate your recorder inputs:

// Un-pause the clock
realtimeClock.paused = false;
// Recreate recorder inputs
cameraInput = new CameraInput(recorder, realtimeClock, ...);
audioInput = new AudioInput(recorder, realtimeClock, ...);

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.

Last updated