读书人

请教怎么上传视频,并且更改视频的文件

发布时间: 2012-01-19 00:22:28 作者: rapoo

请问如何上传视频,并且更改视频的文件名,最后生成缩略图阿
请问各位adp.net中如何上传视频,最后生成缩略图啊。
能给个源代码吗?

[解决办法]
Google "ffmpeg "
[解决办法]
private void btAdd_Click(object sender, System.EventArgs e)
{
if(this.txtTitle.Text.Trim().Length ==0)
{
this.lblMsg.Text = "标题不能为空 ";
return;
}

string strBaseLocation = System.Configuration.ConfigurationSettings.AppSettings[ "TargetFolder "] ;
string filename = " ";

if (null != uplTheFile.PostedFile && uplTheFile.PostedFile.ContentLength > 0 )
{
if(uplTheFile.PostedFile.ContentLength> 4 * 1024 * 1024)
{
lblMsg.Text = "文件大小超过4M! ";
return;
}
System.IO.FileInfo fi = new System.IO.FileInfo(uplTheFile.PostedFile.FileName);
if(fi.Extension.ToLower() != ".avi " && fi.Extension.ToLower() != ".wmv " && fi.Extension.ToLower() != ".flv ")
{
lblMsg.Text = "文件格式应为AVI, WMV, FLV视频! 现在格式为: " + fi.Extension.ToLower();
return;
}

filename = Guid.NewGuid().ToString() + fi.Name;

try
{
uplTheFile.PostedFile.SaveAs(strBaseLocation + @ "old\ " + filename);
}
catch (Exception ee)
{
throw ee;
}
}

string itemID = Framework.AddNewItem(this._userID,CMS.HandleString(this.txtTitle.Text),
CMS.HandleString(this.txtDesc.Text), CMS.HandleString(this.txtDesc.Text));

string fileName = System.Configuration.ConfigurationSettings.AppSettings[ "FFMpeg "];

string orginalFile = strBaseLocation + @ "old\ " + filename;
string targetFile = strBaseLocation + itemID + ".flv ";
string argu = @ "-i " + orginalFile + " -ab 56 -ar 22050 -b 500 -r 15 -s 480x360 " + targetFile;

try
{
if(! fileName.EndsWith( ".flv "))
{
try
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(fileName,argu);
System.Diagnostics.Process.Start(startInfo);
}
catch
{
throw;
}

System.Threading.Thread.Sleep(6000);
}
else
{
System.IO.File.Copy(orginalFile, targetFile);
}

//picture
argu = @ "-i " + targetFile + " -y -f image2 -ss 08.010 -t 0.001 -s 352x240 " + strBaseLocation + itemID + ".jpg ";
System.Diagnostics.ProcessStartInfo startInfo1 = new System.Diagnostics.ProcessStartInfo(fileName,argu);
System.Diagnostics.Process.Start(startInfo1);
System.Threading.Thread.Sleep(2000);

if(! System.IO.File.Exists(strBaseLocation + itemID + ".jpg "))
{
System.IO.File.Copy(strBaseLocation + "17.jpg ", strBaseLocation + itemID + ".jpg ");
}

}
catch(Exception ee)
{
throw ee;
}

if(System.IO.File.Exists(targetFile) && (new System.IO.FileInfo(targetFile)).Length> 10000)
{
this.lblMsg.Text = " <a href= 'viewitem.aspx?itemid= " + itemID + " '> 添加成功! </a> ";
}
else
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(fileName,argu);
System.Diagnostics.Process.Start(startInfo);



if(System.IO.File.Exists(targetFile) && (new System.IO.FileInfo(targetFile)).Length> 10000)
{
this.lblMsg.Text = " <a href= 'viewitem.aspx?itemid= " + itemID + " '> 添加成功! </a> ";
}
else
{
Framework.DeleteItem(itemID, this._userID);
this.lblMsg.Text = "此文件格式添加失败! ";
}
}

}

[解决办法]
这是我上传视频的方法:
/// <summary>
/// 上传视频
/// </summary>
/// <param name= "Fileup "> </param>
/// <param name= "sSavePath "> </param>
/// <param name= "preFileName "> </param>
/// <param name= "iMaxSize "> </param>
/// <returns> </returns>
public string[] upVideo(ref System.Web.UI.HtmlControls.HtmlInputFile Fileup, string sSavePath, string preFileName, int iMaxSize)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
string strfileoldpath; //上传文件的保存路径
string strfileoldname;//保存后名称

string fileName, fileExtension;
string[] arrayImageUrl = new string[4] { " ", " ", " ", " " };
try
{
//没有文件上传
if (Fileup.PostedFile.FileName == " ")
{
arrayImageUrl[2] = "errorNoFile ";
return arrayImageUrl;

}
HttpPostedFile postedFile = Fileup.PostedFile;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == " ")
{
arrayImageUrl[2] = "errorNoFile ";
return arrayImageUrl;

}
fileExtension = System.IO.Path.GetExtension(fileName);
//判断上传文件类型
string filetype = Fileup.PostedFile.ContentType;
//判断是视频
bool legal=false;
string res = fileExtension.Trim().ToLower();
string[] allKinds= ".3gp/.wmv/.asx/.asf/.rm/.rmvb/.ram/.mpg/.mpeg/.mpe/.dat/.vob/.dv/.mov/.mp4/.m4v/.avi/.mkv/.flv ".Split( '/ ');
for(int i=0;i <allKinds.Length;i++)
{
if(res==allKinds[i])
{
legal=true;
break;
}

}
if (!legal)
{
arrayImageUrl[2] = "errorT ";
return arrayImageUrl;
}

//判断是否大小超过上限
int MaxFile = 0;
MaxFile = Fileup.PostedFile.ContentLength;
if (MaxFile > (iMaxSize * 1024))
{
arrayImageUrl[2] = "errorb ";
return arrayImageUrl;
}

//检查保存文件的目录是否存在,否则创建
strfileoldpath = CheckProductPictureML(sSavePath);
//为文件命名,然后保存
Random ra = new Random();

strfileoldname = preFileName + ra.Next(100000, 999999).ToString() + fileExtension;

strfileoldpath += "\\ " + strfileoldname;



//保存文件
postedFile.SaveAs(strfileoldpath);

arrayImageUrl[0] = sSavePath + "/ " + strfileoldname;

return arrayImageUrl;
}
catch(Exception ex)
{
arrayImageUrl[2] = "error ";
return arrayImageUrl;
}
}
[解决办法]
/// <summary>
/// 检查图片的保存路径是否存在
/// </summary>
/// <returns> </returns>
public string CheckProductPictureML(string sSavePath)
{
string strml=this.Server.MapPath(sSavePath);


if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}

#region maybe Use

/*****************************************************************

//年
strml+= "\\ "+DateTime.Now.Year.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//月
strml+= "\\ "+DateTime.Now.Month.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//日
strml+= "\\ "+DateTime.Now.Day.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
*****************************************************************/
#endregion

return strml;
}

读书人网 >asp.net

热点推荐