本文實例講述了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#程序設計有所幫助。