123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System.Web;
- using System.Configuration;
- namespace Ant.Service.Utilities
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class VideoConvert : System.Web.UI.Page
- {
- public VideoConvert()
- { }
- string[] strArrMencoder = new string[] { "wmv", "rmvb", "rm" };
- string[] strArrFfmpeg = new string[] { "asf", "avi", "mpg", "3gp", "mov" };
- #region 配置
- public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];
- public static string mencodertool = ConfigurationManager.AppSettings["mencoder"];
- public static string savefile = ConfigurationManager.AppSettings["savefile"] + "/";
- public static string sizeOfImg = ConfigurationManager.AppSettings["CatchFlvImgSize"];
- public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
- public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
- #endregion
- #region 获取文件的名字
-
-
-
- public static string GetFileName(string fileName)
- {
- int i = fileName.LastIndexOf("\\") + 1;
- string Name = fileName.Substring(i);
- return Name;
- }
- #endregion
- #region 获取文件扩展名
-
-
-
- public static string GetExtension(string fileName)
- {
- int i = fileName.LastIndexOf(".") + 1;
- string Name = fileName.Substring(i);
- return Name;
- }
- #endregion
- #region 获取文件类型
-
-
-
- public string CheckExtension(string extension)
- {
- string m_strReturn = "";
- foreach (string var in this.strArrFfmpeg)
- {
- if (var == extension)
- {
- m_strReturn = "ffmpeg"; break;
- }
- }
- if (m_strReturn == "")
- {
- foreach (string var in strArrMencoder)
- {
- if (var == extension)
- {
- m_strReturn = "mencoder"; break;
- }
- }
- }
- return m_strReturn;
- }
- #endregion
- #region 视频格式转为Flv
-
-
-
-
-
- public bool ConvertFlv(string vFileName, string ExportName)
- {
- if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName))))
- {
- return false;
- }
- vFileName = HttpContext.Current.Server.MapPath(vFileName);
- ExportName = HttpContext.Current.Server.MapPath(ExportName);
- string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\"";
- System.Diagnostics.Process p = new System.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();
- return true;
- }
- #endregion
- #region 生成Flv视频的缩略图
-
-
-
-
- public string CatchImg(string vFileName)
- {
- if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) return "";
- try
- {
- string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg";
- string Command = " -i " + HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + HttpContext.Current.Server.MapPath(flv_img_p);
- System.Diagnostics.Process p = new System.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);
-
- if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p)))
- {
- return flv_img_p;
- }
- return "";
- }
- catch
- {
- return "";
- }
- }
- #endregion
- #region 运行FFMpeg的视频解码(绝对路径)
-
-
-
-
-
-
-
- public string ChangeFilePhy(string fileName, string playFile, string imgFile)
- {
- string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
- if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
- {
- return "";
- }
- string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
- string FlvImgSize = VideoConvert.sizeOfImg;
- System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
- FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
- try
- {
- System.Diagnostics.Process.Start(FilestartInfo);
- CatchImg(fileName, imgFile);
- }
- catch
- {
- return "";
- }
- return "";
- }
- public string CatchImg(string fileName, string imgFile)
- {
- string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
- string flv_img = imgFile + ".jpg";
- string FlvImgSize = VideoConvert.sizeOfImg;
- System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
- ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
- try
- {
- System.Diagnostics.Process.Start(ImgstartInfo);
- }
- catch
- {
- return "";
- }
- if (System.IO.File.Exists(flv_img))
- {
- return flv_img;
- }
- return "";
- }
- #endregion
- #region 运行FFMpeg的视频解码(相对路径)
-
-
-
-
-
-
-
- public string ChangeFileVir(string fileName, string playFile, string imgFile)
- {
- string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
- if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
- {
- return "";
- }
- string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), ".jpg");
- string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
- string FlvImgSize = VideoConvert.sizeOfImg;
- System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
- ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img;
- System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
- FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
- try
- {
- System.Diagnostics.Process.Start(FilestartInfo);
- System.Diagnostics.Process.Start(ImgstartInfo);
- }
- catch
- {
- return "";
- }
-
-
- if (System.IO.File.Exists(flv_img))
- {
- return flv_img;
- }
- return "";
- }
- #endregion
- #region 运行mencoder的视频解码器转换(绝对路径)
-
-
-
- public string MChangeFilePhy(string vFileName, string playFile, string imgFile)
- {
- string tool = Server.MapPath(VideoConvert.mencodertool);
- if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName)))
- {
- return "";
- }
- string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
- string FlvImgSize = VideoConvert.sizeOfImg;
- System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
- FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- 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";
- try
- {
- System.Diagnostics.Process.Start(FilestartInfo);
- CatchImg(flv_file, imgFile);
- }
- catch
- {
- return "";
- }
- return "";
- }
- #endregion
- }
- }
|