2

I am trying to strip audio from a video file using FFMPEG in C#. I know what the code is for doing such an operation (as far as I know) but I am not 100% sure where I need to keep the ffmpeg.exe file in my project and how to access it.

My code so far is as follows:

public void stripAudioTest(string videoFilename, ExportProgressWindow callback, string destinationAudioFile)
{
    string FFMPEG_PATH = "*************"; //not sure what to put here?????
    string strParam = " -i " + videoFileName + " -ab 160k -ar 44100 -f wav -vn " +   destinationAudioFile;
    process(FFMPEG_PATH, strParam);
    callback.Increment(100); 
}

public void process(string Path_FFMPEG, string strParam)
{
    try
    {
        Process ffmpeg = new Process();
        
        ffmpeg.StartInfo.UseShellExecute = false;
        ffmpeg.StartInfo.RedirectStandardOutput = true;
        ffmpeg.StartInfo.FileName = Path_FFMPEG;
        ffmpeg.StartInfo.Arguments = strParam;
        ffmpeg.Start();
        ffmpeg.WaitForExit();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

If anyone has any ideas please let me know. Anything helps!

2 Answers 2

3

You can use any absolute or relative path you want.

But I would advice against a relative path, just in case the 'Current Directory' changes.

Under WinForms you can use the ExecutablePath and put the exe in your own Bin folder.

 // winforms
 string FFMPEG_PATH = Path.Combine(
      Path.GetDirectoryName( Application.ExecutablePath), 
      "ffmpeg.exe");

For a Console App I couldn't find such an easy way to get the Exe path.

Sign up to request clarification or add additional context in comments.

1 Comment

Application.ExecutablePath returns "the path for the executable file that started the application, including the executable name." So this will return something like ...\bin\MyApp.EXE\ffmpeg.exe which won't be valid.
0

You can add ffmpeg.exe directory to your solution. Set its Build Action to Content and set Copy to Output Directory to Copy always like so:

enter image description here

Now this will ensure it exists in your bin directory alongside your executable. You could then modify your method like so:

    public void stripAudioTest(string videoFilename, ExportProgressWindow callback, string destinationAudioFile)
    {
        var appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
        var FFMPEG_PATH = Path.Combine(appDirectory, "ffmpeg.exe");
        if (!File.Exists(FFMPEG_PATH))
        {
            MessageBox.Show("Cannot find ffmpeg.exe.");
            return;
        }

        string strParam = " -i " + videoFilename + " -ab 160k -ar 44100 -f wav -vn " + destinationAudioFile;
        process(FFMPEG_PATH, strParam);
        callback.Increment(100);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.