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

本文关键字:持续时间 方法 媒体文件 获取 是什么 | 更新日期: 2023-09-27 18:25:19

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

假设我有一个1000个文件路径的列表,我想获得每个路径的持续时间,但获得持续时间需要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();
}

可执行代码:

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();

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

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

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

MediaInfo MI;
public Form1()
{
    InitializeComponent();
    MI = new MediaInfo();
    MI.Option("ParseSpeed", "0"); // Advanced information (e.g. GOP size, captions detection) not needed, request to scan as fast as possible
    MI.Option("ReadByHuman", "0"); // Human readable strings are not needed, no noeed to spend time on them
}
private void button1_Click(object sender, EventArgs e)
{
    MI.Open(textBox1.Text);
    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)
    MI.Close();
}

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

Jérôme,MediaInfo 的开发者