2

I built a WPF app that uses System.Diagnostics.Process to run ffmpeg.exe. In there I added an "External" folder to hold the ffmpeg files.

enter image description here

Then just did something like

new ProcessStartInfo()
{
    FileName = @"External\ffmpeg\bin\x64\ffmpeg.exe",
    ...
}

Which worked just fine.

Now I want to port this project to .Net Maui to leverage its cross-platformness so my app can be run on PC or Mac.

My question is where do I add the ffmpeg files in the Maui project? Do I Just put them in "Resources/Raw" and do some kind of check in my code to see which version use; do I make a "Resources" folder in "Platforms/Windows" and "Platforms/MacCatalyst" and add the appropriate files there?

Side Note: I know almost nothing about Mac.

master_ruko
  • 629
  • 1
  • 4
  • 22
  • Since you want to target both Windows and Mac, it would be a better approach to use ffmpeg as a library instead of bundling an unsigned executable: https://stackoverflow.com/questions/2401764/can-ffmpeg-be-used-as-a-library-instead-of-a-standalone-program – r2d2rigo Apr 27 '22 at 16:45
  • @r2d2rigo I considered that when I first started playing with ffmpeg when I started my WPF project, but was unable to work out how to use the ffmpeg libraries (libavcodec and libavformat, from what I gather) in a c# project. That is why I settled on running ffmpeg.exe and ffprobe.exe through a process. – master_ruko May 01 '22 at 20:50

1 Answers1

0

I wanted to do something similar, (in my case, run SQL scripts) and at least under Android, I needed to make the scripts embedded resources, and copy them to the app directory at runtime to actually work with the files (or executables).

Here's what I did.

private bool CopyDbScriptsToAppData()
{
    string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    if (assemblyDirectory == null) return false;
    var assembly = Assembly.LoadFrom(Path.Combine(assemblyDirectory, "<assembly>.dll"));
    string prefix = "<assembly>.Resources.Raw.";
    foreach (var res in assembly.GetManifestResourceNames())
    {
        if (res.StartsWith(prefix))
        {
            Stream stream = assembly.GetManifestResourceStream(res);
            if (stream == null)
            {
                _logger.Warning("Could not write resource:{0} to app data.", res);
                continue;
            }
            string fullPath = ResourcePathHelper.PathNameFromResourceName(prefix, res);
            fullPath = Path.Combine(_dbPath, fullPath);
            if (!File.Exists(fullPath))
            {
                string dirName = Path.GetDirectoryName(fullPath);
                if (dirName != null)
                    Directory.CreateDirectory(dirName);
            }
            using FileStream fs = File.OpenWrite(fullPath);
            stream.CopyTo(fs);
            fs.Close();
        }
    }
    return true;
}
ericnev
  • 1
  • 1
  • I think ```string.isNullOrEmpty(stringVar)``` is prefered over ```stringVar == null``` – master_ruko Sep 26 '22 at 17:40
  • @master_ruko - agreed. Those checks were put in place to please Resharper, but in general, totally safer to use IsNullOrEmpty. – ericnev Oct 04 '22 at 16:54