Document directory
- Asp.net converts various video files into. FLV formats
Asp.net converts various video files into. FLV formats
First, deploy the folder. Create Several folders under the project directory, for example:
The upfiles folder is the file you uploaded, And the playfiles folder is the file you saved after conversion (used for online playback)
The imgfile folder stores the image of the captured video file, and the two mencoder and FFMPEG folders are video conversion tools.
It can be called mencoder + FFMPEG video conversion.
First, configure the paths for these folders in the configuration file as follows:
[Run code] [copy to clipboard] [±]
Code:
<Deleetask>
<! -- Tool folder -->
<Add key = "FFMPEG" value = "FFMPEG/ffmpeg.exe"/>
<Add key = "mencoder" value = "mencoder/mencoder.exe"/>
<Add key = "mplayer" value = "mencoder/mplayer.exe"/>
<! -- Upload file path -->
<Add key = "upfile" value = "upfiles"/>
<! -- Path of the uploaded file -->
<Add key = "imgfile" value = "imgfile"/>
<! -- Upload File image size -->
<Add key = "catchflvimgsize" value = "240x180"/>
<Add key = "widthsize" value = "400"/>
<Add key = "heightsize" value = "350"/>
<! -- Converted file path -->
<Add key = "playfile" value = "playfiles"/>
</Appsettings>
On the upload. ASPX page
[Run code] [copy to clipboard] [±]
Code:
Title: <asp: textbox id = "txttitle" runat = "server" width = "358px"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator2" runat = "server" controltovalidate = "txttitle"
Errormessage = "title not empty"> </ASP: requiredfieldvalidator>
<Br/>
<Asp: fileupload id = "fileupload1" runat = "server" width = "339px"/>
<Asp: button id = "btnupload" runat = "server" onclick = "btnupload_click" text = "Upload video" width = "70px"/>
File Type <span style = "color: red;"> (. ASF |. FLV |. avi |. MPG |. 3GP |. moV |. WMV |. rm |. rmvb) </span>
<Asp: regularexpressionvalidator id = "imagepathvalidator" runat = "server" errormessage = "incorrect file type"
Validationgroup = "vgvalidation" display = "dynamic"
Validationexpression = "^ [A-Za-Z] :( //. + )(. ASF |. FLV |. avi |. MPG |. 3GP |. moV |. WMV |. rm |. rmvb) $"
Controltovalidate = "fileupload1">
</ASP: regularexpressionvalidator>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server" controltovalidate = "fileupload1"
Errormessage = "file not empty"> </ASP: requiredfieldvalidator> </div>
<Div style = "height: 0px; border-top: solid 1px red; font-size: 0px;"> </div>
<Div> upload list. </div>
The working principle of the program is: upload the video first, and then start conversion.
Therefore, the CS file contains
Here, I use the sqldatasource data control for data operations.
[Run code] [copy to clipboard] [±]
Code:
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;
Public partial class _ default: system. Web. UI. Page
{
// Define the extension
String [] strarrffmpeg = new string [] {"ASF", "Avi", "MPG", "3GP", "mov "};
String [] strarrmencoder = new string [] {"WMV", "RM", "rmvb "};
Protected void page_load (Object sender, eventargs E)
{
}
//
Protected void btnupload_click (Object sender, eventargs E)
{
String upfilename = "";
If (this. fileupload1.hasfile)
{
String filename = publicmethod. getfilename (this. fileupload1.filename); // getfilename ();
If (string) session ["file"] = filename)
{
Return;
}
Upfilename = server. mappath (publicmethod. upfile + filename );
This. fileupload1.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 (this. fileupload1.postedfile. 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 );
}
Else if (extension = "mencoder ")
{
PM. mchangefilephy (upfilename, playfile, imgfile );
}
Insertdata(this.txt title. Text, filename, savename );
Session ["file"] = filename;
}
}
//
Private string checkextension (string extension)
{
String m_strreturn = "";
Foreach (string VaR in this. strarrffmpeg)
{
If (Var = extension)
{
M_strreturn = "FFMPEG"; break;
}
}
If (m_strreturn = "")
{
Foreach (string VaR in strarrmencoder)
{
If (Var = extension)
{
M_strreturn = "mencoder"; break;
}
}
}
Return m_strreturn;
}
# Region insert data to the database
Private void insertdata (string medianame, string filename, string savename)
{
// String name = filename. substring (0, filename. lastindexof ('.'));
String imgname = savename + ". jpg"; // image file name;
String playname = savename + ". FLV ";
String sqlstr = "insert
Media (fmedianame, fmediauppath, fmediaplaypath, fmediaimgpath)
Values (@ mname, @ muppath, @ mplaypath, @ mimgpath )";
// String constr = configurationmanager. connectionstrings ["sqlcon"]. tostring ();
Sqldatasource1.insertcommand = sqlstr;
Sqlperformance1.insertcommandtype = sqlperformancecommandtype. Text; // commandtype. text;
Sqldatasource1.insertparameters. Add ("mname", medianame );
Sqldatasource1.insertparameters. Add ("muppath", publicmethod. upfile + filename );
Sqldatasource1.insertparameters. Add ("mplaypath", publicmethod. playfile + playname );
Sqlperformance1.insertparameters. Add ("mimgpath", publicmethod. imgfile + imgname );
Sqldatasource1.insert ();
}
# Endregion
}
The publicmethod class is as follows:
This class is mainly used for file conversion and storage. The CPU usage can reach 100% during file conversion.
Its main principle is that when another process is switched, you will find that there is one more process.
Using system;
Using system. configuration;
/// <Summary>
/// Summary description for publicmethod
/// </Summary>
Public class publicmethod: system. Web. UI. Page
{
Public publicmethod ()
{
}
// File path
Public static string ffmpegtool = configurationmanager. deleettings ["FFMPEG"];
Public static string mencodertool = configurationmanager. receivettings ["mencoder"];
Public static string mplayertool = configurationmanager. deleettings ["mplayer"];
Public static string upfile = configurationmanager. deleettings ["upfile"] + "/";
Public static string imgfile = configurationmanager. deleettings ["imgfile"] + "/";
Public static string playfile = configurationmanager. deleettings ["playfile"] + "/";
// File image size
Public static string sizeofimg = configurationmanager. receivettings ["catchflvimgsize"];
// File Size
Public static string widthoffile = configurationmanager. receivettings ["widthsize"];
Public static string heightoffile = configurationmanager. receivettings ["heightsize"];
///// 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:/51 aspx/ffmpeg.exe"/>
String FFMPEG = 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 = 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:/51 aspx/ffmpeg.exe"/>
String FFMPEG = 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 (server. mappath (imgfile), ". jpg ");
String flv_file = system. Io. Path. changeextension (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
{
System. Diagnostics. process. Start (filestartinfo );
System. Diagnostics. process. Start (imgstartinfo );
}
Catch
{
Return "";
}
/**/
/// Note: After the image is intercepted successfully, it takes a long time to write data from the memory cache to the disk, which may take 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;
/// This is omitted from the time code. If there is a message indicating how ffmpeg.exe fails, please let me know. Thank you!
If (system. Io. file. exists (flv_img ))
{
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 = 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 ");
// 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
}
The main content of the full text is here