运行 time/length 文件夹中所有媒体文件的信息
Run time/length info for all media files in a folder
如果文件夹中有多个媒体文件,如下所示:
MediaFiles(folder)
-> file1.mp4
-> file2.mp4
...
当我们 select 所有文件和
Right Click -> Properties
在详细信息选项卡的属性 windows 中,有一个长度字段显示媒体文件的总运行时间,如下所示:
是否可以使用 C# 获取此信息?
您可以使用 windows 媒体对象并加载文件,然后获取其属性。
读取文件夹文件并循环遍历每个文件并加载它们并读取您想要的 属性 并且可能存储在数据库中。
如this link, with the help of the Microsoft.WindowsAPICodePack-Shell nuget package所示,可以得到总长度如下;
static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\to\your\path");
FileInfo[] files = dir.GetFiles("*.mp4");
var totalDuration = files.Sum(v => GetVideoDuration(v.FullName));
}
public static double GetVideoDuration(string filePath)
{
using (var shell = ShellObject.FromParsingName(filePath))
{
IShellProperty prop = shell.Properties.System.Media.Duration;
var t = (ulong)prop.ValueAsObject;
return TimeSpan.FromTicks((long)t).TotalMilliseconds;
}
}
如果文件夹中有多个媒体文件,如下所示:
MediaFiles(folder)
-> file1.mp4
-> file2.mp4
...
当我们 select 所有文件和
Right Click -> Properties
在详细信息选项卡的属性 windows 中,有一个长度字段显示媒体文件的总运行时间,如下所示:
是否可以使用 C# 获取此信息?
您可以使用 windows 媒体对象并加载文件,然后获取其属性。 读取文件夹文件并循环遍历每个文件并加载它们并读取您想要的 属性 并且可能存储在数据库中。
如this link, with the help of the Microsoft.WindowsAPICodePack-Shell nuget package所示,可以得到总长度如下;
static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\to\your\path");
FileInfo[] files = dir.GetFiles("*.mp4");
var totalDuration = files.Sum(v => GetVideoDuration(v.FullName));
}
public static double GetVideoDuration(string filePath)
{
using (var shell = ShellObject.FromParsingName(filePath))
{
IShellProperty prop = shell.Properties.System.Media.Duration;
var t = (ulong)prop.ValueAsObject;
return TimeSpan.FromTicks((long)t).TotalMilliseconds;
}
}