上傳視頻時自動完成截取縮圖(二)

來源:互聯網
上載者:User

標籤:ffmpeg   cmd   視頻網站   

         從上篇文章中我們知道了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行業有所作為的我們還是多多思考別人在設計產品功能時時怎麼考慮設計的,不斷學習才能不斷進步。    


上傳視頻時自動完成截取縮圖(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.