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

来源:互联网 发布:gson 源码 编辑:程序博客网 时间:2024/05/21 19:42

         从上篇文章中我们知道了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)。

     效果:

     

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

     

    不怕不知道就怕不知道,做项目的这段时间最大的感触就是我们经常接触使用的东西每一处都是开发人智慧的结晶。作为一个致力于在IT行业有所作为的我们还是多多思考别人在设计产品功能时时怎么考虑设计的,不断学习才能不断进步。    


4 0
原创粉丝点击