0

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?

Vitalii
  • 4,434
  • 4
  • 35
  • 77
  • Mp3 files with vbr and without proper xing headers might be calculated incorrectly. More info: http://stackoverflow.com/questions/383164/how-to-retrieve-duration-of-mp3-in-net/13269914#13269914 – Daniel Mošmondor Nov 09 '12 at 16:53

3 Answers3

2

From the documentation:

Depending on the source format, the duration might not be exact. For example, if the source contains a variable bit-rate (VBR) stream, the method might return an estimated duration.

Are you using a VBR stream, by any chance?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi, no, it is fixed-bitrate MP3 file; so, as I understand, documentation never promises that duration will be correct, but how Windows Media Player knows duration? Are there more reliable ways to get exact duration of media file with DirectShow API? – Vitalii Mar 01 '12 at 13:18
  • @VASoftOnline: Hmm... don't know in that case. I would really have expected it to be okay if it's fixed-rate... – Jon Skeet Mar 01 '12 at 13:23
2

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.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • You reversed IMediaPosition and IMediaSeeking. The documentation at http://msdn.microsoft.com/en-us/library/dd406977(VS.85).aspx indicates that "applications should use IMediaSeeking instead of IMediaPosition". – Bruno Martinez Jun 14 '12 at 17:31
  • Well I would say this is a not a good advice. On the graph side both interfaces are available, and `IMediaPosition` is definitely easier to use, not to say that it is derived from `IDispatch`. So the answer stands as stated above. – Roman R. Jun 14 '12 at 18:04
0

You can try the same on a clean windows installation. It might be possible you have a codec(pack) installed which is buggy.

wimh
  • 15,072
  • 6
  • 47
  • 98