using System; using System.Buffers; using System.Diagnostics; using System.IO; using Unity.Collections; namespace ADOFAIRenderer.Util.FFmpeg { public class FFmpegProcess { public static string FFmpegPath = ""; private Process? _ffmpegProcess; private Stream? _inputStream; public bool IsEnd => _ffmpegProcess is { HasExited: true }; protected void Execute(string command) { if (string.IsNullOrEmpty(command)) { throw new ArgumentException("Command is empty"); } if (string.IsNullOrEmpty(FFmpegPath) || !File.Exists(FFmpegPath)) { throw new FileNotFoundException($"ffmpeg binary not found at '{FFmpegPath}'"); } _ffmpegProcess = new Process(); _ffmpegProcess.StartInfo.FileName = FFmpegPath; _ffmpegProcess.StartInfo.Arguments = command; _ffmpegProcess.StartInfo.UseShellExecute = false; _ffmpegProcess.StartInfo.RedirectStandardInput = true; _ffmpegProcess.StartInfo.CreateNoWindow = true; _ffmpegProcess.Start(); _inputStream = _ffmpegProcess.StandardInput.BaseStream; } public void SendDataFromBuffer(byte[] buffer, int length = -1) { if (_inputStream == null) return; var writeLength = length < 0 ? buffer.Length : length; _inputStream.Write(buffer, 0, writeLength); _inputStream.Flush(); } public void Close() { if (_ffmpegProcess == null || _ffmpegProcess.HasExited || _inputStream == null) return; _inputStream.Flush(); _inputStream.Close(); _ffmpegProcess.WaitForExit(); } public static byte[] RentRawBytes(int size) { var array = ArrayPool.Shared.Rent(size); return array; } /*public static byte[] RentRawBytes(NativeArray arr) where T : struct { var val = new NativeSlice(arr).SliceConvert(); var array = ArrayPool.Shared.Rent(val.Length); val.CopyTo(array); return array; }*/ protected static byte[] ToRawBytes(NativeArray arr) where T : struct { var val = new NativeSlice(arr).SliceConvert(); var array = new byte[val.Length]; val.CopyTo(array); return array; } } }