获取媒体文件持续时间的最快方法是什么?

Mag*_*ias 4 c# c#-4.0 mediainfo

我正在开发一个程序,该程序扫描放置文件夹中的文件,并将它们注册到另一个需要文件持续时间的系统。到目前为止,我能找到的最佳解决方案是使用 MediaInfo 从标头获取持续时间,但由于某种原因,它往往需要几秒钟才能返回结果。

假设我有 1,000 个文件路径的列表,并且我想获取每个文件路径的持续时间,但获取持续时间需要 15 秒。对列表进行线性迭代只需要 4 个多小时,甚至并行运行 8 个任务也需要半个小时。根据我的测试,这将是最好的情况。

我尝试过使用 MediaInfo DLL 以及调用 .exe,两者似乎都有相似的处理时间。

DLL代码:

MediaInfo MI;

public Form1()
{
    InitializeComponent();
    MI = new MediaInfo();
}

private void button1_Click(object sender, EventArgs e)
{
    MI.Open(textBox1.Text);
    MI.Option("Inform", "Video;%Duration%");
    label2.Text = MI.Inform();
    MI.Close();
}
Run Code Online (Sandbox Code Playgroud)

可执行代码:

Process proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "MediaInfo.exe",
        Arguments = $"--Output=Video;%Duration% \"{textBox1.Text}\"",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

StringBuilder line = new StringBuilder();
proc.Start();

while (!proc.StandardOutput.EndOfStream)
{
    line.Append(proc.StandardOutput.ReadLine());
}

label2.Text = line.ToString();
Run Code Online (Sandbox Code Playgroud)

应该注意的是,正在处理的文件位于网络驱动器上,但我已经测试过检索本地文件的持续时间,并且只快了几秒钟。请注意,该程序必须在 Windows Server 2003 R2 上运行,这意味着仅限 .net 4.0。我将处理的大多数文件都是 .mov,但我不能仅限于此。

小智 5

一些更好的代码(更喜欢 DLL 调用,init 需要时间)以及减少扫描持续时间的选项:

\n\n
MediaInfo MI;\n\npublic Form1()\n{\n    InitializeComponent();\n    MI = new MediaInfo();\n    MI.Option("ParseSpeed", "0"); // Advanced information (e.g. GOP size, captions detection) not needed, request to scan as fast as possible\n    MI.Option("ReadByHuman", "0"); // Human readable strings are not needed, no noeed to spend time on them\n}\n\nprivate void button1_Click(object sender, EventArgs e)\n{\n    MI.Open(textBox1.Text);\n    label2.Text = MI.Get(Stream_Video, "Duration"); //Note: prefer Stream_General if you want the duration of the program (here, you select the duration of the video stream)\n    MI.Close();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

根据您的具体需求(即您不关心很多功能),有多种改进解析时间的可能性,但这是直接添加到 MediaInfo 的代码(例如,对于 MP4/QuickTime 文件,仅获取持续时间可能会花费更少的时间)如果我禁用其他功能,则需要超过 200 毫秒),如果您需要速度,请添加功能请求。

\n\n

J\xc3\xa9r\xc3\xb4me,MediaInfo 开发者

\n