[Unity]Record Video on iOS
VideoCreator Asset helps to implement the recording of wav and mov files in your Unity app.
Support .mov, .wav, Live Photos, .png and .jpeg.
Example
Example repository↓
https://github.com/fuziki/VideoCreator
In the scene of the Unity sample project, there are two cameras placed.
One is the Main Camera, and the other is the Recording Camera.
The video from the main camera is displayed on the screen.
Record the video from the recording camera and the audio from the microphone as a .mov file and save it to the Photos app.

How it works

When tap the stop recoding
button, the Recording Camera video will be saved to the album.
Installation
Copy Examples/UnityExample/Assets/VideoCreator
to your project
Usage
Setup MediaCreator for mov file (no audio)
Set the tmp file location as absolute path and codec.
If you want to record a video with an alpha channel, you need to specify the “hevcWithAlpha”.
string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", width, height);
Setup MediaCreator for mov file (with audio)
In addition to no audio, set the number of channels and sampling rate for audio.
string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", texture.width, texture.height, 1, 48_000);
Setup MediaCreator for Live Photos
In addition to the usual mov, set Content Identifier.
string uuid = System.Guid.NewGuid().ToString();
string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", width, height, uuid);
Setup MediaCreator for wav file
In addition to the number of audio channels and sampling rate, set the Bit Depth.
string cachePath = "file://" + Application.temporaryCachePath + "/tmp.wav";
MediaCreator.InitAsWav(cachePath, 1, 48000, 32);
Start Recording
Set a start time in the timeline of the source samples.
The time unit is microseconds.
long startTimeOffset = 0;
MediaCreator.Start(startTimeOffset);
Write Texture
Give a time based on start and any texture.
The time unit is microseconds.
Texture texture = Get Texture;
long time = startTimeOffset + Elapsed time from Start;
MediaCreator.WriteVideo(texture, time);
In the example project, we have created a RenderTexture and set it as the TargetTexture of the Recording Camera.
We also set this RenderTexture as a property of the SerializeField.

Write Audio PCM
Give a time based on start and pcm float array.
The time unit is microseconds.
float[] pcm = Get PCM float array;
long time = startTimeOffset + Elapsed time from Start;
MediaCreator.WriteAudio(pcm, time);
Add an AudioSource, set the Microphone clip, and play.
var source = gameObject.AddComponent<AudioSource>();
var clip = Microphone.Start(null, true, 1, 48_000);
source.clip = clip;
source.loop = true;
while (Microphone.GetPosition(null) < 0) { }
source.Play();
Using the OnAudioFilterRead function, get and write a float array of pcm.
void OnAudioFilterRead(float[] data, int channels)
{
writeAudio(data, channels);
for (int i = 0; i < data.Length; i++)
{
data[i] = 0;
}
}
Finish Recording
Save recording files synchronously.
This process may take some time.
MediaCreator.FinishSync();
Save mov to album app
If you want to save your recorded videos to an album, you can use MediaSaver to do so.
MediaSaver.SaveVideo(cachePath);
Save Live Photos to album app
If you want to save your recorded Live Photos to an album, you can use MediaSaver to do so.
Set the thumbnail and the same Content Identifier as the video.
MediaSaver.SaveLivePhotos(texture, uuid, cachePath);
Save Texture to album app as picture
You can choose the format from “jpeg”, “jpg”, “heif”, and “png”.
MediaSaver.SaveImage(texture, "png");