62,046
社区成员
发帖
与我相关
我的任务
分享
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;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
// 扩展名定义
string[] strArrFfmpeg = new string[] { "asf", "avi", "mpg", "3gp", "mov" };
string[] strArrMencoder = new string[] { "wmv", "rm", "rmvb" };
protected void Page_Load(object sender, EventArgs e)
{
}
//
protected void btnUpload_Click(object sender, EventArgs e)
{
string upFileName = "";
if (this.FileUpload1.HasFile)
{
string fileName = PublicMethod.GetFileName(this.FileUpload1.FileName);// GetFileName();
if ((string)Session["file"] == fileName)
{
return;
}
upFileName = Server.MapPath(PublicMethod.upFile + fileName);
this.FileUpload1.SaveAs(upFileName);
string saveName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string playFile = Server.MapPath(PublicMethod.playFile + saveName);
string imgFile = Server.MapPath(PublicMethod.imgFile + saveName);
//System.IO.File.Copy(Server.MapPath(PublicMethod.playFile + "00000002.jpg"), Server.MapPath(PublicMethod.imgFile+"aa.jpg"));
PublicMethod pm = new PublicMethod();
string m_strExtension = PublicMethod.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower();
if (m_strExtension == "flv")
{//直接拷贝到播放文件夹下
System.IO.File.Copy(upFileName, playFile + ".flv");
pm.CatchImg(upFileName, imgFile);
}
string Extension = CheckExtension(m_strExtension);
if (Extension == "ffmpeg")
{
pm.ChangeFilePhy(upFileName,playFile,imgFile);
}
/* else if (Extension == "mencoder")
{
pm.MChangeFilePhy(upFileName, playFile, imgFile);
}
InsertData(this.txtTitle.Text, fileName, saveName);*/
Session["file"] = fileName;
}
}
//
private string CheckExtension(string extension)
{
string m_strReturn = "";
foreach (string var in this.strArrFfmpeg)
{
if (var == extension)
{
m_strReturn = "ffmpeg"; break;
}
}
/* if (m_strReturn == "")
{
foreach (string var in strArrMencoder)
{
if (var == extension)
{
m_strReturn = "mencoder"; break;
}
}
}*/
return m_strReturn;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
/// <summary>
///PublicMethod 的摘要说明
/// </summary>
public class PublicMethod
{
public PublicMethod()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];
public static string mencodertool = ConfigurationManager.AppSettings["mencoder"];
public static string mplayertool = ConfigurationManager.AppSettings["mplayer"];
public static string upFile = ConfigurationManager.AppSettings["upfile"] + "/";
public static string imgFile = ConfigurationManager.AppSettings["imgfile"] + "/";
public static string playFile = ConfigurationManager.AppSettings["playfile"] + "/";
//文件图片大小
public static string sizeOfImg = ConfigurationManager.AppSettings["CatchFlvImgSize"];
//文件大小
public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
// // //获取文件的名字
public static string GetFileName(string fileName)
{
int i = fileName.LastIndexOf("\\") + 1;
string Name = fileName.Substring(i);
return Name;
}
//获取文件扩展名
public static string GetExtension(string fileName)
{
int i = fileName.LastIndexOf(".") + 1;
string Name = fileName.Substring(i);
return Name;
}
//
#region //运行FFMpeg的视频解码,(这里是绝对路径)
/// <summary>
/// 转换文件并保存在指定文件夹下面(这里是绝对路径)
/// </summary>
/// <param name="fileName">上传视频文件的路径(原文件)</param>
/// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
/// <param name="imgFile">从视频文件中抓取的图片路径</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public string ChangeFilePhy(string fileName, string playFile, string imgFile)
{
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(PublicMethod.ffmpegtool);
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
}
//获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
// string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
string filename = TransferVedio(upFile, playFile);
string smallpic = CatchImg(upFile, imgFile);
}
public string TransferVedio(string upFileName, string playFile)
{
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(PublicMethod.ffmpegtool);
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
//string FlvImgSize = VedioConvert.sizeOfImg;
Process p = new Process();//建立外部调用线程
p.StartInfo.FileName = ffmpeg;//要调用外部程序的绝对路径
p.StartInfo.Arguments = " -i " + "\"" + upFileName + "\"" + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + "\"" + flv_file + "\"";
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
//p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
if (System.IO.File.Exists(flv_file) && new FileInfo(flv_file).Length > 0)
{
return flv_file.Substring(flv_file.LastIndexOf("\\") + 1);
}
else
{
return "";
}
}
public string CatchImg(string fileName, string imgFile)
{
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(PublicMethod.ffmpegtool);
string flv_img = imgFile + ".jpg";
string FlvImgSize = VedioConvert.sizeOfImg;
Process p = new Process();//建立外部调用线程
p.StartInfo.FileName = ffmpeg;//要调用外部程序的绝对路径
p.StartInfo.Arguments = @"-i " + "\"" + fileName + "\"" + " -y -f image2 -ss 08.010 -t 0.001 -s 352x240 " + "\"" + flv_img + "\"";
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
//p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
if (System.IO.File.Exists(flv_img) && new FileInfo(flv_img).Length > 0)
{
return flv_img.Substring(flv_img.LastIndexOf("\\") + 1);
}
else
{
return "";
}
}
#endregion
}