上传视频时自动完成截取缩略图(二)

从上篇文章中我们知道了ffmpeg是怎么使用的。那么这篇文章给大家介绍下我是怎么通过调用ffmpeg实现在视频上传的同时自动截取图片的。

首先我们不能直接调用ffmpeg实现想要的功能是防止cmd命令执行时出现的黑窗口。所以我们可以封装一个类,然后调用里面的方法只需传递参数就可以实现功能了。

这里我写了一个VideoConverToImg类:

public class VideoConverToImg{/// <summary>/// 从视频中截取img格式图片/// </summary>/// <param name="applicationPath">ffmpeg.exe文件路径</param>/// <param name="fileNamePath">视频文件路径(带文件名)</param>/// <param name="targetImgNamePath">生成img图片路径(带文件名)</param>public static void ConverToImg(string applicationPath, string fileNamePath, string targetImgNamePath){//string c = applicationPath + @"\ffmpeg.exe -i" + fileNamePath + targetImgNamePath+"-ss 00:00:05 -r 1 -vframes 1 -an -vcodec mjpeg " ;string c = applicationPath + @"\ffmpeg.exe -ss 00:00:05 -i" + " " + fileNamePath + " " + targetImgNamePath + " " + "-r 1 -vframes 1 -an -vcodec mjpeg ";//速度快Cmd(c);//-i:设定输入文件名//-r:设定帧 此处设为1帧//-f:设定输出格式//-ss 从指定时间截图//-vcodec:设定影像解码器,没有输入时为文件原来相同的解码器//-vframes 设置转换多少桢(frame)的视频//-an 不处理声音}/// <summary>/// 程序中调用CMD.exe程序,并且不显示命令行窗口界面/// </summary>/// <param name="c">执行的cmd命令</param>private static void Cmd(string c){try{System.Diagnostics.Process process = new System.Diagnostics.Process();process.StartInfo.CreateNoWindow = true; //不显示程序窗口process.StartInfo.FileName = "cmd.exe";//要执行的程序名称process.StartInfo.UseShellExecute = false;//process.StartInfo.RedirectStandardError = true;process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息process.StartInfo.RedirectStandardInput = true; //可能接受来自调用程序的输入信息process.Start();//启动程序process.StandardInput.WriteLine(c);process.StandardInput.AutoFlush = true;process.StandardInput.WriteLine("exit");}catch { }}} 在类里面的注释很清楚,不再过多赘述了。

然后我们看怎么调用:

/// <summary>/// 上传视频文件并截取缩略图/// </summary>/// <param name="file">视频文件</param>/// <param name="model">MongoDB里面的Document对象</param>/// <returns></returns>[AcceptVerbs(HttpVerbs.Post)]public ActionResult Index(HttpPostedFileBase file){uploadModel model = new uploadModel();if (file.ContentLength > 0){string fileName = (file.FileName).Replace(" ", "");//获得保存路径string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"),Path.GetFileName(fileName));file.SaveAs(filePath);string filetext = file.ContentType;model.videoFormat = filetext;model.videoName = fileName;TempData["videoFormat"] = model.videoFormat;model.videoSize = ((file.ContentLength / 1024 / 1024 * 100) / 100).ToString() + "" + "MB";TempData["videoSize"] = model.videoSize;model.video = filePath;model.videoPath = "../../Uploads/" + Path.GetFileName(fileName);model.Id = Guid.NewGuid();TempData["videoID"] = model.Id;string name = fileName.Substring(0, fileName.LastIndexOf("."));model.imgPath = "../../VideoImages/ " + name + ".jpg";//获得视频地址和截图要存的地址给截图方法用string fileNamePath = model.video;string applictionPath = "";//这里写你的ffmpeg所在的绝对路径string targetImgPath = HttpContext.Server.MapPath("~/Uploads") + "\\VideoImages" + "\\" + name + ".jpg";//调用截图方法uploadDB.VideoConverToImg.ConverToImg(applictionPath, fileNamePath, targetImgPath);//添加到mongoDB数据库uploadDB.UploadModel.AddVideo(model);return RedirectToAction("VideoSave", "UpLoad");}return View();} 我是把所要上传的视频一些属性保存到MongoDB中了,这里我们只需重点看截取图片方法的调用过程即可:uploadDB.VideoCoverToImg.ConverToImg(applictionPath,fileNamePath,targetImgPath)。

效果:

上传到指定文件夹的视频及自动截取的缩略图:

你可以很有个性,但某些时候请收敛。

上传视频时自动完成截取缩略图(二)

相关文章:

你感兴趣的文章:

标签云: