asp.net 獲取音視頻時長 的方法

http://www.evernote.com/l/AHPMEDnEd65A7ot_DbEP4C47QsPDYLhYdYg/shell

日誌:windows

 
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 />" );
 
2.第二種方法:利用: mediainfo .dll:
 
                 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~
 
相關文章
相關標籤/搜索