Attilah
Attilah

Reputation: 17930

Duration of an MP3/wav audio file

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

Upvotes: 5

Views: 7999

Answers (9)

Bruce McGee
Bruce McGee

Reputation: 15334

You can calculate the duration by dividing the size of the file by the bit rate. You can get the bit rate from one of the frame headers. Of course, this won't work for variable rate MP3s, where you can have a different rate for each frame.

Using the Header Layout (it's just four bytes):

  1. Open the MP3 in a stream

  2. Find the beginning of the first frame header by reading until you find the sync header, which has 11 consecutive bits set to 1. This used to be 12, but it was tweaked to allow for MPEG version 2.5.

  3. Determine the MPEG version ID. For the purposes of finding the bit rate, V2.5 is the same as V2

  4. Determine the layer description

  5. Read the bit rate index

  6. Using the MPEG version, layer description and bit rate index, determine the actual bit rate from the bit rate index table in the linked header reference

  7. Divide the file size in kilobits ((8 * size in bytes) / 1000) by the bit rate to get the duration in seconds

I couldn't find a Delphi sample, but here is a C# implementation that uses this technique for reference purposes. See the getLengthInSeconds method.

Upvotes: 7

nukturnal
nukturnal

Reputation: 61

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

http://blog.ncodedev.com

Upvotes: 0

PA.
PA.

Reputation: 29339

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;

Upvotes: 4

Icebob
Icebob

Reputation: 1196

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;

Upvotes: 2

Vivian Mills
Vivian Mills

Reputation: 2562

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;

Upvotes: 2

Andy
Andy

Reputation: 376

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

Upvotes: 1

J. Polfer
J. Polfer

Reputation: 12481

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

Upvotes: 2

Eric
Eric

Reputation: 19863

Mp3 are divided into frames like this

You will need to count the number of frames

Upvotes: 3

Secko
Secko

Reputation: 7716

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

Try,

FileSize(var aFile)

Upvotes: -1

Related Questions