您的位置:首页 > 编程语言 > ASP

ASP.NET上传视频文件同时转换为flv并且抓取第一帧生面图片源码分析

2010-10-13 17:48 861 查看
下面介绍使用ASP.NET上传视频文件并且转换为FLV文件的方法,还要生成视频图片。下面开始吧

WEB.config配置节如下

<appSettings>
<!--工具文件夹-->
<add key="ffmpeg" value="ffmpeg/ffmpeg.exe"/>
<add key="mencoder" value="mencoder/mencoder.exe"/>
<add key="mplayer" value="mencoder/mplayer.exe"/>
<!--上传文件的路径-->
<add key="upfile" value="UpFiles"/>
<!--上专文件图片路径-->
<add key="imgfile" value="ImgFile"/>
<!--上传文件图片大小-->
<add key="CatchFlvImgSize" value="240x180"/>
<add key="widthSize" value="400"/>
<add key="heightSize" value="350"/>
<!--转换后文件路径-->
<add key="playfile" value="PlayFiles"/>
</appSettings>
上传类:
namespace VideoToFLV
{
public class PublicMethod:System.Web.UI.Page
{
public PublicMethod()
{

}
//文件路径
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)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:/51aspx/ffmpeg.exe" />
string ffmpeg = 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 FlvImgSize = PublicMethod.sizeOfImg;

System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);

FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
//ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.05 -s " + FlvImgSize + " " + flv_img;

try
{
//转换
System.Diagnostics.Process.Start(FilestartInfo);
//截图
CatchImg(fileName, imgFile);
//System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
return "";
}
//
return "";
}
//
public string CatchImg(string fileName,string imgFile)
{
//
string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
//
string flv_img =imgFile+".jpg";
//
string FlvImgSize = PublicMethod.sizeOfImg;
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
return "";
}
//
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}

return "";
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