Duration of an MP3 file loaded with Mix_LoadMUS

Hello again.

How do I get the duration of an MP3 file loaded with Mix_LoadMUS function ?

Thank you.

I think that’s what Mix_GetMusicLoopLengthTime is for.

Get the loop time length of music stream, in seconds.

I’m not totally sure so you probably want to test it but I think “loop time” means the time it takes to play the music once. If you specify the number of “loops” (i.e. repeats) to be greater than 1 when calling Mix_PlayMusic then you probably need to multiply by that amount to get the total duration that the music will play for.

1 Like

Thank you for the link !

Mix_Music *music = Mix_LoadMUS(file);
double duration = Mix_GetMusicLoopLengthTime(music);
console_print(“duration: %f.\n”, duration);

I load music from an existing simple MP3 file.

General
CompleteName : D:\Temp\1\music.mp3
Format/String : MPEG Audio
FileSize/String : 7.69 MiB
Duration/String : 8 min 23 s
OverallBitRate_Mode/String : Constant
OverallBitRate/String : 128 kb/s
Encoded_Library/String : LAME3.100

Audio
Format/String : MPEG Audio
Format_Version : Version 1
Format_Profile : Layer 3
Format_Settings : Joint stereo / MS Stereo
Duration/String : 8 min 23 s
BitRate_Mode/String : Constant
BitRate/String : 128 kb/s
Channel(s)/String : 2 channels
SamplingRate/String : 48.0 kHz
FrameRate/String : 41.667 FPS (1152 SPF)
Compression_Mode/String : Lossy
StreamSize/String : 7.69 MiB (100%)
Encoded_Library/String : LAME3.100

The music object is not NULL meaning that there is no load error.

When I use the Mix_GetMusicLoopLengthTime function, it returns -1 size.
Why it does not work ?
Does it mean that SDL_mixer does not support MPEG Layer 3 format ?
Thank you.

I’m starting to think this loop length function is used for something else.

I found the following discussion which describes a feature that might be related to what this function is about.

So if this is not how you get the duration then how do you do it? When searching all I can find is people asking for a way but there doesn’t seem to be one, which is surprising to me.

1 Like

Thank you, Peter, for your efforts to help me.
I also tried to search, but I can not find the way to get duration of an MP3 file.

Do the creators of this library still support their creation ?
It would be good to hear something from them.

You should be able to use Mix_MusicDuration(). It requires you use SDL_Mixer 2.6.0 or later.

The thing to understand about MP3 is that it’s meant not only to be a compressed audio format, but a streamable compressed audio format. As such, it has no file header. It’s just a sequence of audio frames. And since that means there’s no convenient single place to look up the audio length, you have to scan through the file, look at each frame’s header, and add up the length of each frame’s audio.

This also means that you should be able to take an MP3 file, cut it in half, and each half will also be a valid MP3 file (decoders are supposed to ignore any “junk” data at the start of a file until they see a valid audio frame header, as well as any incomplete frames or other data at the end of the file).

1 Like

sjr, thank you very much. It works ! :slight_smile: