C # (Asp.net) Upload a video to the FLV format and give the video screenshot code

Source: Internet
Author: User

During video projects, videos are generally converted to the FLV format for playback, and videos are displayed during uploads. For convenience, C # (Asp.net) is provided to upload videos to the FLV format andCode.

The following code adds a video:

String filename = publicmethod. getfilename (_ FILES [I]. filename); // getfilename ();
String upfilename = server. mappath (publicmethod. upfile + filename );
_ FILES [I]. saveas (upfilename );
String savename = datetime. Now. tostring ("yyyymmddhhmmssffff ");
String playfile = server. mappath (publicmethod. playfile + savename );
String imgfile = server. mappath (publicmethod. imgfile + savename );
// System. Io. file. Copy (server. mappath (publicmethod. playfile + "00000002.jpg"), server. mappath (publicmethod. imgfile +" aa.jpg "));
Publicmethod PM = new publicmethod ();
String m_strextension = publicmethod. getextension (_ FILES [I]. filename). tolower ();
If (m_strextension = "FLV ")
{// Directly copy to the playback folder
System. Io. file. Copy (upfilename, playfile + ". FLV ");
PM. catchimg (upfilename, imgfile );
}
String extension = checkextension (m_strextension );
If (extension = "FFMPEG ")
{
PM. changefilephy (upfilename, playfile, imgfile );
// Pm. catchimg (upfilename, imgfile );
}
Else if (extension = "mencoder ")
{
PM. mchangefilephy (upfilename, playfile, imgfile );
PM. catchimg (upfilename, imgfile );
}
Else if (extension = "error ")
{
Page. clientscript. registerstartupscript (this. GetType (), "error", "<script language = 'javascript '> alert (' does not support uploading in this video format! '); Location. href ('addvideo. aspx'); </SCRIPT> ");
}
String videourl = publicmethod. upfile + filename;
String videoflvurl = publicmethod. playfile + savename + ". FLV ";
String videoimg = publicmethod. imgfile + savename + ". jpg ";
Productimage = videourl + "," + videoflvurl + "," + videoimg;

