天天看點

c#中調用Ffmpeg轉換視訊格式的問題

我的目的是調用ffmpeg轉化.avs檔案為.flv檔案,參數這些是沒有問題的,我在指令行裡面運作可以成功轉換視訊格式. 現在我把它寫成一個函數在程式裡面調用,就出問題了:(

public int ConvertVideoToFLV()

{

int re = 999;

Process p = new Process();//建立外部調用線程

p.StartInfo.FileName = @"f:\flv\ffmpeg.rev10464\ffmpeg.exe";//調用外部程式的絕對路徑

p.StartInfo.Arguments = "-y -i sss.avs -ab 56 -b 500k programout.flv";//參數(FFMPEG參數)

p.StartInfo.UseShellExecute = false;//不使用作業系統外殼程式啟動線程

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;//把外部程式錯誤輸出寫到StandardError流中

p.StartInfo.CreateNoWindow = false;//不建立程序視窗

p.ErrorDataReceived = new DataReceivedEventHandler(Output);//FFMPEG輸出流時候把流的處理過程轉移到Output

p.Start();//啟動線程

p.BeginErrorReadLine();//開始異步讀取

p.WaitForExit();//阻塞等待程序結束

p.Close();//關閉程序

p.Dispose();//釋放資源

return re;

}

調試以後發現程式運作很正常,但就是不進行實際的轉換. 我對程序使用也不是很熟悉, 請高人指點一下哪裡出了問題!

網友回複:你沒有使用作業系統外殼程式啟動,我想是這樣的,你設定成使用作業系統外殼程式啟動程式

可能是轉換過程需要視訊解碼之類什麼的!!

網友回複:幫LZ頂

網友回複:“/”應用程式中的伺服器錯誤。

要重定向 IO 流,Process 對象必須将 UseShellExecute 屬性設定為 False。

說明: 執行目前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤資訊,以了解有關該錯誤以及代碼中導緻錯誤的出處的具體資訊。

異常具體資訊: System.InvalidOperationException: 要重定向 IO 流,Process 對象必須将 UseShellExecute 屬性設定為 False。

設定成true以後會報錯成這樣...

網友回複:我檢視了程序,發現函數調用的時候ffmpeg.exe程序已經運作,但是cpu顯示為0,根本沒有執行轉換. 高手看看是什麼原因引起的,多謝!

網友回複:解決了. 是需要設定一個工作目錄~ 結貼

網友回複:public static string VideoConvertFlv(string FromName, string WidthAndHeight, string ExportName)

{

string ffmpeg = HttpContext.Current.Server.MapPath("~/FLV/ffmpeg.exe");

string Command =" -i " FromName " -y -ab 56 -ar 22050 -b 500 -r 15 -s " WidthAndHeight " " ExportName; //Flv格式

//string Command = "E:\\FFmpeg\\ffmpeg.exe -i E:\\ClibDemo\\VideoPath\\admin\\a.wmv -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 " ExportName;

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = ffmpeg;

p.StartInfo.Arguments = Command;

p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/FLV/");

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.CreateNoWindow = false;

//開始執行

p.Start();

p.BeginErrorReadLine();

p.WaitForExit();

p.Close();

p.Dispose();

//p.StandardInput.WriteLine(Command);

//p.StandardInput.WriteLine("Exit ");

return ExportName;

}

protected void Button1_Click(object sender, EventArgs e)

{

strFile = HttpContext.Current.Server.MapPath("~/FLV/祝福勁牌(大碟版).mpg"); ;

VideoConvertFlv(strFile, "480*360", "zh.flv");

VideoConvertImg(strFile, "480*360", "zh.jpg");

}

http://hi.baidu.com/honfei/blog/item/d636cf4eabf0000cb2de0574.html

繼續閱讀