feedback
Jul 22 2005

Retreiving the duration of a WMV in C#

by John Dyer

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.

Comments

Skooter January 31. 2008 08:56

Thanks John... helped me a lot. I'm new to the MS-programming-world but "imports" - isn't that VB-code? Shouldn't it read "using"?

Skooter

John Dyer February 1. 2008 10:12

Wow, you found a 3-year old post! Yes, you are correct. I fixed the code sample, so now it has "using" instead of "imports".
John Dyer's last post: Papervision3D Bookshelf

John Dyer

Greg February 7. 2008 11:30

Thanks for this code snippet! It is just what I needed!!! Luckly Skooter found this 3 year old post since that probably boosted its relevance enough for me to find this in Google.

For some reason though, this fails with a "Unable to cast COM object of type..." when I try to call media.duration. The attributeCount, getAttributeName, and getItemInfo methods all work fine though.
IWMPMedia media = player.newMedia(filepath);
duration = media.duration;

Equally strange is that I can get the duration with:
duration = player.newMedia(filepath).duration;

Greg

evil March 14. 2008 03:50

It works with me
thanks

evil

Phil March 18. 2008 21:32

Also, have a look at:

www.codeproject.com/.../readingWMVASFWMA.aspx

Phil

mstasp.net April 3. 2008 07:24

This code works fine in local machine,but when i upload to the server it shows duration=00:00.
why??
I am not getting..
please help me

mstasp.net

mstasp.net April 3. 2008 07:28

This code works fine in local machine,but when i upload to the server it shows duration=00:00.

here is the code

WindowsMediaPlayer wmp = new WindowsMediaPlayer();
IWMPMedia mediaInfo = wmp.newMedia(FileUpload1.PostedFile.FileName);

Response.Write("Duration = " + mediaInfo.durationString


why it is not calculating the duration in the server??

mstasp.net

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading