c# ffmpeg視頻轉換
什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,帶着問題去找答案吧!先參考百度百科把,我覺得它很強大無奇不有,為了方便大家我就把鏈接提供了!
今天工作中遇到這么個問題,這個問題很屌,我通過搜索引擎找了個遍,幾乎沒有找到理想的答案,於是被逼無奈之下,自己研究參數,於是就有了如下的成就,所以,遇到問題不要害怕,這才是成長最快的時刻。當問題解決了的時候,會有種無比的成就感,把自己的成果分享給他人,供他人參考,這也是種喜悅!
------------沒有做不到的,只有想不到的。
轉碼進度公式=當前時間/總時間*100%;

1 public class FFmpeg 2 { 3 static int timeCount; 4 public static void Execute() 5 { 6 Process p = new Process(); 7 p.StartInfo.FileName = @"E:\張立平資料\Demo\FFmpeg_Demo\ffmpeg\ffmpeg.exe"; 8 p.StartInfo.UseShellExecute = false; 9 string srcFileName = @"E:\張立平資料\Demo\FFmpeg_Demo\videoold\test.mov"; 10 string destFileName = @"E:\張立平資料\Demo\FFmpeg_Demo\videonew\test.mp4"; 11 p.StartInfo.Arguments = "-i " + srcFileName + " -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 " + destFileName; //執行參數 12 //-i D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06.mov -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06_mov_stream.mp4 13 14 p.StartInfo.RedirectStandardInput = true; 15 p.StartInfo.RedirectStandardOutput = true; 16 p.StartInfo.RedirectStandardError = true;//把外部程序錯誤輸出寫到StandardError流中 17 p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 18 p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 19 20 using (p) 21 { 22 p.Start(); 23 p.BeginErrorReadLine();//開始異步讀取 24 p.WaitForExit();//阻塞等待進程結束 25 p.Close();//關閉進程 26 } 27 } 28 29 private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) 30 { 31 32 var output = e.Data; 33 if (output != null) 34 { 35 if (output.Contains("Duration")) 36 { 37 Int32 indexOfDuration = output.IndexOf("Duration"); 38 Int32 indexOfBegin = output.IndexOf(":", indexOfDuration); 39 Int32 indexOfEnd = output.IndexOf(",", indexOfBegin); 40 var duration = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); 41 timeCount =ConvertStringtoSecond(duration); 42 } 43 if (output.Contains("time=")) 44 { 45 Int32 indexOfTime = output.IndexOf("time="); 46 Int32 indexOfBegin = output.IndexOf("=", indexOfTime); 47 Int32 indexOfEnd = output.IndexOf("bitrate", indexOfBegin); 48 string timeStr = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); 49 var time =Convert.ToDouble(timeStr); 50 var process = time / timeCount*100; 51 process = Math.Round(process); 52 Console.Write("{0}% ", process); 53 } 54 //Console.WriteLine(e.Data); 55 } 56 } 57 58 private static int ConvertStringtoSecond(string input) 59 { 60 int totalSecond = 0; 61 try 62 { 63 string[] split = input.Split(new char[] { ':', '.' }); 64 int hour = int.Parse(split[0]) * 3600; 65 int min = int.Parse(split[1]) * 60; 66 int second = int.Parse(split[2]); 67 int millisecond = int.Parse(split[3]) / 1000; 68 totalSecond = hour + min + second + millisecond; 69 } 70 catch (System.Exception ex) 71 { 72 Console.Write(ex.Message); 73 74 } 75 return totalSecond; 76 } 77 78 private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 79 { 80 Console.WriteLine(e.Data); 81 } 82 }