Use FFMPEG and mencoder in ASP. NET to implement video conversion screenshots

Source: Internet
Author: User

I recently used two tools, FFMPEG and mencoder, to create a video playing system. I checked some materials and found that there were a lot of materials, but it was a bit messy, I sorted it out from the beginning and shared it with you:

1. FFMPEG converts video (AVI, WMV, and other formats) to FLV format:

 

/// <Summary> /// convert the video to FLV /// </Summary> /// <Param name = "FILENAME"> path of the uploaded video file (original file) </param> /// <Param name = "playfile"> the path of the converted file (Network playback file) </param> /// <returns>: 1 failed to be returned: 0 </returns> Public int fchangefilevir (string filename, string playfile) {// string widthoffile = "320"; string heightoffile = "240 "; // obtain the path of ffmpeg.exe. The path is configured on the web. config, for example, <add key = "FFMPEG" value = "E: \ aspx1 \ ffmpeg.exe"/> string FFmpeg = server. mappath (publicmethod. ffmpegtool); If ((! System. Io. file. exists (FFMPEG) | (! System. io. file. exists (filename) {return 0;} // get (. FLV) file relative path string flv_file = system. io. path. changeextension (server. mappath (playfile ),". FLV "); // create the FFMPEG process system. diagnostics. processstartinfo filestartinfo = new system. diagnostics. processstartinfo (FFMPEG); // set the background to run. The window filestartinfo is not displayed. windowstyle = system. diagnostics. processwindowstyle. hidden; // run the compile command to synthesize the parameters required by the ffmpeg.exe file. Run the following command to debug FFMPEG 0.4.9 or above: // FFMPEG-I f: \ 01.wmv-AB 56-ar 22050-B 500-R 15-s 320x240 F: \ test. FLV filestartinfo. arguments = "-I" + filename + "-AB 56-ar 22050-B 500-R 15-s" + widthoffile + "X" + heightoffile + "" + flv_file; try {// start to convert system. diagnostics. process. start (filestartinfo);} catch {return 0;} return 1 ;}

2. mencoder converts video (AVI, WMV, and other formats) to FLV format:

 

 

/// <Summary> /// convert the video to FLV /// </Summary> /// <Param name = "FILENAME"> path of the uploaded video file (original file) </param> /// <Param name = "playfile"> the path of the converted file (Network playback file) </param> /// <returns>: 1 failed to be returned: 0 </returns> Public int mchangefilevir (string filename, string playfile) {// string widthoffile = "320"; string heightoffile = "240 "; // obtain the mencoder.exe path. The path is configured on the web. config, such as: <add key = "mencoder" value = "E: \ aspx1 \ mencoder.exe"/> String tool = server. mappath (publicmethod. mencodertool); // you can check whether the mencoder tool and file exist if ((! System. Io. file. exists (FFMPEG) | (! System. io. file. exists (filename) {return 0;} // get (. FLV) file relative path string flv_file = system. io. path. changeextension (server. mappath (playfile ),". FLV "); // create a tool process system. diagnostics. processstartinfo filestartinfo = new system. diagnostics. processstartinfo (Tool); // sets the background to run filestartinfo. windowstyle = system. diagnostics. processwindowstyle. hidden; // run the filestartinfo parameter. arguments = "" + vfilename + "-o" + flv_file + "-Of lavf-lavfopts audio-OAC mp3lame-lameopts ABR: Br = 56-OVC lavc-lavcopts vcodec = FLV: vbitrate = 200: mbd = 2: mv0: trell: v4mv: White: last_pred = 1: Dia =-1: CMP = 0: vb_strategy = 1-VF scale = "+ widthoffile +": "+ heightoffile +"-ofps 12-srate 22050 "; try {// start to convert the system. diagnostics. process. start (filestartinfo);} catch {return 0;} return 1 ;}

3. FFMPEG video content:

 

 

// Video, filename video address, imgfile image Address Public String catchimg (string filename, string imgfile) {// capture the image string FFMPEG = server using FFMPEG. mappath (publicmethod. ffmpegtool); // image name string flv_img = imgfile + ". jpg "; // image size string flvimgsize = publicmethod. sizeofimg; // establish the FFMPEG process system. diagnostics. processstartinfo imgstartinfo = new system. diagnostics. processstartinfo (FFMPEG); // run imgstartinfo in the background. windowstyle = system. diagnostics. processwindowstyle. hidden; // process parameter // FFMPEG-I input. FLV-y-F image2-SS 10.11-T 0.001-s 240x180 catchimg.jpg; imgstartinfo. arguments = "-I" + filename + "-y-F image2-SS 2-vframes 1-s" + flvimgsize + "" + flv_img; try {// start to capture the image system. diagnostics. process. start (imgstartinfo); // wait for a sleep and set system as needed. threading. thread. sleep (5000);} catch {return ";} // If (system. io. file. exists (flv_img) {return flv_img;} return "";}

 

The principle is very simple. In fact, it is to use system. diagnostics. process calls the FFMPEG and mencoder ready-made tools for work. The key is parameter transmission, mainly depending on the degree of familiarity with FFMPEG and mencoder.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.