asp.net 获取音视频时长 的方法
http://www.evernote.com/l/AHPMEDnEd65A7ot_DbEP4C47QsPDYLhYdYg/
日志:
1.第一种方法:
调用:shell32.dll ,win7下可以,window2008 r2 服务器上 不行。(原因不知道,有可能是声卡驱动没有安装?)
//添加引用:COM组件的Microsoft Shell Controls And Automation
引用shell32底层接口c:windows\system32\shell32.dll,vs自动转换成interop.shell32.dll(注:64位系统和32位系统生成的interop.shell32.dll不一样) 参考:http://www.stepday.com/topic/?867
string file = Request.Form["mp3path" ];
Shell32. ShellClass sh = new Shell32.ShellClass();
Folder dir = sh.NameSpace(Path .GetDirectoryName(file));
FolderItem item = dir.ParseName(Path .GetFileName(file));
log.Info( "file:" + file);
string mp3Time = "" ;
if (Environment .OSVersion.Version.Major >= 6)
{
mp3Time = dir.GetDetailsOf(item, 27);
}
else
{
mp3Time = dir.GetDetailsOf(item, 21);
}
sb.Append( "文件路径:" + file + "\r\n");
sb.Append( "<br />");
sb.Append( "服务器的OSVersion.Version.Major:" + Environment.OSVersion.Version.Major);
sb.Append( "用Shell32.dll方式测试文件的时长:" + mp3Time);
sb.Append( "<br />");
MediaInfo MI = new MediaInfo();
MI.Open(file);
string s = MI.Get(StreamKind .Audio, 0, "Duration");
string dateTimeStr = Common.TimeHelper .GetDateTimeStr(Convert.ToInt32(s));
sb.Append( "用mediainfo.dll计算时长:" + dateTimeStr);
一样,也是win7下没有问题,服务器上有问题。
3, 第三种方法:利用:
//用ffmpeg.exe 获取:
sb.Append( "<br />");
string fromffmpeg = Fromffmpeg(file);
sb.Append( "fromffmpeg:" + fromffmpeg);
子方法:
private string Fromffmpeg(string fileName)
{
string duration = "" ;
using (System.Diagnostics.Process pro = new System.Diagnostics. Process())
{
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.ErrorDialog = false;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory +
"ffmpeg.exe";
pro.StartInfo.Arguments = " -i " + fileName;
pro.Start();
System.IO. StreamReader errorreader = pro.StandardError;
pro.WaitForExit(1000);
string result = errorreader.ReadToEnd();
if (!string .IsNullOrEmpty(result))
{
result = result.Substring(result.IndexOf( "Duration: ") +
("Duration: ").Length, ( "00:00:00").Length);
duration = result;
}
return duration;
}
}
到此:成功!服务器ok~