Alright. Actually i need mostly the mp4 format. But if it is possible to get for other types as well that would be nice. I just need to read the duration of the file. How can i do that with C# 4.0 ?
So the thing i need is like this video is like : 13 minutes 12 seconds
I can use 3 third party exes too. Like they save the information about the file to a text file. I can parse that text file.
Thank you.
This answer about P/Invoke for Shell32 reminded me of the Windows API Code Pack to access common Windows Vista/7/2008/2008R2 APIs.
It was very easy, using the PropertyEdit demo in the included samples, to figure out the Shell32 API to get various media file properties, like duration.
I assume the same prerequisite applies for having the proper demultiplexers installed, but it was quite simple, as it only required adding references to Microsoft.WindowsAPICodePack.dll
and Microsoft.WindowsAPICodePack.Shell.dll
and the following code:
using Microsoft.WindowsAPICodePack.Shell; using Microsoft.WindowsAPICodePack.Shell.PropertySystem; 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); }
Some common properties for an MPEG-4/AAC audio media file:
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
It's easy to iterate through all properties if you're looking for the available metadata:
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); } }
You could also use windows media player, although it don't support alle file types you requested
using WMPLib; public Double Duration(String file) { WindowsMediaPlayer wmp = new WindowsMediaPlayerClass(); IWMPMedia mediainfo = wmp.newMedia(file); return mediainfo.duration; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With