Download the build files for ffmpeg, I downloaded from https://ffmpeg.zeranoe.com/builds/. I then created a folder in my root directory, "ffmpeg/windows" and "ffmpeg/unix". In the windows folder I added the exe files for "ffmpeg.exe", "ffprob.exe" and "ffplay.exe". In addition, I added the build for mac os in the unix directory.
In my ConfigureService method in Startup.cs, I registered the service as follows;
string ffmpegFilePath = null;
var osEnvironment = Environment.OSVersion;
if(osEnvironment.Platform == PlatformID.Win32NT)
{
ffmpegFilePath = Path.Combine(Environment.CurrentDirectory, "ffmpeg", "windows", "ffmpeg.exe");
}
else
{
ffmpegFilePath = Path.Combine(Environment.CurrentDirectory, "ffmpeg",
"unix", "ffmpeg");
}
if (!string.IsNullOrEmpty(ffmpegFilePath))
{
services.AddMediaToolkit(ffmpegFilePath);
}
Following how the author, AydinAdn/MediaToolkit
, implemented FfMpegTaskBase
, I created a FfTaskConvertVideo
class passing in arguments for converting video from sink to source in the constructor.
public class FfTaskConvertVideo: FfMpegTaskBase<int>
{
private readonly string _inputFilePath;
private readonly string _outputFilePath;
/// <param name="inputFilePath">Full path to the input video file.</param>
/// <param name="outputFilePath">Full path to the output video file.</param>
public FfTaskGetVideoPortion(string inputFilePath, string outputFilePath)
{
this._inputFilePath = inputFilePath;
this._outputFilePath = outputFilePath;
}
/// <summary>
/// FfTaskBase.
/// </summary>
public override IList<string> CreateArguments()
{
var arguments = new[]
{
"-i",
$@"{this._inputFilePath}",
$@"{this._outputFilePath}"
};
return arguments;
}
/// <summary>
/// FfTaskBase.
/// </summary>
public override async Task<int> ExecuteCommandAsync(IFfProcess ffProcess)
{
await ffProcess.Task;
return 0;
}
}
//Then we use this way
var convertTask = new FfTaskConvertVideo("input.mp4", "output.ogg");
//using the injected IMediaToolkitService as _media
await _media.ExecuteAsync(convertTask);
References:
https://opensource.com/article/17/6/ffmpeg-convert-media-file-formats - Used this to know the args for converting