Detailed code of the publicmethod class:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Namespace company. Comm
{
Public class publicmethod
{
Public publicmethod ()
{
//
// Todo: add the constructor logic here
//
}
// File path
Public static string ffmpegtool = "http://www.cnblogs.com/ffmpeg/ffmpeg.exe ";
Public static string mencodertool = "http://www.cnblogs.com/mencoder/mencoder.exe ";
Public static string upfile = "http://www.cnblogs.com/UpFiles" + "/";
Public static string imgfile = "http://www.cnblogs.com/ImgFile" + "/";
Public static string playfile = "http://www.cnblogs.com/PlayFiles" + "/";
// File image size
Public static string sizeofimg = "240x180 ";
// File Size
Public static string widthoffile = "400 ";
Public static string heightoffile = "350 ";
///// Obtain the Object Name
Public static string getfilename (string filename)
{
Int I = filename. lastindexof ("\") + 1;
String name = filename. substring (I );
Return name;
}
// Obtain the file extension
Public static string getextension (string filename)
{
Int I = filename. lastindexof (".") + 1;
String name = filename. substring (I );
Return name;
}
//
# Region // run the FFMPEG video decoding (here is the absolute path)
/// <Summary>
/// Convert the file and save it under the specified folder (here is the absolute path)
/// </Summary>
/// <Param name = "FILENAME"> path of the uploaded video file (original file) </param>
/// <Param name = "playfile"> path of the converted file (Network playback file) </param>
/// <Param name = "imgfile"> path of the image captured from the video file </param>
/// <Returns> success: the virtual address of the image is returned; Failure: An empty string is returned. </returns>
Public String changefilephy (string filename, string playfile, string imgfile)
{
// Obtain the path of ffmpeg.exe. The path is configured in Web. config, for example, <add key = "FFMPEG" value = "E: \ aspx1 \ ffmpeg.exe"/>
String FFMPEG = httpcontext. Current. server. mappath (publicmethod. ffmpegtool );
If ((! System. Io. file. exists (FFMPEG) | (! System. Io. file. exists (filename )))
{
Return "";
}

// Obtain the relative path of the image and (. FLV) file/the path stored in the database, for example,/web/user1/00001.jpg

String flv_file = system. Io. Path. changeextension (playfile, ". FLV ");

// Configure the size in Web. config, for example, <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_img;

Try
{
// Conversion
System. Diagnostics. process. Start (filestartinfo );
//
Catchimg (filename, imgfile );
// System. Diagnostics. process. Start (imgstartinfo );
}
Catch
{
Return "";
}
//
Return "";
}
//
Public String catchimg (string filename, string imgfile)
{
//
String FFMPEG = httpcontext. Current. 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_img;
Try
{
System. Diagnostics. process. Start (imgstartinfo );
}
Catch
{
Return "";
}
//
If (system. Io. file. exists (flv_img ))
{
Return flv_img;
}

Return "";
}
# Endregion
//
# Region // run the FFMPEG video decoding (here is the relative path (virtual)
/// <Summary>
/// Convert the file and save it under the specified folder (here is the relative path)
/// </Summary>
/// <Param name = "FILENAME"> path of the uploaded video file (original file) </param>
/// <Param name = "playfile"> path of the converted file (Network playback file) </param>
/// <Param name = "imgfile"> path of the image captured from the video file </param>
/// <Returns> success: the virtual address of the image is returned; Failure: An empty string is returned. </returns>
Public String changefilevir (string filename, string playfile, string imgfile)
{
// Obtain the path of ffmpeg.exe. The path is configured in Web. config, for example, <add key = "FFMPEG" value = "E: \ aspx1 \ ffmpeg.exe"/>
String FFMPEG = httpcontext. Current. server. mappath (publicmethod. ffmpegtool );
If ((! System. Io. file. exists (FFMPEG) | (! System. Io. file. exists (filename )))
{
Return "";
}

// Obtain the relative path of the image and (. FLV) file/the path stored in the database, for example,/web/user1/00001.jpg
String flv_img = system. Io. Path. changeextension (httpcontext. Current. server. mappath (imgfile), ". jpg ");
String flv_file = system. Io. Path. changeextension (httpcontext. Current. server. mappath (playfile), ". FLV ");

// Configure the size in Web. config, for example, <add key = "catchflvimgsize" value = "240x180"/>
String flvimgsize = publicmethod. sizeofimg;

System. Diagnostics. processstartinfo filestartinfo = new system. Diagnostics. processstartinfo (FFMPEG );
System. Diagnostics. processstartinfo imgstartinfo = new system. Diagnostics. processstartinfo (FFMPEG );

Filestartinfo. windowstyle = system. Diagnostics. processwindowstyle. hidden;
Imgstartinfo. windowstyle = system. Diagnostics. processwindowstyle. hidden;
// This part combines the parameters required by the ffmpeg.exe file. The command passes
// 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;
Imgstartinfo. Arguments = "-I" + filename + "-y-F image2-T 0.001-s" + flvimgsize + "" + flv_img;

try
{< br> system. diagnostics. process. start (filestartinfo);
system. diagnostics. process. start (imgstartinfo);
}< br> catch
{< br> return "";
}

/**/
// Note: After an image is captured successfully, it takes a long time to write data from the memory cache to the disk, which may be 3, 4 seconds or longer;
// This operation requires latency before detection. My server latency is 8 seconds. That is, if the image does not exist for more than 8 seconds, it is considered as a failure;
// Delete the code of the time extension. If there is a message indicating how ffmpeg.exe fails, please let me know. Thank you!
If (system. Io. file. exists (flv_img)
{< br> return flv_img;
}

Return "";
}
# Endregion

# Region // run the mencoder Video Decoder conversion (here is (absolute path ))
Public String mchangefilephy (string vfilename, string playfile, string imgfile)
{
String tool = httpcontext. Current. server. mappath (publicmethod. mencodertool );
// String mplaytool = httpcontext. Current. 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 ");

// Configure the size in Web. config, for example, <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_not_use_ B _frames-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
{
System. Diagnostics. process. Start (filestartinfo );
Catchimg (flv_file, imgfile );
}
Catch
{
Return "";
}
//
Return "";
}
# Endregion

}
}

You can directly copy the above Code to convert the C # (Asp.net) video upload to the FLV format and send it to the video. If you have any questions, go to the official website.Technical Community(Http://bbs.25yi.com. Source: 2.5 billion.

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.