C#上传视频生成缩略图

注意:需要FFmpeg软件,用到ffmpeg.exe;FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。

FFmpeg下载地址:http://download.csdn.net/detail/kingcruel/6549339

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>






    


    

后台代码

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("
" + this.CatchImg("uploadfile/" + filename + "." + imgfileExp));//可以使用 CatchImg1("/uploadfile/" + fileName);//可以使用经过整理 } } /// /// 上传视频生成缩略图 /// /// /// /// 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中,如: 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 ""; } } /// /// 上传视频生成缩略图 /// /// /// /// 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 ""; } } } }



你可能感兴趣的:(.Net技术)