Files
ADOFAIRenderer/Util/FFmpeg/FFmpegProcess.cs
T
2026-05-27 11:56:35 +09:00

72 lines
2.3 KiB
C#

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 };
public 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;
int 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<T>(NativeArray<T> arr) where T : struct
{
var val = new NativeSlice<T>(arr).SliceConvert<byte>();
var array = ArrayPool<byte>.Shared.Rent(val.Length);
val.CopyTo(array);
return array;
}
public static byte[] ToRawBytes<T>(NativeArray<T> arr) where T : struct
{
var val = new NativeSlice<T>(arr).SliceConvert<byte>();
var array = new byte[val.Length];
val.CopyTo(array);
return array;
}
}
}