c#上传视频,生成视频缩略图
以下代码运行后老提示"连接被重置
载入页面时到服务器的连接被重置。",想调试都没办法,请大神帮我看看,谢谢!
WebForm7.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="WTS_Digital.Admin.WebForm7" %>
<!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>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click1" />
</div>
</form>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////
WebForm7.aspx.cs
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);//可以使用经过整理
}
}
}
}
Fifth
9 years, 9 months ago