C#调用mmpeg进行各种视频转换的类实例

本文实例讲述了C#调用mmpeg进行各种视频转换的类。分享给大家供大家参考。具体如下:

这个C#类封装了视频转换所需的各种方法,基本上是围绕着如何通过mmpeg工具来进行视频转换

 

  1 using System.Web;
  2 using System.Configuration;
  3 namespace DotNet.Utilities
  4 {
  5   //if (this.fload.HasFile)
  6   //{
  7   //  string upFileName = HttpContext.Current.Server.MapPath("~/savefile") + "\\" + this.fload.PostedFile.FileName;
  8   //  string saveName  = DateTime.Now.ToString("yyyyMMddHHmmssffff");
  9   //  string playFile  = Server.MapPath(VideoConvert.savefile + saveName);
 10   //  string imgFile  = Server.MapPath(VideoConvert.savefile + saveName);
 11   //  VideoConvert pm = new VideoConvert();
 12   //  string m_strExtension = VideoConvert.GetExtension(this.fload.PostedFile.FileName).ToLower();
 13   //  if (m_strExtension == "flv")
 14   //  {
 15   //    System.IO.File.Copy(upFileName, playFile + ".flv");
 16   //    pm.CatchImg(upFileName, imgFile);
 17   //  }
 18   //  string Extension = pm.CheckExtension(m_strExtension);
 19   //  if (Extension == "ffmpeg")
 20   //  {
 21   //    pm.ChangeFilePhy(upFileName, playFile, imgFile);
 22   //  }
 23   //  else if (Extension == "mencoder")
 24   //  {
 25   //    pm.MChangeFilePhy(upFileName, playFile, imgFile);
 26   //  }
 27   //}
 28   public class VideoConvert : System.Web.UI.Page
 29   {
 30     public VideoConvert()
 31     { }
 32     string[] strArrMencoder = new string[] { "wmv", "rmvb", "rm" };
 33     string[] strArrFfmpeg = new string[] { "asf", "avi", "mpg", "3gp", "mov" };
 34     #region 配置
 35     public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];
 36     public static string mencodertool = ConfigurationManager.AppSettings["mencoder"];
 37     public static string savefile = ConfigurationManager.AppSettings["savefile"] + "/";
 38     public static string sizeOfImg = ConfigurationManager.AppSettings["CatchFlvImgSize"];
 39     public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
 40     public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
 41     #endregion
 42     #region 获取文件的名字
 43     /// <summary>
 44     /// 获取文件的名字
 45     /// </summary>
 46     public static string GetFileName(string fileName)
 47     {
 48       int i = fileName.LastIndexOf("\\") + 1;
 49       string Name = fileName.Substring(i);
 50       return Name;
 51     }
 52     #endregion
 53     #region 获取文件扩展名
 54     /// <summary>
 55     /// 获取文件扩展名
 56     /// </summary>
 57     public static string GetExtension(string fileName)
 58     {
 59       int i = fileName.LastIndexOf(".") + 1;
 60       string Name = fileName.Substring(i);
 61       return Name;
 62     }
 63     #endregion
 64     #region 获取文件类型
 65     /// <summary>
 66     /// 获取文件类型
 67     /// </summary>
 68     public string CheckExtension(string extension)
 69     {
 70       string m_strReturn = "";
 71       foreach (string var in this.strArrFfmpeg)
 72       {
 73         if (var == extension)
 74         {
 75           m_strReturn = "ffmpeg"; break;
 76         }
 77       }
 78       if (m_strReturn == "")
 79       {
 80         foreach (string var in strArrMencoder)
 81         {
 82           if (var == extension)
 83           {
 84             m_strReturn = "mencoder"; break;
 85           }
 86         }
 87       }
 88       return m_strReturn;
 89     }
 90     #endregion
 91     #region 视频格式转为Flv
 92     /// <summary>
 93     /// 视频格式转为Flv
 94     /// </summary>
 95     /// <param name="vFileName">原视频文件地址</param>
 96     /// <param name="ExportName">生成后的Flv文件地址</param>
 97     public bool ConvertFlv(string vFileName, string ExportName)
 98     {
 99       if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName))))
