Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   mp3 duration (https://www.delphipraxis.net/131986-mp3-duration.html)

DmVinny 3. Apr 2009 18:35


mp3 duration
 
Hi,

Pardon me for speaking english, my last attempt at google translation did not work too well ;), I am trying to determine an mp3's duration without loading the file, I need it to work with CBR and VBR.

Thank you.

DeddyH 3. Apr 2009 18:51

Re: mp3 duration
 
Maybe this thread could help (VB, but this does not matter due to using mciSendString).

Gausi 3. Apr 2009 18:59

Re: mp3 duration
 
Hi again. ;-)

You can use Mp3FileUtils for this also.
Delphi-Quellcode:
var  Id3v1Tag: TId3v1Tag;
  Id3v2Tag: TId3v2Tag;
  mpegInfo: TMpegInfo;
  stream: TFileStream;
begin
  stream := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite);
  Id3v2Tag.ReadFromStream(stream); // Read the id3v2Tag first (recommended, reading MPEG-Info will be much slower otherwise)

  // Seek to the end of the ID3Tag, if exists
  if Not Id3v2Tag.exists then
    stream.Seek(0, sobeginning)
  else
    stream.Seek(Id3v2Tag.size, soFromBeginning);

  // read MPEG-Information (like duration, bitrate, and so on)
  MpegInfo.LoadFromStream(Stream);

  // optionally: read the old id3v1-Tag
  Id3v1Tag.ReadFromStream(stream);
  stream.free;
end;
The duration (in seconds) is then stored in mpegInfo.Dauer ("Dauer" is german for "duration" - maybe I should change this. ;-))

DmVinny 3. Apr 2009 20:37

Re: mp3 duration
 
When I try to use loadfromstream i get an access violation. :(

Wishmaster 4. Apr 2009 08:55

Re: mp3 duration
 
Try Audio Tools Library 2.3 it works with CBR and VBR
http://mac.sourceforge.net/atl/


AudioGenie is a fast 32Bit ActiveX-OCX/Dll with over 345 functions to read
audio file information (like Bitrate, Samplerate, Frames, Duration, Version-Number, etc).
http://www.audiogenie.de/en/index.htm

Or

Bass.dll (http://www.un4seen.com/)
Delphi-Quellcode:

function TAudioEngine.Get_MP3_Bitrate: WORD;
const
MPEG_BIT_RATES: array[1..3] of array[1..3] of array[0..15] of word = ((
      { Version 1, Layer I }
      (0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0),
      { Version 1, Layer II }
      (0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0),
      { Version 1, Layer III }
      (0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0)),
      { Version 2, Layer I }
      ((0,32,48, 56, 64, 80, 96,112,128,144,160,176,192,224,256,0),
      { Version 2, Layer II }
      (0, 8,16,24, 32, 40, 48, 56, 64, 80, 96, 112,128,144,160,0),
      { Version 2, Layer III }
      (0, 8,16,24, 32, 40, 48, 56, 64, 80, 96, 112,128,144,160,0)),
      { Version 2.5, Layer I }
      ((0,32,48, 56, 64, 80, 96,112,128,144,160,176,192,224,256,0),
      { Version 2.5, Layer II }
      (0, 8,16,24, 32, 40, 48, 56, 64, 80, 96, 112,128,144,160,0),
      { Version 2.5, Layer III }
      (0, 8,16,24, 32, 40, 48, 56, 64, 80, 96, 112,128,144,160,0)
    ));
var
  framehead, fpos: DWORD;
  Version, layer, bitrate_index: DWORD;
  fHandle : integer;
  FileFS : THandleStream;
begin

    fpos := BASS_StreamGetFilePosition(Channel, BASS_FILEPOS_DECODE) +
            BASS_StreamGetFilePosition(Channel, BASS_FILEPOS_START);

    fHandle := CreateFile(PChar(Cur_FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE,0);
 if fHandle = INVALID_HANDLE_VALUE then
  exit;
   FileFS := THandleStream.Create(fHandle);
   FileFS.Seek(fpos, soFromBeginning);
   FileFS.Read(framehead, SizeOf(framehead));
   FileFS.Free;
  CloseHandle(fHandle);
  framehead := ((framehead and $ff) shl 24) or
               ((framehead and $ff00) shl 8) or
               ((framehead and $ff0000) shr 8) or
               ((framehead and $ff000000)shr 24); // reverse byte order

  if ((framehead shr 21) = $7ff) then // got frame sync
   begin
    layer := (framehead shr 19) and 3;
    version := (framehead shr 17) and 3;
    bitrate_index := (framehead shr 12) and 15;
    Result := MPEG_BIT_RATES[version][layer][bitrate_index];
  end
 else
  Result:= 0;
end;

Only for CBR (Bass.dll)

Delphi-Quellcode:
var
  fFileSize, fBitRate : DWORD;
  fFloatPos : FLOAT;

  fFileSize:= BASS_StreamGetFilePosition(Channel, BASS_FILEPOS_END);
  fFloatPos:= BASS_ChannelBytes2Seconds(Channel, BASS_StreamGetLength(Channel));


        fBitRate:= Trunc((fFileSize /(125 * fFloatPos))+0.5);
       if Odd(fBitRate) then
        Dec(fBitRate);
      Result:= fBitRate;



have fun :wink:

Gausi 4. Apr 2009 09:02

Re: mp3 duration
 
Zitat:

Zitat von DmVinny
When I try to use loadfromstream i get an access violation. :(

Sorry, I forgot to add to create the objects ;-)
Delphi-Quellcode:
Id3v1Tag := TId3v1Tag.Create;
Id3v2Tag := TId3v2Tag.Create;
mpegInfo := TMpegInfo.Create;
This should work.

DP-Maintenance 4. Apr 2009 09:57

DP-Maintenance
 
Dieses Thema wurde von "fkerber" von "Open-Source" nach "Multimedia" verschoben.
I move this thread because it\\\'s not an open source program but a question.


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:47 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz