C # implements the method of capturing screenshots from an FLV video file by FFmpeg

Source: Internet
Author: User
Tags flv file

This article describes a C # implementation of a method by ffmpeg from an FLV video file. Share to everyone for your reference. The specific analysis is as follows:

Need to download FFmpeg first, this is open source, the code is as follows:


The code is as follows:

Using System;

Using System.Configuration;

public class PublicMethod:System.Web.UI.Page

{

Public Publicmethod ()

{

}

File path

public static string ffmpegtool = "Ffmpeg/ffmpeg.exe";

public static string mencodertool = "Mencoder/mencoder.exe";

public static string flvtool = "Flvtool/flvtool2.exe";//flv markup Tool

public static string upfile = "Upfiles" + "/";//Upload Folder

public static string imgfile = "Imgfile" + "/";//Picture folder

public static string playfile = "Playfiles" + "/";//flv folder

public static string xmlfile = "Xmlfiles" + "/";//xml folder

public static string sizeofimg = "240x180";//width and height of picture

public static string widthoffile = "n"; width of//flv file

public static string heightoffile = "n"; the height of the//flv file

public static string ffmpegtool = configurationmanager.appsettings["FFmpeg"];

public static string mencodertool = configurationmanager.appsettings["MEncoder"];

public static string upfile = configurationmanager.appsettings["Upfile"] + "/";

public static string imgfile = configurationmanager.appsettings["Imgfile"] + "/";

public static string playfile = configurationmanager.appsettings["Playfile"] + "/";

File picture size

public static string sizeofimg = configurationmanager.appsettings["Catchflvimgsize"];

File size

public static string widthoffile = configurationmanager.appsettings["Widthsize"];

public static string heightoffile = configurationmanager.appsettings["Heightsize"];

//Get the name of the file

Private System.Timers.Timer MyTimer = new System.Timers.Timer (3000);//Timer

public static string flvname = "";

public static string imgname = "";

public static string flvxml = "";

public static int pId = 0;

public static string GetFileName (String fileName)

{

int i = filename.lastindexof ("\") + 1;

String Name = Filename.substring (i);

return Name;

}

Get file name extension

public static string GetExtension (String fileName)

{

int i = Filename.lastindexof (".") +1;

String Name = Filename.substring (i);

return Name;

}

//

#region//Run FFmpeg video decoding, (this is the absolute path)

<summary>

Convert the file and save it under the specified folder (this is the absolute path)

</summary>

<param name= "FileName" > The path to the uploaded video file (original file) </param>

<param name= "Playfile" > The path of the converted file (network play file) </param>

<param name= "Imgfile" > Picture paths captured from video files </param>

<returns> success: Return image virtual address; Failed: returned empty string </returns>

public void Changefilephy (string fileName, String playfile, String imgfile)

{

Get Ffmpeg.exe path, path configured in Web. config, such as: <add key= "FFmpeg" value= "E:aspx1ffmpeg.exe"/>

String ffmpeg = Server.MapPath (Publicmethod.ffmpegtool);

if ((! System.IO.File.Exists (ffmpeg)) | | (! System.IO.File.Exists (fileName)))

{

Return

}

Get picture and (. flv) file relative path/last path stored to database, such as:/web/user1/00001.jpg

String flv_file = System.IO.Path.ChangeExtension (Playfile, ". flv");

Size, configured in Web. config, such as: <add key= "catchflvimgsize" value= "240x180″/>

string flvimgsize = publicmethod.sizeofimg;

System.Diagnostics.ProcessStartInfo filestartinfo = new System.Diagnostics.ProcessStartInfo (ffmpeg);

Filestartinfo.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;

filestartinfo.arguments = "-I" + FileName + "-ab 56-ar 22050-b 500-r 15-s" + widthoffile + "X" + Heightoffile + " "+ flv_file;

imgstartinfo.arguments = "-I" + FileName + "-y-f image2-t 0.05-s" + flvimgsize + "" + Flv_im G

Try

{

Transformation

System.Diagnostics.Process.Start (Filestartinfo);

//

Catchimg (FileName, imgfile);

System.Diagnostics.Process.Start (Imgstartinfo);

}

Catch

{

}

}

#endregion

#region

public string catchimg (String filename,string imgfile)

{

//

String ffmpeg = Server.MapPath (Publicmethod.ffmpegtool);

//

String flv_img =imgfile+ ". jpg";

//

string flvimgsize = publicmethod.sizeofimg;

//

System.Diagnostics.ProcessStartInfo imgstartinfo = new System.Diagnostics.ProcessStartInfo (ffmpeg);

Imgstartinfo.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//

imgstartinfo.arguments = "-I" + FileName + "-y-f image2-ss 2-vframes 1-s" + flvimgsize + "" + flv_i mg

Try

{

System.Diagnostics.Process.Start (Imgstartinfo);

}

Catch

{

Return "";

}

//

Catchflvtool (FileName);

if (System.IO.File.Exists (flv_img))

{

return flv_img;

}

Return "";

}

#endregion

#region//Run FFmpeg video decoding, (here is (virtual) relative path)

<summary>

Convert the file and save it under the specified folder (here is the relative path)

</summary>

<param name= "FileName" > The path to the uploaded video file (original file) </param>

<param name= "Playfile" > The path of the converted file (network play file) </param>

<param name= "Imgfile" > Picture paths captured from video files </param>

<returns> success: Return image virtual address; Failed: returned empty string </returns>

public void Changefilevir (string fileName, String playfile, String imgfile)

{

Get Ffmpeg.exe path, path configured in Web. config, such as: <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

}

Get picture and (. flv) file relative path/last path stored to database, such as:/web/user1/00001.jpg

String flv_img = System.IO.Path.ChangeExtension (Server.MapPath (Imgfile), ". jpg");

String flv_file = System.IO.Path.ChangeExtension (Server.MapPath (Playfile), ". flv");

Size, configured in Web. config, such as: <add key= "catchflvimgsize" value= "240x180″/>

string flvimgsize = publicmethod.sizeofimg;

System.Diagnostics.ProcessStartInfo filestartinfo = new System.Diagnostics.ProcessStartInfo (ffmpeg);

Filestartinfo.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;

This is combined into the parameters required by the Ffmpeg.exe file, where the command is FFmpeg 0.4.9 debug through

Ffmpeg-i F:\01.wmv-ab 56-ar 22050-b 500-r 15-s

filestartinfo.arguments = "-I" + FileName + "-ab 56-ar 22050-b 500-r 15-s" + widthoffile + "X" + Heightoffile + " "+ flv_file;

Try

{

System.Diagnostics.Process PS = new System.Diagnostics.Process ();

Ps. StartInfo = Filestartinfo;

Ps. Start ();

Session.add ("ProcessID", Ps. ID);

Session.add ("flv", flv_file);

Session.add ("img", Imgfile);

mytimer.elapsed + = new System.Timers.ElapsedEventHandler (mytimer_test);

Mytimer.enabled = true;

}

Catch

{

}

}

#endregion

#region//Run MEncoder Video Decoder conversion (this is (absolute path))

public void Mchangefilephy (string vfilename, String playfile, String imgfile)

{

String tool = Server.MapPath (Publicmethod.mencodertool);

String mplaytool = Server.MapPath (Publicmethod.ffmpegtool);

if ((! System.IO.File.Exists (tool)) | | (! System.IO.File.Exists (vfilename)))

{

Return

}

String flv_file = System.IO.Path.ChangeExtension (Playfile, ". flv");

Size, configured in Web. config, such as: <add key= "catchflvimgsize" value= "240x180″/>

string flvimgsize = publicmethod.sizeofimg;

System.Diagnostics.ProcessStartInfo filestartinfo = new System.Diagnostics.ProcessStartInfo (tool);

Filestartinfo.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;

Filestartinfo.arguments = "" + Vfilename + "-o" + flv_file + "-of lavf-lavfopts i_certify_that_my_video_stream_does_n OT_USE_B_FRAMES-OAC mp3lame-lameopts ABR:BR=56-OVC lavc-lavcopts VCODEC=FLV:VBITRATE=200:MBD=2:MV0:TRELL:V4MV:CBP: LAST_PRED=1:DIA=-1:CMP=0:VB_STRATEGY=1-VF scale= "+ widthoffile +": "+heightoffile +"-ofps 12-srate 22050″;

Try

{

System.Diagnostics.Process PS = new System.Diagnostics.Process ();

Ps. StartInfo = Filestartinfo;

Ps. Start ();

Session.add ("ProcessID", Ps. ID);

Session.add ("flv", flv_file);

Session.add ("img", Imgfile);

PId = PS. Id;

Flvname = Flv_file;

Imgname = Imgfile;

mytimer.elapsed + = new System.Timers.ElapsedEventHandler (mytimer_test);

Mytimer.enabled = true;

}

Catch

{

}

}

<summary>

Timer function, Auto Save

</summary>

<param name= "Sender" ></param>

<param name= "E" ></param>

private void Mytimer_test (object sender, System.Timers.ElapsedEventArgs e)

{

if (!object. Equals (NULL, session["ProcessID"]))

{

Try

{

System.Diagnostics.Process PRS = System.Diagnostics.Process.GetProcessById (int. Parse (session["ProcessID"). ToString ()));

if (PRS. hasexited)

{

Catchimg (session["flv"). ToString (), session["IMG"]. ToString ());

Catchflvtool (session["flv"). ToString ());

mytimer.enabled = false;

Mytimer.close ();

Mytimer.dispose ();

Session.Abandon ();

}

}

Catch

{

Catchimg (session["flv"). ToString (), session["IMG"]. ToString ());

Catchflvtool (session["flv"). ToString ());

mytimer.enabled = false;

Mytimer.close ();

Mytimer.dispose ();

Session.Abandon ();

}

}

}

#endregion

public string Catchflvtool (string fileName)

{

//

String flvtools = Server.MapPath (Publicmethod.flvtool);

//

String flv_xml = Filename.replace (". flv", ". xml"). Replace (PublicMethod.upFile.Replace ("/", ""), PublicMethod.xmlFile.Replace ("/", ""));

//

System.Diagnostics.ProcessStartInfo imgstartinfo = new System.Diagnostics.ProcessStartInfo (flvtools);

Imgstartinfo.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//

Imgstartinfo.arguments = "" + filename + "-UPX" + filename + ">" + flv_xml;

Try

{

System.Diagnostics.Process.Start (Imgstartinfo);

}

Catch

{

Return "";

}

//

if (System.IO.File.Exists (Flv_xml))

{

return flv_xml;

}

Return "";

}

}

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Implementation of the method by ffmpeg from an FLV video file

This address: http://www.paobuke.com/develop/c-develop/pbk23094.html






Related content C # ways to get File paths and folder paths using the browse button in C # static void Main (string[] args) parameter example in C # Implement a ComboBox control to display multiple data source Properties C # implements a method for ordering a two-dimensional array
Image Grayscale method of C # Digital Image processing C # methods to obtain the copyright information for a specified file C # method of using LINQ to calculate the number of occurrences of an element in a list IDisposable mode usage in C #

C # Implementation of the method by ffmpeg from an FLV video file

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.