简体   繁体   English

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

[英]What is the fastest way to get a media file's duration?

I am working on a program that scans drop folders for files, and registers them to another system that requires a duration for the file. 我正在研究一个程序,它扫描文件夹中的文件夹,并将它们注册到另一个需要文件持续时间的系统。 The best solution I've been able to find so far is to use MediaInfo to get the duration from the header, but for some reason it tends to take a few seconds to return a result. 到目前为止我能找到的最佳解决方案是使用MediaInfo从头部获取持续时间,但由于某种原因,它往往需要几秒钟才能返回结果。

Suppose I have a list of 1,000 file paths, and I want to get the duration for each one, but getting the duration takes 15 seconds. 假设我有一个包含1,000个文件路径的列表,我希望获得每个文件路径的持续时间,但是获取持续时间需要15秒。 Linear iteration over the list would take just over 4 hours, and even running 8 tasks in parallel would take half an hour. 列表上的线性迭代只需要4个多小时,即使并行运行8个任务也需要半个小时。 With my tests, this would be the best case scenario. 通过我的测试,这将是最好的情况。

I've tried using the MediaInfo DLL as well as calling the .exe, and both seemed to have similar processing times. 我已经尝试使用MediaInfo DLL以及调用.exe,两者似乎都有类似的处理时间。

DLL Code: 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();
}

Executable code: 可执行代码:

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

It should be noted that the files being processed are on a networked drive, but I have tested retrieving the duration of a local file and it was only a few seconds faster. 应该注意的是,正在处理的文件位于网络驱动器上,但我已经测试了检索本地文件的持续时间,并且只有几秒钟的速度。 Note, this program has to run on Windows Server 2003 R2, which means .net 4.0 only. 请注意,此程序必须在Windows Server 2003 R2上运行,这仅表示.net 4.0。 Most of the files I will be processing are .mov but I can't restrict it to that. 我将处理的大多数文件都是.mov,但我不能将其限制为。

Some better code (prefer DLL call, init takes time) with options for reducing the scan duration: 一些更好的代码(更喜欢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();
}

There are several possibilities for improving parsing time depending of your specific needs (ie you don't care of lot of features) but this is code to add directly to MediaInfo (eg for MP4/QuickTime files, getting only the duration could take less than 200 ms if I disable other features), add a feature request if you need speed. 根据您的特定需求(即您不关心许多功能),有多种方法可以改善解析时间,但这是直接添加到MediaInfo的代码(例如,对于MP4 / QuickTime文件,只获取持续时间可能少于如果我禁用其他功能,则为200毫秒),如果需要速度,请添加功能请求

Jérôme, developer of MediaInfo Jérôme,MediaInfo的开发者

apt-get install python-pip
pip install pymediainfo

#!/usr/bin/env python
import pymediainfo
duration = float(pymediainfo.MediaInfo.parse(myfilename).tracks[0])/1000.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM