[英]What's the fastest way to get an ObjectContext reference from an entity object?
[英]What is the fastest way to get a media file's duration?
我正在研究一個程序,它掃描文件夾中的文件夾,並將它們注冊到另一個需要文件持續時間的系統。 到目前為止我能找到的最佳解決方案是使用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();
}
可執行代碼:
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,但我不能將其限制為。
一些更好的代碼(更喜歡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的開發者
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.