您的位置:首页 > 编程语言 > C#

C#中获取mp4等媒体文件的播放时长

2017-09-22 16:03 1526 查看
添加一个类

public class FormatConverter
{
/// <summary>
/// 获取媒体文件播放时长
/// </summary>
/// <param name="path">媒体文件路径</param>
/// <returns></returns>
public static string GetMediaTimeLen(string path)
{
try
{

//ShellClass shell = new ShellClass();
Shell32.Shell shell = new Shell32.ShellClass();
//文件路径
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
//文件名称
Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));

return folder.GetDetailsOf(folderitem, 21);

}
catch (Exception ex)
{
return null;
}

}

public static string GetMediaTimes(string SongPath)
{
string dirName = Path.GetDirectoryName(SongPath);
string SongName = Path.GetFileName(SongPath);//获得文件名称
FileInfo fInfo = new FileInfo(SongPath);
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(dirName);
FolderItem item = dir.ParseName(SongName);
return Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取文件时间
}

}


调用

string time = FormatConverter.GetMediaTimes(filefullname);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#