from the previous article we learned how FFmpeg was used. Then this article will introduce you how I call FFmpeg implementation in the video upload while automatically capturing the picture.
First we cannot call FFmpeg directly to implement the desired function is to prevent the cmd command from appearing when the black window is executed. So we can encapsulate a class, and then call the inside method to implement the function simply by passing the parameters.
Here I have written a videoconvertoimg class:
public class Videoconvertoimg {//<summary>///Video capture img format images///</summary> <param name= "applicationpath" >ffmpeg.exe file path </param>///<param Name= "Filenamepath" > Video file path (with text Name) </param>//<param name= "Targetimgnamepath" > Generate img Picture path (with file name) </param> public static void Convertoimg (String Applicationpath, String Filenamepath, String targetimgnamepath) {//string c = appli Cationpath + @ "\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";//Fast CMD (c); -I: Set the input file name//-r: Set frame here to 1 frame//-f: Set the output format//-SS from the specified time//-vcodec: Set the image decoder, no input for the text Pieces of the original same decoder//-vframes settings convert how many frames (frame) of the video//-an do not process the sound }///<summary>//program called CMD.exe, and does not display command Line window interface///////<par Am Name= "c" > cmd command executed </param> private static void Cmd (string c) {try { System.Diagnostics.Process Process = new System.Diagnostics.Process (); Process. Startinfo.createnowindow = true; The program window process is not displayed. Startinfo.filename = "cmd.exe";//The program name to execute process. Startinfo.useshellexecute = false; Process. Startinfo.redirectstandarderror = true; Process. Startinfo.redirectstandardoutput = true;//Gets the output information from the calling program process. Startinfo.redirectstandardinput = true; The input information from the calling program may be accepted by the process. Start ();//Starts the program process. Standardinput.writeline (c); Process. Standardinput.autoflush = true; Process. Standardinput.writeline ("Exit"); } catch {}} }
The annotations in the class are clear and not too much to repeat.
Then we look at how to invoke:
<summary>///upload video files and intercept thumbnails///</summary>//<param name= "file" > video file </param >//<param name= "model" >mongodb inside the Document object </param>//<returns></returns> [Acceptverbs (Httpverbs.post)] public actionresult Index (httppostedfilebase file) {Uploadmod El model = new Uploadmodel (); if (file. ContentLength > 0) {string fileName = (file. FileName). Replace ("", ""); Get the save path 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 "; Get the video address and the address to be saved to the method with string filenamepath = Model.video; String applictionpath = "";//here is the absolute path where your ffmpeg is located string targetimgpath = HttpContext.Server.MapPath ("~/upload S ") +" \\VideoImages "+" \ \ "+ name +". jpg "; Call Method UploadDB.VideoConverToImg.ConverToImg (Applictionpath, Filenamepath, Targetimgpath); Add to MongoDB database UploadDB.UploadModel.AddViDeo (model); Return redirecttoaction ("Videosave", "UpLoad"); } return View (); }
I'm going to upload the video some properties are saved to MongoDB, here we only need to focus on the interception of image method call process can be: UPLOADDB.VIDEOCOVERTOIMG.CONVERTOIMG (Applictionpath,filenamepath,targetimgpath).
Effect:
Videos uploaded to the specified folder and automatically captured thumbnails:
not afraid not to know, do not know, do the project this time the biggest feeling is that we often touch the use of the things each place is the development of wisdom of the crystallization of people. As a commitment to make a difference in the IT industry, we still think a lot of people in the design of product functions, how to consider design, continuous learning to progress.
Automatically capture thumbnails when uploading videos (ii)