视频转成flv格式

最近完成了这个小Demo,来分享一下!
上面给了我两天时间,来完成这个小功能
于时我花了半天时间从网络上到处鄱资料,又花了半天时间调试代码,成功之后,终于有了以下一点的经验之谈:

这里讲一下重要的:
1.用到两个工具,一个是ffmpeg.exe,另一个是mencoder.exe
ffmpeg最新版本的下载地址:http://ffdshow.faireal.net/mirror/ffmpeg/
Mencoder新版本的下载地址:http://www5.mplayerhq.hu/MPlayer/releases/win32/

这里有一个重点,网上的文章都没讲到,所以造成有些人运行后没反应,原因是上面路径的下载,有很多版本,不同的版本可能个别参数不同,而网上的文章所用的参数都是用很早的版本写的,所以会造成运行后因参数错误而没有效果
简单处理是:把网上参数在cmd命令行执行一下,这时命令行会报哪个参数错误,把它删了即可!

2.判断处理成功与失败或是进度是否完成,从异步获取的输出信息判断[包括获取原视频的宽与高]
这里重点在两个委托事件中,详情见以下几行代码

Code
 1 
 2  private  void StartProcess(string tool)
 3         {
 4             StartProcess(tool, false);
 5         }
 6         private  void StartProcess(string tool,bool onlyCheckInfo)
 7         {
 8             System.Diagnostics.Process p = new System.Diagnostics.Process();
 9             p.StartInfo.FileName = tool;
10             p.StartInfo.Arguments = commandPara;
11             p.StartInfo.UseShellExecute = false;
12             p.StartInfo.RedirectStandardInput = true;
13             p.StartInfo.RedirectStandardOutput = true;
14             p.StartInfo.RedirectStandardError = true;
15             p.StartInfo.CreateNoWindow = false;
16             p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_OutputDataReceived);
17             if (onlyCheckInfo)//只检测文件是否可转换与获到内部宽与高
18             {
19                 p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_CheckInfoDataReceived);
20             }
21             else
22             {
23                 p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_ErrorDataReceived);
24             }
25             //开始执行 
26             try
27             {
28                 p.Start();
29                 p.BeginOutputReadLine();
30                 p.BeginErrorReadLine();
31                 p.WaitForExit();
32             }
33             catch (Exception err)
34             {
35                 Console.WriteLine(err.Message);
36             }
37             finally
38             {
39                 p.Close();
40             }
41         }
42         void p_CheckInfoDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
43         {
44             if (!string.IsNullOrEmpty(e.Data))
45             {
46                 if (e.Data.Contains("Stream"&& e.Data.Contains("Video:"))//设置原视频窗口大小作为flv视频的宽与高
47                 {
48                     Match match = Regex.Match(e.Data, @", (\d+)x(\d+)");
49                     if (match != null)
50                     {
51                         videoWidth = match.Groups[1].Value;
52                         videoHeight = match.Groups[2].Value;
53                     }
54                 }
55                 else if (e.Data.Contains("could not find codec parameters"))//ffmpeg转换失败
56                 {
57                     isCanChangeToFlv = false;
58                     Program.SetDataBase(id, 1, count + 1);
59                 }
60             }
61 
62         }
63 
64          void p_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
65         {
66             if (!string.IsNullOrEmpty(e.Data))
67             {
68                 if (e.Data.Contains("video:"&& e.Data.Contains("muxing overhead"))//ffmpeg转换完成
69                 {
70                     Program.SetDataBase(id, 2, count + 1);
71                     Console.WriteLine("转换完成");
72                 }
73                 Console.WriteLine(e.Data);
74             }
75             
76         }
77 
78          void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
79         {
80             if (!string.IsNullOrEmpty(e.Data))
81             {
82                 if (e.Data.Contains("Writing index"))//mencoder转换完成
83                 {
84                     Program.SetDataBase(id, 2, count + 1);
85                     Console.WriteLine("转换完成");
86                 }
87                 //else if (e.Data.Contains("Exiting"))//mencoder转换失败
88                 //{
89                 //    Console.WriteLine("转换失败");
90                 //}
91                 Console.WriteLine(e.Data);
92             }
93         }
94 
95 


本文只讲重点,请结合网络其它文章与本文即可!

转载于:https://my.oschina.net/secyaher/blog/274107

猜你喜欢

转载自blog.csdn.net/weixin_34010949/article/details/91967094