Retreiving the duration of a WMV in C#

July 23, 2005 | Uncategorized | 9 Comments

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.

9 Responses to “Retreiving the duration of a WMV in C#”

  1. Skooter says:

    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"?

  2. John Dyer says:

    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".

  3. Greg says:

    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;

  4. evil says:

    It works with me
    thanks

  5. mstasp.net says:

    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

  6. mstasp.net says:

    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??

  7. Flavio says:

    Thanks for this code John! It is just what I needed!!!

    "muito obrigado pela ajuda!!"

    VALEUU!!

  8. Jesus says:

    Thank you man, you saved me some time!

Leave a Reply

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by AB-WebLog.com.