好的。其實我最需要的是MP4格式。但是,如果有可能獲得其他類型,那也不錯。我只需要閱讀文件的持續時間。我如何用C#4.0來做到這一點?如何從mp4,wmv,flv,mov視頻獲取視頻時長
所以我需要的東西是這樣的視頻是這樣的:13 minutes 12 seconds
我可以使用3個第三方前男友了。就像他們將有關文件的信息保存到文本文件一樣。我可以解析該文本文件。
謝謝。
好的。其實我最需要的是MP4格式。但是,如果有可能獲得其他類型,那也不錯。我只需要閱讀文件的持續時間。我如何用C#4.0來做到這一點?如何從mp4,wmv,flv,mov視頻獲取視頻時長
所以我需要的東西是這樣的視頻是這樣的:13 minutes 12 seconds
我可以使用3個第三方前男友了。就像他們將有關文件的信息保存到文本文件一樣。我可以解析該文本文件。
謝謝。
您可以使用DirectShow的API MediaDet
對象,通過DirectShow.NET包裝庫。代碼示例請參閱Getting length of video,get_StreamLength
以秒爲單位給出您的持續時間。這假定Windows安裝了MPEG-4解複用器(需要第三方組件,Windows之前7,我相信這同樣適用於另一個answer by cezor,雖然有免費重新分配組件)。
你好。我無法添加mediainfodll作爲參考。你知道爲什麼嗎 ?這裏的錯誤消息:http://img707.imageshack.us/img707/8130/hataow.png – MonsterMMORPG 2012-04-17 12:27:38
@MonsterMMORPG:你必須用params調用應用程序並獲得輸出,不能在你的解決方案中用它作爲參考 – Marco 2012-04-17 12:41:55
我認爲你正在尋找FFMPEG - http://www.ffmpeg-csharp.com/
也有,你可以在這個問題上讀到過一些免費的替代品 - Using FFmpeg in .net?
FFMpeg.NET FFMpeg-Sharp FFLib.NET
你可以看到這個鏈接例如使用FFMPEG並查找持續時間 - http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/
public VideoFile GetVideoInfo(string inputPath)
{
VideoFile vf = null;
try
{
vf = new VideoFile(inputPath);
}
catch (Exception ex)
{
throw ex;
}
GetVideoInfo(vf);
return vf;
}
public void GetVideoInfo(VideoFile input)
{
//set up the parameters for video info
string Params = string.Format("-i {0}", input.Path);
string output = RunProcess(Params);
input.RawInfo = output;
//get duration
Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)");
Match m = re.Match(input.RawInfo);
if (m.Success)
{
string duration = m.Groups[1].Value;
string[] timepieces = duration.Split(new char[] { ':', '.' });
if (timepieces.Length == 4)
{
input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
}
}
}
這是一個支付有申請。他們只提供試用。這對我不起作用:( – MonsterMMORPG 2012-04-17 12:17:55
@MonsterMMORPG看看我的編輯.. – 2012-04-17 12:27:28
謝謝,但正如我所說的,他們只提供試用版。 – MonsterMMORPG 2012-04-17 12:28:43
你也可以使用Windows媒體播放器,雖然它不支持全部的文件類型,你要求
using WMPLib;
public Double Duration(String file)
{
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
IWMPMedia mediainfo = wmp.newMedia(file);
return mediainfo.duration;
}
}
謝謝,但該應用程序將在Windows Server 2008上工作。我想他們沒有Windows媒體播放器。 – MonsterMMORPG 2012-04-17 12:27:58
@MonsterMMORPG看起來你是對的http://stackoverflow.com/a/13929540/188926 – Dunc 2015-09-24 09:05:40
FFMPEG項目有一個名爲ffprobe的工具,它可以爲您提供所需的關於多媒體文件的信息,並將信息輸出到一個很好的格式化的JSON中。
查看this answer的示例。
This answer關於P/Invoke for Shell32使我想起Windows API Code Pack來訪問常見的Windows Vista/7/2008/2008R2 API。
使用包含的示例中的PropertyEdit演示來計算Shell32 API以獲取各種媒體文件屬性(如持續時間)非常簡單。
我承擔同樣的先決條件適用於具有安裝了合適的解複用器,但它是非常簡單的,因爲它僅需要添加引用到Microsoft.WindowsAPICodePack.dll
和Microsoft.WindowsAPICodePack.Shell.dll
和下面的代碼:
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
using (ShellPropertyCollection properties = new ShellPropertyCollection(filePath))
{
foreach (IShellProperty prop in properties)
{
string value = (prop.ValueAsObject == null) ? "" : prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
Console.WriteLine("{0} = {1}", prop.CanonicalName, value);
}
}
using (ShellObject shell = ShellObject.FromParsingName(filePath))
{
// alternatively: shell.Properties.GetProperty("System.Media.Duration");
IShellProperty prop = shell.Properties.System.Media.Duration;
// Duration will be formatted as 00:44:08
string duration = prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
}
對於一些常見的屬性MPEG-4/AAC音頻媒體文件:
System.Audio.Format = {00001610-0000-0010-8000-00AA00389B71}
System.Media.Duration = 00:44:08
System.Audio.EncodingBitrate = ?56kbps
System.Audio.SampleRate = ?32 kHz
System.Audio.SampleSize = ?16 bit
System.Audio.ChannelCount = 2 (stereo)
System.Audio.StreamNumber = 1
System.DRM.IsProtected = No
System.KindText = Music
System.Kind = Music
非常整潔,但持續時間分辨率是秒 - 我也需要分數。請注意,任何嘗試此操作的人,都需要將NuGet引用添加到Windows7APICodePack-Core/Shell。 – 2017-01-18 16:20:15
非常感謝您的回答! – 2017-01-31 13:39:14
愛它:)我在這一點上關於CodePack。請注意,該屬性的原始值似乎爲10 -7秒......我不知道是否爲所有文件類型提供了實際的詳細信息,但您可以查找它。原始值位於屬性「.ValueAsObject」中,並且您需要從一個通用對象中刪除這個值。我將編輯答案以嘗試添加此內容。 – 2018-01-29 02:51:09
我發現NReco.VideoInfo library是最好的選擇,遠比一些上述的簡單。這是一個簡單的給庫中的文件路徑和它吐出來的是元數據:
var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;
使用Windows Media Player組件也,我們可以得到視頻的持續時間。
下面的代碼片段可以幫助你們:
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
,不要忘記添加的
wmp.dll
的參考,這將是 出現在System32
文件夾。
StreamReader errorreader;
string InterviewID = txtToolsInterviewID.Text;
Process ffmpeg = new Process();
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.FileName = Server.MapPath("ffmpeg.exe");
ffmpeg.StartInfo.Arguments = "-i " + Server.MapPath("videos") + "\\226.flv";
ffmpeg.Start();
errorreader = ffmpeg.StandardError;
ffmpeg.WaitForExit();
string result = errorreader.ReadToEnd();
string duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);
這個DirectShot API在哪裏?你也可以給我這個第三部分的網址嗎?或者你的意思是像k lite mega編解碼器包? – MonsterMMORPG 2012-04-17 12:37:10
好吧試了一個文件,它顯示了持續時間NAN:D – MonsterMMORPG 2012-04-17 12:42:00
感謝這一行給出了正確的持續時間秒數:mediaDet.get_StreamLength(out mediaLength); – MonsterMMORPG 2012-04-17 12:47:23