100       {
101         return false;
102       }
103       vFileName = HttpContext.Current.Server.MapPath(vFileName);
104       ExportName = HttpContext.Current.Server.MapPath(ExportName);
105       string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\""; //Flv格式  
106       System.Diagnostics.Process p = new System.Diagnostics.Process();
107       p.StartInfo.FileName = ffmpegtool;
108       p.StartInfo.Arguments = Command;
109       p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/tools/");
110       p.StartInfo.UseShellExecute = false;
111       p.StartInfo.RedirectStandardInput = true;
112       p.StartInfo.RedirectStandardOutput = true;
113       p.StartInfo.RedirectStandardError = true;
114       p.StartInfo.CreateNoWindow = false;
115       p.Start();
116       p.BeginErrorReadLine();
117       p.WaitForExit();
118       p.Close();
119       p.Dispose();
120       return true;
121     }
122     #endregion
123     #region 生成Flv视频的缩略图
124     /// <summary>
125     /// 生成Flv视频的缩略图
126     /// </summary>
127     /// <param name="vFileName">视频文件地址</param>
128     public string CatchImg(string vFileName)
129     {
130       if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) return "";
131       try
132       {
133         string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg";
134         string Command = " -i " + HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + HttpContext.Current.Server.MapPath(flv_img_p);
135         System.Diagnostics.Process p = new System.Diagnostics.Process();
136         p.StartInfo.FileName = ffmpegtool;
137         p.StartInfo.Arguments = Command;
138         p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
139         try
140         {
141           p.Start();
142         }
143         catch
144         {
145           return "";
146         }
147         finally
148         {
149           p.Close();
150           p.Dispose();
151         }
152         System.Threading.Thread.Sleep(4000);
153         //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
154         if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p)))
155         {
156           return flv_img_p;
157         }
158         return "";
159       }
160       catch
161       {
162         return "";
163       }
164     }
165     #endregion
166     #region 运行FFMpeg的视频解码(绝对路径)
167     /// <summary>
168     /// 转换文件并保存在指定文件夹下
169     /// </summary>
170     /// <param name="fileName">上传视频文件的路径(原文件)</param>
171     /// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
172     /// <param name="imgFile">从视频文件中抓取的图片路径</param>
173     /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns>
174     public string ChangeFilePhy(string fileName, string playFile, string imgFile)
175     {
176       string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
177       if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
178       {
179         return "";
180       }
181       string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
182       string FlvImgSize = VideoConvert.sizeOfImg;
183       System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
184       FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
185       FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
186       try
187       {
188         System.Diagnostics.Process.Start(FilestartInfo);//转换
189         CatchImg(fileName, imgFile); //截图
190       }
191       catch
192       {
193         return "";
194       }
195       return "";
196     }
197     public string CatchImg(string fileName, string imgFile)
198     {
199       string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
200       string flv_img = imgFile + ".jpg";
201       string FlvImgSize = VideoConvert.sizeOfImg;
202       System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
203       ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
204       ImgstartInfo.Arguments = "  -i  " + fileName + " -y -f image2  -ss 2 -vframes 1 -s  " + FlvImgSize + "  " + flv_img;
205       try
206       {
207         System.Diagnostics.Process.Start(ImgstartInfo);
208       }
209       catch
210       {
211         return "";
212       }
213       if (System.IO.File.Exists(flv_img))
214       {
215         return flv_img;
216       }
217       return "";
218     }
219     #endregion
220     #region 运行FFMpeg的视频解码(相对路径)
221     /// <summary>
222     /// 转换文件并保存在指定文件夹下
223     /// </summary>
224     /// <param name="fileName">上传视频文件的路径(原文件)</param>
225     /// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
226     /// <param name="imgFile">从视频文件中抓取的图片路径</param>
227     /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns>
228     public string ChangeFileVir(string fileName, string playFile, string imgFile)
229     {
230       string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
231       if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
232       {
233         return "";
234       }
235       string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), ".jpg");
236       string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
237       string FlvImgSize = VideoConvert.sizeOfImg;
238       System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
239       ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
240       ImgstartInfo.Arguments = "  -i  " + fileName + "  -y  -f  image2  -t  0.001  -s  " + FlvImgSize + "  " + flv_img;
241       System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
242       FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
243       FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
244       try
245       {
246         System.Diagnostics.Process.Start(FilestartInfo);
247         System.Diagnostics.Process.Start(ImgstartInfo);
248       }
249       catch
250       {
251         return "";
252       }
253       ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; 
254       ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;  
255       if (System.IO.File.Exists(flv_img))
256       {
257         return flv_img;
258       }
259       return "";
260     }
261     #endregion
262     #region 运行mencoder的视频解码器转换(绝对路径)
263     /// <summary>
264     /// 运行mencoder的视频解码器转换
265     /// </summary>
266     public string MChangeFilePhy(string vFileName, string playFile, string imgFile)
267     {
268       string tool = Server.MapPath(VideoConvert.mencodertool);
269       if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName)))
270       {
271         return "";
272       }
273       string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
274       string FlvImgSize = VideoConvert.sizeOfImg;
275       System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
276       FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
277       FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale=" + widthOfFile + ":" + heightOfFile + " -ofps 12 -srate 22050";
278       try
279       {
280         System.Diagnostics.Process.Start(FilestartInfo);
281         CatchImg(flv_file, imgFile);
282       }
283       catch
284       {
285         return "";
286       }
287       return "";
288     }
289     #endregion
290   }
291 }

希望本文所述对大家的C#程序设计有所帮助。

转载于:https://www.cnblogs.com/moy-1313133/p/6652947.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值