3

I'm using MediaToolkit.NetCore which is in an early stage in order to convert videos into mp4 format but I have not been able to get it to work on ASP Core 2.

With MeidaToolkit.NetCore I tried this:

var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};

var outputFile = new MediaFile {Filename = 
@"C:\Path\To_Save_New_Video.mp4"};

using (var engine = new Engine())

{ engine.Convert(inputFile, outputFile);}

But the problem is that ffmpeg.exe is not embedded in library binaries so I get an error on new Engine(). In order to fix this, you have to pass a path to ffmpeg.exe explicitly in the constructor, which I don't know how to do.

How can I pass the ffmpeg.exe in the constructor above?

AlTheSwede
  • 59
  • 6

2 Answers2

3

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

2

You can pass it to the constructor of Engine class:

using (var engine = new Engine(@"D:\MediaToolkit\ffmpeg.exe"))
Nemanja Stolic
  • 141
  • 1
  • 4