All Media to FLV asp.net線上視頻自動轉換並截圖調試成功!

來源:互聯網
上載者:User

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
            {
            }
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.