63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using System.Diagnostics;
|
|
using BlazorApp1.Handler.Interfaces;
|
|
using BlazorApp1.Helpers;
|
|
using BlazorApp1.Models;
|
|
|
|
namespace BlazorApp1.Handler;
|
|
|
|
public class VideoHandler : IVideoHandler
|
|
{
|
|
private readonly string _finalPath;
|
|
private const string FILTER = "minterpolate";
|
|
private const string CODECH265 = "libx265";
|
|
private const string CODECH264 = "libx264";
|
|
private const string CODECVP9 = "";
|
|
private const string CONTAINERMP4 = "mp4";
|
|
private const string CONTAINERWEBM = "webm";
|
|
|
|
public VideoHandler()
|
|
{
|
|
_finalPath = Directory.GetCurrentDirectory() + "/wwwroot/Videos/Final/";
|
|
}
|
|
|
|
public bool ConvertVideo(string filePath, FFOptionsCodecs codecs, FFOptionsFilters filters, out string newFilePath)
|
|
{
|
|
string codec = ChooseCodec(codecs);
|
|
string container = ChooseContainer(codecs);
|
|
|
|
newFilePath = _finalPath + filePath.GetFileNameFromPath().RemoveFileEnding() + "_Converted" + "." + container;
|
|
|
|
ProcessStartInfo startInfo = new()
|
|
{
|
|
FileName = "ffmpeg",
|
|
Arguments = "-i " + filePath + " -c:v " + codec + " " + newFilePath,
|
|
CreateNoWindow = true,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
};
|
|
|
|
using Process lol = Process.Start(startInfo)!;
|
|
lol.WaitForExit();
|
|
return lol.Responding;
|
|
}
|
|
|
|
private string ChooseCodec(FFOptionsCodecs codecs)
|
|
{
|
|
return codecs switch
|
|
{
|
|
FFOptionsCodecs.H264 => CODECH264,
|
|
FFOptionsCodecs.H265 => CODECH265,
|
|
FFOptionsCodecs.VP9 => CODECVP9,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(codecs), codecs, null)
|
|
};
|
|
}
|
|
|
|
private string ChooseContainer(FFOptionsCodecs codecs)
|
|
{
|
|
return codecs switch
|
|
{
|
|
FFOptionsCodecs.VP9 => CODECVP9,
|
|
_ => CONTAINERMP4
|
|
};
|
|
}
|
|
} |