tags:

views:

1285

answers:

9

How do you get the duration (in minutes and seconds) of an MP3/wav audio file in Delphi ?

A: 

It's been a long time since I played with Dephi.

Try,

FileSize(var aFile)
Secko
That will give the size in bytes, not minutes and seconds.
Charlie Salts
@Charlie Salts - This was posted before OP explained that they wanted minutes/seconds. I'd give Secko a break. Also, FileSize() might be helpful and used in the formula Bruce and I posted about.
sheepsimulator
The question was "How do you get the length of an MP3/wav audio file in Delphi?".
Secko
I would have assumed the poster meant length in time. If they had asked for the length of *any* file, I would have assumed file size. Getting the file size of any file is usually trivial, getting length of an MP3 sometimes is not trivial, hence the question.
Charlie Salts
I agree, but my brain somehow just sent me a picture of the FileSize function.
Secko
+3  A: 

Mp3 are divided into frames like this

You will need to count the number of frames

Eric
+1 for the info on frames.
sheepsimulator
is not as simple as saying. The mp3 file have a lot of information before the frames start...
Tom Brito
+4  A: 

Here is C# implementation that shouldn't be too difficult to translate. Look at the getLengthInSeconds function.

Bruce McGee
+1, the code for computing the length in seconds is identical to what I found.
sheepsimulator
+2  A: 

Not sure this will work, but I found this forum post. I'd compare results with something like winamp to make sure it works.

sheepsimulator
Accurate for CBR (constant bitrate), likely inaccurate for VBR (variable bitrate). In cases where only an approximate time is needed, this is a good idea
Charlie Salts
Although this method assumes you know the bitrate!
Charlie Salts
@Charlie Salts - Your'e correct, the formula won't work in VBR cases, and the forum post has no indication of how to determine the bitrate. Bruce McGee's answer is more complete, and I'd recommend that one over mine.
sheepsimulator
+1  A: 

Go to www.un4seen.com and download bass library you will get a lot of info from the forum section. ;)

Andy
+2  A: 

Under windows there is a reasonably effective way of determining the length of an MP3 file.

This is a huge hack but it seems to work.

Ryan.

//add MPlayer to the uses clause;
//
//add the MP3PlayLength function to an existing form and 
//place a button on the form, linking the button click method to see how it works.

uses MPlayer;

function TForm1.MP3PlayLength(aMP3FileName:string):string;
var
  wMP : TMediaPlayer;
  wLen : Cardinal;
begin
  Try
     wMP := TMediaPlayer.Create(self);
     try
        wMP.Visible := false;
        wMP.parent := self;
        wMP.FileName := aMP3FileName;
        wMP.TimeFormat := tfMilliseconds;
        wMP.DeviceType := dtAutoSelect;
        wMP.Open;
        try
           wLen := trunc(wMP.Length / 1000);
           result := inttostr(wLen div 60)+':'+inttostr(wLen mod 60);
        finally
           wMP.Close;
        end;
     finally
        wMP.free;
     end;
  except
     result := '(err)';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if OpenDialog1.Execute then
      showmessage(MP3PlayLength(OpenDialog1.FileName));
end;
Ryan J. Mills
+1  A: 

Or, try MediaInfo.dll link text.

It's included a Delphi wrapper class. For example:

MediaInfo_Get(Handle, Stream_General, 0, 'Duration', Info_Text, Info_Name)

Other solution DSPack link text

size := FilterGraph.Duration;
Icebob
+2  A: 

I recommend you to use BASS

http://www.un4seen.com/bass.html

BASS is an audio library .. to provide developers with powerful stream (MP3.. OGG.. ) functions. All in a tiny DLL, under 100KB in size.

it's very easy to use

   uses BASS;

   var
      playingChannel: HSTREAM;
      playingLength: Double;
      mp3filename: String;

    begin
      BASS_Init(-1,44100,0,Application.Handle,nil);
      playingChannel:=BASS_StreamCreateFile(FALSE,pchar(mp3filename),0,0,0);
      playingLength:=BASS_ChannelBytes2Seconds(playingChannel,
        BASS_ChannelGetLength(playingChannel,BASS_POS_BYTE));
    end;
PA
A: 

checkout this blog, using ffmpeg for background workaround in a ruby project.

http://blog.ncodedev.com

nukturnal