C#獲取視頻文件播放長度


添加引用SHELL32.DLL

根據網上代碼改編

    public static class GetVideoLength
    {
        public static string GetMediaTimeLen(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路徑               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名稱             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
                if (Environment.OSVersion.Version.Major >= 6)
                { 
                    return folder.GetDetailsOf(folderitem, 27);
                }
                else 
                { 
                    return folder.GetDetailsOf(folderitem, 21);
                }
            }
            catch (Exception ex) { return null; }
        }

        public static int GetMediaTimeLenSecond(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路徑               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名稱             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
                string len;
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    len =  folder.GetDetailsOf(folderitem, 27);
                }
                else
                {
                    len = folder.GetDetailsOf(folderitem, 21);                    
                }

                string[] str = len.Split(new char[] { ':' });
                int sum = 0;
                sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);

                return sum;
            }
            catch (Exception ex) { return 0; }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM