熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

上傳視頻並將各種視頻文件轉換成.flv格式

2022-06-13   來源: .NET編程 

  首先我們部署一下文件夾在工程的目錄下新建幾個文件夾如下圖

  UpFiles文件夾是要保存你上傳的文件PlayFiles文件夾是用於你轉換後保存的文件(用於網上播放)
ImgFile文件夾是保存截取視頻文件的圖片然後那兩個mencoder和ffmpeg文件夾是視頻轉換工具此視頻轉換也
可叫做mencoder+ffmpeg視頻轉換
首先在配置文件中給這些文件夾進行路徑的配置如下
 
CODE:
<appSettings>
<!工具文件夾>
<add key=ffmpeg value=ffmpeg/ffmpegexe/>
<add key=mencoder value=mencoder/mencoderexe/>
<add key=mplayer value=mencoder/mplayerexe/>
<!上傳文件的路徑>
<add key=upfile value=UpFiles/>
<!上專文件圖片路徑>
<add key=imgfile value=ImgFile/>
<!上傳文件圖片大小>
<add key=CatchFlvImgSize value=x/>
<add key=widthSize value=/>
<add key=heightSize value=/>
<!轉換後文件路徑>
<add key=playfile value=PlayFiles/>
</appSettings>

  在上傳ASPX頁面中

  CODE:
標題<asp:TextBox ID=txtTitle runat=server Width=px></asp:TextBox>
<asp:RequiredFieldValidator ID=RequiredFieldValidator runat=server ControlToValidate=txtTitle
ErrorMessage=標題不為空></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID=FileUpload runat=server Width=px />
<asp:Button ID=btnUpload runat=server OnClick=btnUpload_Click Text=上傳視頻 Width=px />
文件類型<span >(asf|flv|avi|mpg|gp|mov|wmv|rm|rmvb)</span>
<asp:RegularExpressionValidator ID=imagePathValidator runat=server ErrorMessage=文件類型不正確
ValidationGroup=vgValidation Display=Dynamic ValidationExpression=^[azAZ]:(\\+)(asf|flv|avi|mpg|gp|mov|wmv|rm|rmvb)$
ControlToValidate=FileUpload>
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID=RequiredFieldValidator runat=server ControlToValidate=FileUpload
ErrorMessage=文件不為空></asp:RequiredFieldValidator></div>
<div ></div>
<div>上傳列表</div>

  程序工作的原理是視頻先上傳上去然後跟著就是開始轉換
所以在CS 文件中有
這裡為了方便我是用sqlDatasource數據控件做數據的操作

  CODE:
using System;
using SystemData;
using SystemConfiguration;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
public partial class _Default : SystemWebUIPage
{
// 擴展名定義
string[] strArrFfmpeg = new string[] {asfavimpggpmov };
string[] strArrMencoder = new string[] {wmvrmrmvb };
protected void Page_Load(object sender EventArgs e)
{
}
//
protected void btnUpload_Click(object sender EventArgs e)
{
string upFileName = ;
if (thisFileUploadHasFile)
{
string fileName = PublicMethodGetFileName(thisFileUploadFileName);// GetFileName();
if ((string)Session[file] == fileName)
{
return;
}
upFileName = ServerMapPath(PublicMethodupFile + fileName);
thisFileUploadSaveAs(upFileName);
string saveName = DateTimeNowToString(yyyyMMddHHmmssffff) ;
string playFile = ServerMapPath(PublicMethodplayFile + saveName);
string imgFile = ServerMapPath(PublicMethodimgFile + saveName);
//SystemIOFileCopy(ServerMapPath(PublicMethodplayFile + jpg) ServerMapPath(PublicMethodimgFile+aajpg));
PublicMethod pm = new PublicMethod();
string m_strExtension = PublicMethodGetExtension(thisFileUploadPostedFileFileName)ToLower();
if (m_strExtension == flv)
{//直接拷貝到播放文件夾下
SystemIOFileCopy(upFileName playFile+flv);
pmCatchImg(upFileName imgFile);
}
string Extension = CheckExtension(m_strExtension);
if (Extension == ffmpeg)
{
  pmChangeFilePhy(upFileName playFile imgFile);
}
else if (Extension == mencoder)
{
pmMChangeFilePhy(upFileName playFile imgFile);
}
InsertData(thistxtTitleText fileNamesaveName);
Session[file] = fileName;
}
}
//
private string CheckExtension(string extension)
{
string m_strReturn = ;
foreach (string var in thisstrArrFfmpeg)
{
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 插入數

  據到數據庫中

  private void InsertData(string MediaNamestring fileNamestring saveName)
{
//string name=fileNameSubstring( fileNameLastIndexOf());
string imgName = saveName + jpg;//圖片文件名;
string playName = saveName + flv;
string sqlstr = insert into Media(FMediaNameFMediaUpPathFMediaPlayPathFMediaImgPath) values(@MName@MUppath@MPlaypath@MImgpath);
//string constr = ConfigurationManagerConnectionStrings[sqlcon]ToString();
SqlDataSourceInsertCommand = sqlstr;
SqlDataSourceInsertCommandType = SqlDataSourceCommandTypeText;// CommandTypeText;
SqlDataSourceInsertParametersAdd(MNameMediaName);
SqlDataSourceInsertParametersAdd(MUppathPublicMethodupFile+fileName);
SqlDataSourceInsertParametersAdd(MPlaypathPublicMethodplayFile+playName);
SqlDataSourceInsertParametersAdd(MImgpathPublicMethodimgFile+imgName);
SqlDataSourceInsert();
}
#endregion
}
PublicMethod類如下
在這個類裡面主要是做文件轉換和保存在轉換文件的時候CPU的利用率可以達%
它的主要原理是另起一個進程在轉換的時候你會發現你的進程裡多了一個
using System;
using SystemConfiguration;
/// <summary>
/// Summary description for PublicMethod
/// </summary>
public class PublicMethod:SystemWebUIPage
{
public PublicMethod()
{
}
//文件路徑
public static string ffmpegtool = ConfigurationManagerAppSettings[ffmpeg];
public static string mencodertool = ConfigurationManagerAppSettings[mencoder];
public static string mplayertool = ConfigurationManagerAppSettings[mplayer];
public static string upFile = ConfigurationManagerAppSettings[upfile] + /;
public static string imgFile = ConfigurationManagerAppSettings[imgfile] + /;
public static string playFile = ConfigurationManagerAppSettings[playfile] + /;
//文件圖片大小
public static string sizeOfImg = ConfigurationManagerAppSettings[CatchFlvImgSize];
//文件大小
public static string widthOfFile = ConfigurationManagerAppSettings[widthSize];
public static strin


From:http://tw.wingwit.com/Article/program/net/201311/12705.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.