All Media to FLV asp.net在线视频自动转换并截图调试成功!
代码如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.SessionState; using System.Web.Security; using System.IO; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Diagnostics; using System.Messaging; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; //using UploadControls; /// <summary> /// 上传文件事件; /// </summary> public class Upload : IHttpHandler, IRequiresSessionState { public bool ThumbnailCallback() { return false; } public Upload() { } #region IHttpHandler Members public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { if (context.Request.Files.Count > 0) { string filePath = "/Upload/Mv/" + DateTime.Now.ToString("yyyyMM") + "/"; DirectoryInfo info = new DirectoryInfo(context.Server.MapPath(filePath)); if (!info.Exists) { info.Create(); } Random ro = new Random(); for (int j = 0; j < context.Request.Files.Count; j++) { HttpPostedFile uploadFile = context.Request.Files[j]; if (uploadFile.ContentLength > 0) { string stro = ro.Next(100, 100000000).ToString();//产生一个随机数用于新命名的图片 string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro; string extension = Path.GetExtension(uploadFile.FileName); string fileName = NewName + extension.ToString(); uploadFile.SaveAs(Path.Combine(context.Server.MapPath(filePath), fileName)); if (File.Exists(context.Request.Files[j].ToString())) { try { File.Delete(context.Request.Files[j].ToString()); } catch { } } string str10 = filePath + fileName; if (!MessageQueue.Exists(@".\private$\MvQueue")) { MessageQueue.Create(@".\private$\MvQueue", true); } MessageQueue queue = new MessageQueue(@".\private$\MvQueue"); Message message = new Message(); message.Body = context.Server.MapPath(str10); message.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); MessageQueueTransaction transaction = new MessageQueueTransaction(); transaction.Begin(); queue.Send(message, transaction); transaction.Commit(); bool flag = false; string str12 = "UcfarFlvConvert"; Process[] processes = Process.GetProcesses(); foreach (Process process in processes) { if (str12 == process.ProcessName) { flag = true; break; } } if (!flag) { Process process2 = new Process(); process2.StartInfo.WorkingDirectory = context.Server.MapPath("/FlvConvert/"); process2.StartInfo.FileName = "UcfarFlvConvert.exe"; process2.StartInfo.Arguments = string.Empty; process2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process2.Start(); } continue; } } GC.Collect(); } } #endregion }
转换进程守护代码:
using System; using System.Diagnostics; using System.IO; using System.Messaging; using System.Threading; namespace UcfarFlvConvert { internal class Program { private static void ConvertToFlv() { MessageQueue queue = new MessageQueue(@".\private$\MvQueue"); queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); while (true) { if (queue.Transactional) { MessageQueueTransaction transaction = new MessageQueueTransaction(); transaction.Begin(); string body = queue.Receive(transaction).Body as string; transaction.Commit(); ToFlv(body); } } } private static void Main(string[] args) { Thread[] threadArray = new Thread[5]; for (int i = 0; i < 5; i++) { threadArray[i] = new Thread(new ThreadStart(Program.ConvertToFlv)); threadArray[i].IsBackground = true; threadArray[i].Start(); } Console.WriteLine("start."); Console.ReadLine(); Console.WriteLine("stop"); for (int j = 0; j < 5; j++) { threadArray[j].Abort(); } } private static void runProcess(string inputFile, string imageFile, int imageWidth, int imageHeight) { if (imageFile != string.Empty) { using (Process process = new Process()) { process.StartInfo.WorkingDirectory = Environment.CurrentDirectory; process.StartInfo.FileName = "ffmpeg.exe"; process.StartInfo.Arguments = string.Format(string.Concat(new object[] { "-i \"{0}\" -y -f image2 -ss 1 -t 0.001 -s ", imageWidth, "*", imageHeight, " \"{1}\" " }), inputFile, imageFile); process.StartInfo.UseShellExecute = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); } } } private static void runProcess(int quality, string inputFile, string outputFile, string imageFile, int movieWidth, int imageWidth, int imageHeight) { using (Process process = new Process()) { process.StartInfo.WorkingDirectory = Environment.CurrentDirectory; process.StartInfo.FileName = "mencoder.exe"; process.StartInfo.Arguments = string.Format(string.Concat(new object[] { "-vf scale=", movieWidth, ":-3 -ffourcc FLV1 -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -ovc lavc -lavcopts vcodec=flv:vbitrate=", quality, " -srate 22050 -oac mp3lame -lameopts mode=2:abr:br=56:vol=5 \"{0}\" -o \"{1}\"" }), inputFile, outputFile); process.StartInfo.UseShellExecute = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); process.WaitForExit(); } using (Process process2 = new Process()) { process2.StartInfo.WorkingDirectory = Environment.CurrentDirectory; process2.StartInfo.FileName = "flvmdi.exe"; process2.StartInfo.Arguments = string.Format("\"{0}\" /k", outputFile); process2.StartInfo.UseShellExecute = true; process2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process2.Start(); } if (imageFile != string.Empty) { using (Process process3 = new Process()) { process3.StartInfo.WorkingDirectory = Environment.CurrentDirectory; process3.StartInfo.FileName = "ffmpeg.exe"; process3.StartInfo.Arguments = string.Format(string.Concat(new object[] { "-i \"{0}\" -y -f image2 -ss 1 -t 0.001 -s ", imageWidth, "*", imageHeight, " \"{1}\" " }), outputFile, imageFile); process3.StartInfo.UseShellExecute = true; process3.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process3.Start(); } } } private static void ToFlv(string fileName) { int quality = 700; string path = fileName; string outputFile = fileName.Substring(0, fileName.LastIndexOf('.')) + ".flv"; string imageFile = fileName.Substring(0, fileName.LastIndexOf('.')) + ".jpg"; int movieWidth = 0x177; int imageWidth = 300; int imageHeight = 200; if (Path.GetExtension(path) == ".flv") { runProcess(path, imageFile, imageWidth, imageHeight); } else { runProcess(quality, path, outputFile, imageFile, movieWidth, imageWidth, imageHeight); } try { File.Delete(fileName); } catch { } } } }