“hujiajun” 4a6c026e55 initsdk | hace 9 meses | |
---|---|---|
.. | ||
Clocks | hace 9 meses | |
Inputs | hace 9 meses | |
Internal | hace 9 meses | |
Changelog.md | hace 9 meses | |
Changelog.md.meta | hace 9 meses | |
Clocks.meta | hace 9 meses | |
GIFRecorder.cs | hace 9 meses | |
GIFRecorder.cs.meta | hace 9 meses | |
HEVCRecorder.cs | hace 9 meses | |
HEVCRecorder.cs.meta | hace 9 meses | |
IMediaRecorder.cs | hace 9 meses | |
IMediaRecorder.cs.meta | hace 9 meses | |
Inputs.meta | hace 9 meses | |
Internal.meta | hace 9 meses | |
JPGRecorder.cs | hace 9 meses | |
JPGRecorder.cs.meta | hace 9 meses | |
MP4Recorder.cs | hace 9 meses | |
MP4Recorder.cs.meta | hace 9 meses | |
README.md | hace 9 meses | |
README.md.meta | hace 9 meses | |
WAVRecorder.cs | hace 9 meses | |
WAVRecorder.cs.meta | hace 9 meses |
NatCorder is a lightweight, easy-to-use, native video recording API for iOS, Android, macOS, and Windows. NatCorder comes with a rich featureset including:
NatCorder provides a simple recording API with instances of the IMediaRecorder
interface. NatCorder works by encoding video and audio frames on demand. To start recording, simply create a recorder corresponding to the media type you want to record:
var gifRecorder = new GIFRecorder(...);
var mp4Recorder = new MP4Recorder(...);
var hevcRecorder = new HEVCRecorder(...);
Once you create a recorder, you then commit frames to it. You can commit video and audio frames to these recorders. These committed frames are then encoded into a media file. When committing frames, you must provide the frame data with a corresponding timestamp. The spacing between timestamps determine the final frame rate of the recording.
NatCorder records video using pixel buffers. The pixel buffers must be 32-bit per pixel, RGBA encoding (TextureFormat.RGBA32
). The managed type of the pixel buffer is entirely flexible. As a result, you can commit a Color32[]
, a byte[]
, an int[]
, or any struct array that can be interpreted as an RGBA32
pixel buffer.
When committing a pixel buffer for encoding, you will need to provide a corresponding timestamp. For this purpose, you can use implementations of the IClock
interface. Here is an example illustrating recording a WebCamTexture
:
async void Start () {
// Start camera
var cameraTexture = new WebCamTexture();
cameraTexture.Play();
// Create a clock for generating recording timestamps
var clock = new RealtimeClock();
// Create a recorder
var recorder = new MP4Recorder(...);
// Record 150 frames
for (int i = 0; i < 150; i++) {
// Commit the frame to NatCorder for encoding
recorder.CommitFrame(cameraTexture.GetPixels32(), clock.timestamp);
// Wait for some milliseconds
await Task.Delay(10);
}
// Finish writing
var recordingPath = await recorder.FinishWriting();
}
NatCorder records audio provided as interleaved PCM sample buffers (float[]
). Similar to recording video frames, you will call the IMediaRecorder.CommitSamples
method, passing in a sample buffer and a corresponding timestamp. It is important that the timestamps synchronize with those of video, so it is recommended to use the same IClock
for generating video and audio timestamps. Below is an example illustrating recording game audio using Unity's OnAudioFilterRead
callback:
void OnAudioFilterRead (float[] sampleBuffer, int channels) {
// Commit the audio frame
recorder.CommitSamples(sampleBuffer, clock.Timestamp);
}
In most cases, you will likely just want to record a game camera optionally with game audio. To do so, you can use NatCorder's recorder Inputs
. A recorder Input
is a lightweight utility class that eases out the process of recording some aspect of a Unity application. NatCorder comes with two recorder inputs: CameraInput
and AudioInput
. You can create your own recorder inputs to do more interesting things like add a watermark to the video, or retime the video. Here is a simple example showing recording a game camera:
IClock clock;
IMediaRecorder recorder;
CameraInput cameraInput;
AudioInput audioInput;
void StartRecording () {
// Create a recording clock
clock = new RealtimeClock();
// Start recording
mediaRecorder = new ...;
// Create a camera input to record the main camera
cameraInput = new CameraInput(recorder, clock, Camera.main);
// Create an audio input to record the scene's AudioListener
audioInput = new AudioInput(recorder, clock, audioListener);
}
async void StopRecording () {
// Destroy the recording inputs
cameraInput.Dispose();
audioInput.Dispose();
// Stop recording
var recordingPath = await recorder.FinishWriting();
}
Thank you very much!