c# ffmpeg视频转换

c#  ffmpeg视频转换

什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了!

http://baike.baidu.com/link?url=bD4xX59DgppfdclpyGfmC38WTeEkOTM9yc35XGi4OLnQYY7BqmPJSIZHyPKp0imUdI2yujK76Nm047ACZW2hHa

 

  今天工作中遇到这么个问题,这个问题很屌,我通过搜索引擎找了个遍,几乎没有找到理想的答案,于是被逼无奈之下,自己研究参数,于是就有了如下的成就,所以,遇到问题不要害怕,这才是成长最快的时刻。当问题解决了的时候,会有种无比的成就感,把自己的成果分享给他人,供他人参考,这也是种喜悦!

                                                                                                                          

                                                                                                                                                                                                                              ------------没有做不到的,只有想不到的。

 转码进度公式=当前时间/总时间*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:张立平资料DemoFFmpeg_Demoffmpegffmpeg.exe";
 8             p.StartInfo.UseShellExecute = false;
 9             string srcFileName = @"E:张立平资料DemoFFmpeg_Demovideoold	est.mov";
10             string destFileName = @"E:张立平资料DemoFFmpeg_Demovideonew	est.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     }
View Code

 

 

 

 

原文地址:https://www.cnblogs.com/zlp520/p/4238097.html