C#调用mmpeg进行各种视频转换的类实例
本文实例讲述了C#调用mmpeg进行各种视频转换的类。分享给大家供大家参考。具体如下:
这个C#类封装了视频转换所需的各种方法,基本上是围绕着如何通过mmpeg工具来进行视频转换
usingSystem.Web; usingSystem.Configuration; namespaceDotNet.Utilities { //if(this.fload.HasFile) //{ //stringupFileName=HttpContext.Current.Server.MapPath("~/savefile")+"\\"+this.fload.PostedFile.FileName; //stringsaveName=DateTime.Now.ToString("yyyyMMddHHmmssffff"); //stringplayFile=Server.MapPath(VideoConvert.savefile+saveName); //stringimgFile=Server.MapPath(VideoConvert.savefile+saveName); //VideoConvertpm=newVideoConvert(); //stringm_strExtension=VideoConvert.GetExtension(this.fload.PostedFile.FileName).ToLower(); //if(m_strExtension=="flv") //{ //System.IO.File.Copy(upFileName,playFile+".flv"); //pm.CatchImg(upFileName,imgFile); //} //stringExtension=pm.CheckExtension(m_strExtension); //if(Extension=="ffmpeg") //{ //pm.ChangeFilePhy(upFileName,playFile,imgFile); //} //elseif(Extension=="mencoder") //{ //pm.MChangeFilePhy(upFileName,playFile,imgFile); //} //} publicclassVideoConvert:System.Web.UI.Page { publicVideoConvert() {} string[]strArrMencoder=newstring[]{"wmv","rmvb","rm"}; string[]strArrFfmpeg=newstring[]{"asf","avi","mpg","3gp","mov"}; #region配置 publicstaticstringffmpegtool=ConfigurationManager.AppSettings["ffmpeg"]; publicstaticstringmencodertool=ConfigurationManager.AppSettings["mencoder"]; publicstaticstringsavefile=ConfigurationManager.AppSettings["savefile"]+"/"; publicstaticstringsizeOfImg=ConfigurationManager.AppSettings["CatchFlvImgSize"]; publicstaticstringwidthOfFile=ConfigurationManager.AppSettings["widthSize"]; publicstaticstringheightOfFile=ConfigurationManager.AppSettings["heightSize"]; #endregion #region获取文件的名字 ///<summary> ///获取文件的名字 ///</summary> publicstaticstringGetFileName(stringfileName) { inti=fileName.LastIndexOf("\\")+1; stringName=fileName.Substring(i); returnName; } #endregion #region获取文件扩展名 ///<summary> ///获取文件扩展名 ///</summary> publicstaticstringGetExtension(stringfileName) { inti=fileName.LastIndexOf(".")+1; stringName=fileName.Substring(i); returnName; } #endregion #region获取文件类型 ///<summary> ///获取文件类型 ///</summary> publicstringCheckExtension(stringextension) { stringm_strReturn=""; foreach(stringvarinthis.strArrFfmpeg) { if(var==extension) { m_strReturn="ffmpeg";break; } } if(m_strReturn=="") { foreach(stringvarinstrArrMencoder) { if(var==extension) { m_strReturn="mencoder";break; } } } returnm_strReturn; } #endregion #region视频格式转为Flv ///<summary> ///视频格式转为Flv ///</summary> ///<paramname="vFileName">原视频文件地址</param> ///<paramname="ExportName">生成后的Flv文件地址</param> publicboolConvertFlv(stringvFileName,stringExportName) { if((!System.IO.File.Exists(ffmpegtool))||(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) { returnfalse; } vFileName=HttpContext.Current.Server.MapPath(vFileName); ExportName=HttpContext.Current.Server.MapPath(ExportName); stringCommand="-i\""+vFileName+"\"-y-ab32-ar22050-b800000-s480*360\""+ExportName+"\"";//Flv格式 System.Diagnostics.Processp=newSystem.Diagnostics.Process(); p.StartInfo.FileName=ffmpegtool; p.StartInfo.Arguments=Command; p.StartInfo.WorkingDirectory=HttpContext.Current.Server.MapPath("~/tools/"); p.StartInfo.UseShellExecute=false; p.StartInfo.RedirectStandardInput=true; p.StartInfo.RedirectStandardOutput=true; p.StartInfo.RedirectStandardError=true; p.StartInfo.CreateNoWindow=false; p.Start(); p.BeginErrorReadLine(); p.WaitForExit(); p.Close(); p.Dispose(); returntrue; } #endregion #region生成Flv视频的缩略图 ///<summary> ///生成Flv视频的缩略图 ///</summary> ///<paramname="vFileName">视频文件地址</param> publicstringCatchImg(stringvFileName) { if((!System.IO.File.Exists(ffmpegtool))||(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName))))return""; try { stringflv_img_p=vFileName.Substring(0,vFileName.Length-4)+".jpg"; stringCommand="-i"+HttpContext.Current.Server.MapPath(vFileName)+"-y-fimage2-t0.1-s"+sizeOfImg+""+HttpContext.Current.Server.MapPath(flv_img_p); System.Diagnostics.Processp=newSystem.Diagnostics.Process(); p.StartInfo.FileName=ffmpegtool; p.StartInfo.Arguments=Command; p.StartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Normal; try { p.Start(); } catch { return""; } finally { p.Close(); p.Dispose(); } System.Threading.Thread.Sleep(4000); //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; if(System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p))) { returnflv_img_p; } return""; } catch { return""; } } #endregion #region运行FFMpeg的视频解码(绝对路径) ///<summary> ///转换文件并保存在指定文件夹下 ///</summary> ///<paramname="fileName">上传视频文件的路径(原文件)</param> ///<paramname="playFile">转换后的文件的路径(网络播放文件)</param> ///<paramname="imgFile">从视频文件中抓取的图片路径</param> ///<returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> publicstringChangeFilePhy(stringfileName,stringplayFile,stringimgFile) { stringffmpeg=Server.MapPath(VideoConvert.ffmpegtool); if((!System.IO.File.Exists(ffmpeg))||(!System.IO.File.Exists(fileName))) { return""; } stringflv_file=System.IO.Path.ChangeExtension(playFile,".flv"); stringFlvImgSize=VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfoFilestartInfo=newSystem.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments="-i"+fileName+"-ab56-ar22050-b500-r15-s"+widthOfFile+"x"+heightOfFile+""+flv_file; try { System.Diagnostics.Process.Start(FilestartInfo);//转换 CatchImg(fileName,imgFile);//截图 } catch { return""; } return""; } publicstringCatchImg(stringfileName,stringimgFile) { stringffmpeg=Server.MapPath(VideoConvert.ffmpegtool); stringflv_img=imgFile+".jpg"; stringFlvImgSize=VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfoImgstartInfo=newSystem.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments="-i"+fileName+"-y-fimage2-ss2-vframes1-s"+FlvImgSize+""+flv_img; try { System.Diagnostics.Process.Start(ImgstartInfo); } catch { return""; } if(System.IO.File.Exists(flv_img)) { returnflv_img; } return""; } #endregion #region运行FFMpeg的视频解码(相对路径) ///<summary> ///转换文件并保存在指定文件夹下 ///</summary> ///<paramname="fileName">上传视频文件的路径(原文件)</param> ///<paramname="playFile">转换后的文件的路径(网络播放文件)</param> ///<paramname="imgFile">从视频文件中抓取的图片路径</param> ///<returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> publicstringChangeFileVir(stringfileName,stringplayFile,stringimgFile) { stringffmpeg=Server.MapPath(VideoConvert.ffmpegtool); if((!System.IO.File.Exists(ffmpeg))||(!System.IO.File.Exists(fileName))) { return""; } stringflv_img=System.IO.Path.ChangeExtension(Server.MapPath(imgFile),".jpg"); stringflv_file=System.IO.Path.ChangeExtension(Server.MapPath(playFile),".flv"); stringFlvImgSize=VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfoImgstartInfo=newSystem.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments="-i"+fileName+"-y-fimage2-t0.001-s"+FlvImgSize+""+flv_img; System.Diagnostics.ProcessStartInfoFilestartInfo=newSystem.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments="-i"+fileName+"-ab56-ar22050-b500-r15-s"+widthOfFile+"x"+heightOfFile+""+flv_file; try { System.Diagnostics.Process.Start(FilestartInfo); System.Diagnostics.Process.Start(ImgstartInfo); } catch { return""; } ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败; if(System.IO.File.Exists(flv_img)) { returnflv_img; } return""; } #endregion #region运行mencoder的视频解码器转换(绝对路径) ///<summary> ///运行mencoder的视频解码器转换 ///</summary> publicstringMChangeFilePhy(stringvFileName,stringplayFile,stringimgFile) { stringtool=Server.MapPath(VideoConvert.mencodertool); if((!System.IO.File.Exists(tool))||(!System.IO.File.Exists(vFileName))) { return""; } stringflv_file=System.IO.Path.ChangeExtension(playFile,".flv"); stringFlvImgSize=VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfoFilestartInfo=newSystem.Diagnostics.ProcessStartInfo(tool); FilestartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments=""+vFileName+"-o"+flv_file+"-oflavf-lavfoptsi_certify_that_my_video_stream_does_not_use_b_frames-oacmp3lame-lameoptsabr:br=56-ovclavc-lavcoptsvcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1-vfscale="+widthOfFile+":"+heightOfFile+"-ofps12-srate22050"; try { System.Diagnostics.Process.Start(FilestartInfo); CatchImg(flv_file,imgFile); } catch { return""; } return""; } #endregion } }
希望本文所述对大家的C#程序设计有所帮助。