HEVCRecorder

class NatML.Recorders.HEVCRecorder : IMediaRecorder

The HEVCRecorder records MP4 videos with the H.265 HEVC video codec. This codec provides better compression at the expense of computational cost.

The HEVCRecorder uses the H.265 HEVC codec for video and the AAC codec for audio.

On Android, this recorder is not guaranteed to be supported on any given device.

On Windows, you must install the HEVC Video Extensions pack.

The HEVCRecorder is not supported on WebGL.

Creating the Recorder

/// <summary>
/// Create a HEVC recorder.
/// </summary>
/// <param name="width">Video width.</param>
/// <param name="height">Video height.</param>
/// <param name="framerate">Video framerate.</param>
/// <param name="sampleRate">Audio sample rate. Pass 0 for no audio.</param>
/// <param name="channelCount">Audio channel count. Pass 0 for no audio.</param>
/// <param name="videoBitRate">Video bit rate in bits per second.</param>
/// <param name="keyframeInterval">Keyframe interval in seconds.</param>
/// <param name="audioBitRate">Audio bit rate in bits per second.</param>
HEVCRecorder (
    int width,
    int height,
    float framerate,
    int sampleRate = 0,
    int channelCount = 0,
    int videoBitRate = 10_000_000,
    int keyframeInterval = 2,
    int audioBitRate = 64_000
);

The HEVCRecorder can be created to record video with optional audio. To record video only, simply provide the video width, video height, and video framerate.

// Record video only at 720p60
var recorder = new HEVCRecorder(1280, 720, 60);

To record video with audio, you will provide the audio format along with the video format. The audio format comprises of the sampleRate and channelCount.

// Record 1080p video with 48KHz stereo audio
var recorder = new HEVCRecorder(1920, 1080, 30, 48000, 2);

You can additionally specify the output video bitrate (in bits per second) and keyframe interval (in seconds).

// Record 1080p60 video only with custom compression settings
var recorder = new MP4Recorder(
    1920,
    1080,
    60,
    bitrate: 12_000_000,   // bits per second
    keyframeInterval: 3    // seconds
);

The videoBitRate and keyframeInterval parameters are important for controlling the output video file size.

On Windows, the HEVCRecorder will always output audio with a 192kbps bitrate.

When recording audio from Unity (e.g. using an AudioInput):

  • Set the sampleRate to AudioSettings.outputSampleRate.

  • Set the channelCount to (int)AudioSettings.speakerMode.

Video Size

/// <summary>
/// Video size.
/// </summary>
(int width, int height) frameSize { get; }

Refer to the Frame Size section of the IMediaRecorder interface for more information.

Some devices, especially Android devices, will fail to record when either the width or height is not divisible by two. In general, it is best to stick with a standard recording size like 1280x720 or 1920x1080.

Committing Video Frames

/// <summary>
/// Commit a video pixel buffer for encoding.
/// The pixel buffer MUST have an RGBA8888 pixel layout.
/// </summary>
/// <param name="pixelBuffer">Pixel buffer containing video frame to commit.</param>
/// <param name="timestamp">Pixel buffer timestamp in nanoseconds.</param>
void CommitFrame<T> (T[] pixelBuffer, long timestamp) where T : unmanaged;

Refer to the Committing Video Frames section of the IMediaRecorder interface for more information.

The spacing between consecutive timestamp values determines the actual video frame rate. The value passed to the constructor is merely a hint to the encoder.

Committed timestamps must be strictly monotonic. Not meeting this condition will cause recording to fail.

Committing Audio Frames

/// <summary>
/// Commit an audio sample buffer for encoding.
/// </summary>
/// <param name="sampleBuffer">Linear PCM audio sample buffer, interleaved by channel.</param>
/// <param name="timestamp">Sample buffer timestamp in nanoseconds.</param>
void CommitSamples (float[] sampleBuffer, long timestamp);

Refer to the Committing Audio Frames section of the IMediaRecorder interface for more information.

Finishing Recording

/// <summary>
/// Finish writing and return the path to the recorded media file.
/// </summary>
Task<string> FinishWriting ();

Refer to the Finishing Recording section of the IMediaRecorder interface for more information.

Last updated