1. 程式人生 > >C#上傳視訊生成縮圖

C#上傳視訊生成縮圖

注意:需要FFmpeg軟體,用到ffmpeg.exe;FFmpeg是一個開源免費跨平臺的視訊和音訊流方案,屬於自由軟體,採用LGPL或GPL許可證(依據你選擇的元件)。它提供了錄製、轉換以及流化音視訊的完整解決方案。

前臺程式碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>  
  
<!DOCTYPE html>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:FileUpload ID="FileUpload1" runat="server" />  
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
    </div>  
    </form>  
</body>  
</html>  

後臺程式碼
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string imgfileExp = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1);
            string filename = "11223344";
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);                      //檔名稱
            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();      //檔案的字尾名(小寫)

            if (imgfileExp.ToLower() == "flv" || imgfileExp.ToLower() == "mp4")
            {
                Response.Write("uploadfile\\" + filename + "." + imgfileExp);
                this.FileUpload1.PostedFile.SaveAs(Server.MapPath("uploadfile") + "\\" + fileName);
                string ffmpeg = Server.MapPath("ffmpeg.exe");//E:\視訊縮圖測試\html
                string filenames = Server.MapPath("uploadfile") + "\\" + filename + "." + imgfileExp;
                //Response.Write("<br/>" + this.CatchImg("uploadfile/" + filename + "." + imgfileExp));//可以使用
                CatchImg1("/uploadfile/" + fileName);//可以使用經過整理
            }
        }

        /// <summary>
        /// 上傳視訊生成縮圖
        /// </summary>
        /// <param name="vFileName"></param>
        /// <param name="SmallPic"></param>
        /// <returns></returns>
        public string CatchImg(string vFileName)
        {
            try
            {
                string ffmpeg = "ffmpeg.exe";
                ffmpeg = Server.MapPath(ffmpeg);
                string aaa = System.Web.HttpContext.Current.Server.MapPath(vFileName);
                if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(vFileName))))
                {
                    return "";
                }
                //獲得圖片相對路徑/最後儲存到資料庫的路徑,如:/Web/FlvFile/User1/00001.jpg
                string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
                //圖片絕對路徑,如:D:\Video\Web\FlvFile\User1\0001.jpg
                string flv_img_p = Server.MapPath("/uploadfile/1.jpg");
                //截圖的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="140x110" /> 
                string FlvImgSize = "140x110";
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                //此處組合成ffmpeg.exe檔案需要的引數即可,此處命令在ffmpeg 0.4.9除錯通過
                startInfo.Arguments = " -i " + Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + FlvImgSize + " " + flv_img_p;
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return "";
                }
                System.Threading.Thread.Sleep(4000);
                /**/
                ///注意:圖片擷取成功後,資料由記憶體快取寫到磁碟需要時間較長,大概在3,4秒甚至更長;
                if (System.IO.File.Exists(flv_img_p))
                {
                    return flv_img_p.Replace(Server.MapPath("~/"), ""); ;
                }
                return "";
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 上傳視訊生成縮圖
        /// </summary>
        /// <param name="vFileName"></param>
        /// <param name="SmallPic"></param>
        /// <returns></returns>
        public string CatchImg1(string vFileName)
        {
            try
            {
                string ffmpeg = "/ffmpeg.exe";
                ffmpeg = Server.MapPath(ffmpeg);
                if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(Server.MapPath(vFileName))))
                {
                    return "";
                }
                string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
                string flv_img_p = Server.MapPath(flv_img);
                string FlvImgSize = "140x110";
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                startInfo.Arguments = " -i " + Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + FlvImgSize + " " + flv_img_p;
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return "";
                }
                System.Threading.Thread.Sleep(4000);
                if (System.IO.File.Exists(flv_img_p))
                {
                    return flv_img_p.Replace(Server.MapPath("~/"), ""); ;
                }
                return "";
            }
            catch
            {
                return "";
            }
        }
    }
}