在mvc3中使用ffmpeg对上传视频进行截图和转换格式
/// <summary>
/// 对视频进行图片截取
/// </summary>
/// <param name="fileName">需要截取图片的视频路径</param>
/// <param name="imgFile">截取图片后保存的图片路径</param>
public static void catchImg(string fileName, string imgFile)
{
//获取截图工具路径
string ffmpeg = ConfigurationManager.AppSettings["ffmpeg"];
//获取截图后保存的路径
string flv_img = imgFile;
//获取截取图片的大小
string FlvImgSize = "240x180";
//sizeOfImg;
Process pss = new Process();
//设置启动程序的路径
pss.StartInfo.FileName = ffmpeg;
pss.StartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
//启动进程
pss.Start();
}
//转换电影
#region //运行FFMpeg的视频解码,(这里是绝对路径)
/// <summary>
/// 转换文件并保存在指定文件夹下面(这里是绝对路径)
/// </summary>
/// <param name="fileName">上传视频文件的路径(原文件)</param>
/// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
/// <param name="imgFile">从视频文件中抓取的图片路径</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public static string ChangeFilePhy(string fileName, string playFile, string imgFile)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\51aspx\ffmpeg.exe" />
DateTime start = DateTime.Now;
string ffmpeg = @"D:\Y2项目\AxSchool\AxSchool\AxSchool\Content\IAndVImages\baijunjie\ffmpeg.exe";
//ConfigurationManager.AppSettings["ffmpeg"];
string Name = System.Web.HttpContext.Current.Server.MapPath("../") + upFile + "" + fileName;
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(Name)))
{
return "";
}
//获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
string flv_file = System.IO.Path.ChangeExtension(@"D:\Y2项目\AxSchool\AxSchool\AxSchool\Content\upload\Video\"+fileName, ".flv");
//D:\Y2项目\AxSchool\AxSchool\AxSchool\Content\IAndVImages\baijunjie\
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = "240x180";
System.Diagnostics.Process FilesProcess = new System.Diagnostics.Process();
FilesProcess.StartInfo.FileName = ffmpeg;
FilesProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilesProcess.StartInfo.Arguments = " -i " + Name + " -ab 56 -ar 22050 -b 500 -r 30 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
try
{
FilesProcess.Start();
while (!FilesProcess.HasExited)
{
}
//截图
CatchImg(Name, @"D:\Y2项目\AxSchool\AxSchool\AxSchool\Content\upload\Video\"+fileName);
// WriteRunTime();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "";
}
#endregion
//截图程序
public static string CatchImg(string fileName, string imgFile)
{
//
string ffmpeg = @"D:\Y2项目\AxSchool\AxSchool\AxSchool\Content\IAndVImages\baijunjie\ffmpeg.exe";
//
string flv_img = imgFile + ".jpg";
//
string FlvImgSize = "240x180";
//
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 (Exception ex)
{
Console.WriteLine(ex.Message);
}
//
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
controller中的代码
[AcceptVerbs(HttpVerbs.Post)]
public ContentResult Import(HttpPostedFileBase FileData, string folder)
{
string result = "";
string saveName="";
if (null != FileData)
{
try
{
result = Path.GetFileName(FileData.FileName);//获得文件名
string ext = Path.GetExtension(FileData.FileName);//获得文件扩展名
saveName =result;//实际保存文件名
saveFile(FileData, folder, saveName);//保存文件
}
catch
{
result = "";
}
}
operateMethod.ChangeFilePhy(saveName,folder,folder);//调用转换格式的工具
return Content(result);
}