查看: 317|回复: 0

[.NET开发] C#视频转换类分享

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#视频转换类的具体代码,供大家参考,具体内容如下

VideoConvert

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

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


回复

使用道具 举报