C # Video conversion class,
Using System. web; using System. configuration; namespace DotNet. 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 configure public static string ffmpegtool = ConfigurationManager. appSettings ["ffmpeg"]; public static string menc Odertool = ConfigurationManager. deleetask[ "mencoder"]; public static string savefile = ConfigurationManager. deleetask[ "savefile"] + "/"; public static string sizeOfImg = ConfigurationManager. appSettings ["CatchFlvImgSize"]; public static string widthOfFile = ConfigurationManager. deleetask[ "widthSize"]; public static string heightOfFile = ConfigurationManager. appSettings ["heightSize"]; # endreg Ion # region get the file name /// <summary> /// get the file name /// </summary> public static string GetFileName (string fileName) {int I = fileName. lastIndexOf ("\") + 1; string Name = fileName. substring (I); return Name ;} # endregion # region get the file extension // <summary> // get the file extension /// </summary> public static string GetExtension (string fileName) {int I = fileName. lastIndexOf (". ") + 1; string Name = fileName. substring (I); Return Name ;}# endregion # region obtain the file type /// <summary> /// obtain the file type /// </summary> 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_st RReturn ;} # endregion # convert the region video format to Flv // <summary> // convert the video format to Flv /// </summary> /// <param name = "vFileName"> original video file address </param> /// <param name = "ExportName"> the generated Flv file address </param> 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 + "\" "; // Flv format: System. diagnostics. process p = new System. diagnostics. process (); p. startIn Fo. 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 generate a Flv video thumbnail /// <summary> /// generate a Flv video thumbnail /// </summary> /// <param name = "vFileN Ame "> video file address </param> 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. star TInfo. 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); // Note: After an image is captured successfully, it takes a long time to write data from the memory cache to the disk, which may take 3, 4 seconds or longer. if (System. IO. file. exists (HttpContext. current. server. mapPath (flv_img_p) {return flv_img_p;} return "";} Catch {return "" ;}# endregion # region video decoding of FFMpeg running (absolute path) /// <summary> /// convert the file and save it in the specified folder /// </summary> /// <param name = "fileName"> upload the video file path (original file) </param> /// <param name = "playFile"> path of the converted file (Network playback file) </param> /// <param name = "imgFile"> path of the image captured from the video file </param> /// <returns> success: the virtual address of the image is returned; failed: return an empty string </returns> 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); // convert 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. D Iagnostics. 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 f Lv_img;} return "" ;}# endregion # region video decoding by running FFMpeg (relative path) /// <summary> /// convert the file and save it in the specified folder /// </summary> /// <param name = "fileName"> upload the video file path (original file) </param> /// <param name = "playFile"> path of the converted file (Network playback file) </param> /// <param name = "imgFile"> path of the image captured from the video file </param> /// <returns> success: the virtual address of the image is returned; failed: return an empty string </returns> 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. diagnost Ics. 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" + width OfFile + "x" + heightOfFile + "" + flv_file; try {System. diagnostics. process. start (FilestartInfo); System. diagnostics. process. start (ImgstartInfo);} catch {return "" ;}/// Note: it takes a long time to write data to the disk from the memory cache after the image is intercepted successfully, it may take about 3, 4 seconds or longer. // if the image does not exist for more than 8 seconds, the server latency is 8 seconds. if (System. IO. file. exists (flv_img) {return flv_img;} return "" ;}# endregion # region run the video decoder conversion (absolute path) of mencoder. /// <summary> /// run the mencode R video decoder conversion // </summary> 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 audio-oac mp3lame-lameopts abr: br = 56-ovc lavc-lavcopts vcodec = flv: vbitrate = 200: mbd = 2: mv0: trell: v4mv: White: 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 }}
VideoConvert