美文网首页
.net 获取音视频时长

.net 获取音视频时长

作者: allenjcheng | 来源:发表于2021-10-22 15:39 被阅读0次

    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不同)

                   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.dll
    846.3 KB](https://www.evernote.com/shard/s115/sh/cc1039c4-77ae-40ee-8b7f-0db10fe02e3b/42c3c360b8587588/res/e2e7830b-a8eb-4c00-b95a-6c232521fbb4/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.zip
    5.7 MB](https://www.evernote.com/shard/s115/sh/cc1039c4-77ae-40ee-8b7f-0db10fe02e3b/42c3c360b8587588/res/ec77eba4-fcdc-4840-b3e4-101f7df507c8/ffmpeg.zip)

                  //用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~

    相关文章

      网友评论

          本文标题:.net 获取音视频时长

          本文链接:https://www.haomeiwen.com/subject/lechgltx.html