c#上传视频后,生成视频缩略图

c#上传视频后,生成视频缩略图,以下是我找的别人的代码,但是运行后,点击提交视频,老是出现“连接被重置

载入页面时到服务器的连接被重置。”的提示,里面需要用到的ffmpeg.exe我也下载了,uploadfile文件夹也建了,就是没法正常运行,哪位大神帮我一下,谢谢!
要命的是,现在不用ffmpeg.exe了,还会提示“连接被重置
”我真是不知道该怎么办了..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>






</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 WTS_Digital.Admin
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }


    /// <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 "";
        }
    }

    protected void Button1_Click1(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" || imgfileExp.ToLower() == "wmv")
        {
            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;

            CatchImg1("/uploadfile/" + fileName);//可以使用经过整理

        }
    }
}

}

阅读 7.5k
1 个回答

以下代码就能实现视频截图,希望对有需要的人有所帮助~
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
public class makePic
{

public static string CatchImg(string vFileName)
    {
       string defPic="images/nopic.gif";//无法生成时显示的图片
string picPath="UploadImage/" ;//生成图片输出位置
string movPath="case/";//视频文件位置
        string ffmpeg = HttpContext.Current.Server.MapPath("bin/ffmpeg.exe");

        if (!System.IO.File.Exists(ffmpeg))
        {
            return defPic;
        }
        if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath("case/"+vFileName)))
        {
            return defPic;
        }

        string flv_img =picPath + System.IO.Path.ChangeExtension(vFileName, ".jpg");


       string flv_img_p = HttpContext.Current.Server.MapPath(flv_img);

       if (System.IO.File.Exists(flv_img_p))
        {
            return flv_img;
        }


        string FlvImgSize = "240x180";

        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;


        startInfo.Arguments = " -i " + HttpContext.Current.Server.MapPath(movPath+vFileName) + " -y -f image2 -t 10 -s " + FlvImgSize + " " +  flv_img_p;

        try
        {
            System.Diagnostics.Process.Start(startInfo);
        }
        catch
        {
            return defPic;
        }

       if (System.IO.File.Exists(flv_img_p))
        {
            return flv_img;
        }

        return defPic;
    } 

}

来源:http://admans.net/bbs/dispbbs.asp?boardID=35&ID=6235&page=1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进