John Dyer

Technology and web development in curly bracket languages {Javascript, C#, ActionScript}

Retreiving the duration of a WMV in C#

by John Dyer 23. July 2005 06:30

This week, I needed to get read the duration of a few hundred Windows Media Video files for a project. Since I started my development code in ASP and then ASP.NET, I pretty much only know managed code, so I wanted to use a .NET language. I searched google.com for "wmv duration" and "wmv duration C#" and came up with nothing. I found the 10 different Windows Media SDKs, 9 different DirectX SDKs, and a several forum posts asking the question "How can I read the duration of a WMV/WMA in C#", but no answers.

I finally found this post on windows.public.windowsmedia.sdk.  It wasn't quite right, but it got me where I needed to go.

Here's the final code:

using WMPLib; // this file is called Interop.WMPLib.dll
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv"); 
// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);
// write named attributes
for (int i=0; i<mediaInfo.attributeCount; i++) {
Console.WriteLine(mediaInfo.getAttributeName(i) + " = " +  mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}

That's it.

Web Statistics