2012-06-26 54 views
2

我想在C#中使用FFMPEG從視頻文件中去除音頻。我知道做這樣的操作的代碼是什麼(據我所知),但我並不是100%確定需要將ffmpe.exe文件保存在我的項目中以及如何訪問它。 到目前爲止我的代碼如下:在C#中使用FFMPEG#

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); 
     } 
    }` 

如果任何人有任何想法,請讓我知道。任何幫助!

回答

3

您可以使用任何您想要的絕對或相對路徑。

但我會建議針對相對路徑,以防「當前目錄」發生變化。

在WinForms下,您可以使用ExecutablePath並將exe放入您自己的Bin文件夾中。

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

對於控制檯應用程序,我找不到這樣一種簡單的方法來獲取Exe路徑。

+0

['Application.ExecutablePath'](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx )返回「_啓動應用程序的可執行文件的路徑,包括可執行文件的名稱。」所以這將返回類似'... \ bin \ MyApp.EXE \ ffmpeg.exe'這將無效。 –

+0

@MichaelMinton - 你說得對,我會編輯。 –

+0

感謝您的幫助!我很感激! –

0

您可以將ffmpeg.exe目錄添加到您的解決方案。設置其Build ActionContent並設置Copy to Output DirectoryCopy always像這樣:

enter image description here

現在,這將確保它存在於你的bin目錄旁邊的可執行文件。然後,你可以修改你的方法,像這樣:

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); 
    }