Search code examples
c#.netdirectshowdirectshow.netdirectsound

DirectShow GetDuration gives wrong duration value


I am trying to get media file duration with DirectShow. I use following code (C#):

var seekingParser = filter as IMediaSeeking;
if (seekingParser != null)
{
   long duration;
   if (seekingParser.SetTimeFormat(TimeFormat.MediaTime) == 0
       && seekingParser.GetDuration(out duration) == 0)
       track.Duration = duration / 10000000f;
}

to get media file duration in seconds. However, when I try to open 3-4 mins MP3 files, track.Duration becomes 11-12 mins. I tried on multiple files and effect is always the same. What may be the reason?


Solution

  • You normally use IMediaPosition interface (instead of IMediaSeeking) from the application side. Duration is reported always in seconds. However this is unlikely to make a difference, and what might make it is reading duration from ID3 tags instead, using Windows Media API, ID3 Tag Support.

    Are there more reliable ways to get exact duration of media file with DirectShow API?

    Windows Media Player plays MP3 files through Media Foundation, a non-DirectShow API, so you don't have an option here to expect or do exactly the same from DirectShow.