c# ffmpeg视频转换
什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了!
今天工作中遇到这么个问题,这个问题很屌,我通过搜索引擎找了个遍,几乎没有找到理想的答案,于是被逼无奈之下,自己研究参数,于是就有了如下的成就,所以,遇到问题不要害怕,这才是成长最快的时刻。当问题解决了的时候,会有种无比的成就感,把自己的成果分享给他人,供他人参考,这也是种喜悦!
------------没有做不到的,只有想不到的。
转码进度公式=当前时间/总时间*100%;
public class FFmpeg { static int timeCount; public static void Execute() { Process p = new Process(); p.StartInfo.FileName = @"E:\张立平资料\Demo\FFmpeg_Demo\ffmpeg\ffmpeg.exe"; p.StartInfo.UseShellExecute = false; string srcFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videoold\test.mov"; string destFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videonew\test.mp4"; p.StartInfo.Arguments = "-i " + srcFileName + " -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 " + destFileName; //执行参数 //-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 p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中 p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); using (p) { p.Start(); p.BeginErrorReadLine();//开始异步读取 p.WaitForExit();//阻塞等待进程结束 p.Close();//关闭进程 } } private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) { var output = e.Data; if (output != null) { if (output.Contains("Duration")) { Int32 indexOfDuration = output.IndexOf("Duration"); Int32 indexOfBegin = output.IndexOf(":", indexOfDuration); Int32 indexOfEnd = output.IndexOf(",", indexOfBegin); var duration = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); timeCount =ConvertStringtoSecond(duration); } if (output.Contains("time=")) { Int32 indexOfTime = output.IndexOf("time="); Int32 indexOfBegin = output.IndexOf("=", indexOfTime); Int32 indexOfEnd = output.IndexOf("bitrate", indexOfBegin); string timeStr = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); var time =Convert.ToDouble(timeStr); var process = time / timeCount*100; process = Math.Round(process); Console.Write("{0}% ", process); } //Console.WriteLine(e.Data); } } private static int ConvertStringtoSecond(string input) { int totalSecond = 0; try { string[] split = input.Split(new char[] { ':', '.' }); int hour = int.Parse(split[0]) * 3600; int min = int.Parse(split[1]) * 60; int second = int.Parse(split[2]); int millisecond = int.Parse(split[3]) / 1000; totalSecond = hour + min + second + millisecond; } catch (System.Exception ex) { Console.Write(ex.Message); } return totalSecond; } private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); } }
转载来源于:http://www.cnblogs.com/zlp520/