MediaEncoder
класс в UnityEditor.Media
Описание
Кодирует изображения и аудио образцы в аудио или фильм файл.
Этот класс доступен только в Unity Editor и не входит в сборки плеера.
Создание экземпляра этого класса даёт кодировщик, который сформирует аудио-, видео- или аудиовидеофайл с указанными дорожками.
Вызывайте методы AddFrame() и AddSamples() попеременно для каждой дорожки, так чтобы кадры и семплы равномерно заполняли каждую дорожку.
Как только все необходимые кадры и семплы добавлены в файл, вызовите Dispose(), чтобы закончить каждую дорожку и закрыть файл.
using UnityEditor.Media; using UnityEngine; using Unity.Collections; using System.IO;
public class Recorder { static public void RecordMovie() { var videoAttr = new VideoTrackAttributes { frameRate = new MediaRational(50), width = 320, height = 200, includeAlpha = false };
var audioAttr = new AudioTrackAttributes { sampleRate = new MediaRational(48000), channelCount = 2, language = "fr" };
int sampleFramesPerVideoFrame = audioAttr.channelCount * audioAttr.sampleRate.numerator / videoAttr.frameRate.numerator;
var encodedFilePath = Path.Combine(Path.GetTempPath(), "my_movie.mp4");
Texture2D tex = new Texture2D((int)videoAttr.width, (int)videoAttr.height, TextureFormat.RGBA32, false);
using (var encoder = new MediaEncoder(encodedFilePath, videoAttr, audioAttr)) using (var audioBuffer = new NativeArray<float>(sampleFramesPerVideoFrame, Allocator.Temp)) { for (int i = 0; i < 100; ++i) { // Fill 'tex' with the video content to be encoded into the file for this frame. // ... encoder.AddFrame(tex);
// Fill 'audioBuffer' with the audio content to be encoded into the file for this frame. // ... encoder.AddSamples(audioBuffer); } } } }
using UnityEditor.Media; using UnityEngine; using UnityEditor; using Unity.Collections; using System.IO;
public class Recorder { static public void RecordMovie() { H264EncoderAttributes h264Attr = new H264EncoderAttributes { gopSize = 25, numConsecutiveBFrames = 2, profile = VideoEncodingProfile.H264High };
var videoAttr = new VideoTrackEncoderAttributes(h264Attr) { frameRate = new MediaRational(50), width = 320, height = 200, targetBitRate = 3000000 };
var audioAttr = new AudioTrackAttributes { sampleRate = new MediaRational(48000), channelCount = 2, language = "fr" };
int sampleFramesPerVideoFrame = audioAttr.channelCount * audioAttr.sampleRate.numerator / videoAttr.frameRate.numerator;
var encodedFilePath = Path.Combine(Path.GetTempPath(), "my_movie.mp4");
Texture2D tex = new Texture2D((int)videoAttr.width, (int)videoAttr.height, TextureFormat.RGBA32, false);
using (var encoder = new MediaEncoder(encodedFilePath, videoAttr, audioAttr)) using (var audioBuffer = new NativeArray<float>(sampleFramesPerVideoFrame, Allocator.Temp)) { for (int i = 0; i < 100; ++i) { // Fill 'tex' with the video content to be encoded into the file for this frame. // ... encoder.AddFrame(tex);
// Fill 'audioBuffer' with the audio content to be encoded into the file for this frame. // ... encoder.AddSamples(audioBuffer); } } } }
Конструкторы
| Конструктор | Описание |
|---|---|
| MediaEncoder | Создайте новый кодер с различными дорожками. |
Открытые методы
| Метод | Описание |
|---|---|
| AddFrame | Добавляет кадр к видеодорожке файла. |
| AddSamples | Добавляет кадры образца к указанной аудиодорожке. |
| Dispose | Завершает запись всех треков и закрывает записываемый файл. |