Cg calls mmpeg to perform various video conversion encapsulation class codes

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

Reply to "Cg calls mmpeg to perform various video conversion encapsulation class codes"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website