AutoHotkey Community

It is currently May 27th, 2012, 8:03 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: function GetMP3Len
PostPosted: December 26th, 2006, 6:35 am 
Offline

Joined: February 12th, 2005, 8:31 pm
Posts: 82
A user in the german forum asked for a way to determine the length of an MP3 file. I pointed him to winmm.dll. Thalon was so kind to finish it up.

I thought it might be good to share the solution, just in case someone is interested. I took Thalons code and created a function from it. :mrgreen:
Code:
GetMP3Len(In_PathFile)
  {
    ; In_PathFile: Absolute path to your mp3 file - e.g. C:\Music\test.mp3

    ; Set variable capacities for dllcalls
    VarSetCapacity(ShortName, 255)
    VarSetCapacity(Length   , 255)

    ; Transforms FilePath into it's 8.3-Value
    RetValue := DllCall("kernel32.dll\GetShortPathNameA"
                       , "Str", In_PathFile
                       , "Str", ShortName
                       , "UInt", 255)
    ; Open MP3
    RetValue := DllCall("winmm.dll\mciSendStringA"
                       , "Str", "open " . ShortName . " type MPEGVideo alias mp3audio"
                       , "Str", 0
                       , "UInt", 0
                       , "UInt", 0)

    ; Read Length
    RetValue := DllCall("winmm.dll\mciSendStringA"
                       , "Str", "status mp3audio length"
                       , "Str", Length
                       , "UInt", 255
                       , "UInt", 0)

    ; Close MP3
    RetValue := DllCall("winmm.dll\mciSendStringA"
                       , "Str", "close mp3audio"
                       , "Str", 0
                       , "UInt", 0
                       , "UInt", 0)

    ; Trim milliseconds -> just seconds needed
    StringTrimRight, Length, Length, 3     

    ; Convert the specified number of seconds to hh:mm:ss format.
    time = 19990101  ; *Midnight* of an arbitrary date.
    time += %Length%, seconds
    FormatTime, mmss, %time%, mm:ss
    Return mmss ; This method is used to support more than 24 hours worth of sections.
  }


You can use it in your scripts e.g. in a File Loop:

Code:
Loop, C:\MyMP3s\*.mp3
  {
    MP3Len := GetMP3Len(A_LoopFileFullPath)
    MsgBox, Title:`t%A_LoopFileFullPath%`nLength:`t%MP3Len%
  }
...

_________________
Cheers
BBCodeWriterToDo-ListCopyPassage


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 26th, 2006, 6:53 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
:o

Thanks for sharing this. Thanks to Thalon too.
I guess this will be slightly slower than the header parsing method, but a very elegant solution.

Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: function GetMP3Len
PostPosted: December 26th, 2006, 7:52 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 775
Location: Texas, USA
AGermanUser wrote:
A user in the german forum asked for a way to determine the length of an MP3 file. I pointed him to winmm.dll.

winmm.dll returns invalid values for VBR tracks. It always has AFAIK. Anyone know of an alternative program/DLL? Thanks.

Them be my thoughts...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 26th, 2006, 10:18 am 
During a Google Search I found this:
http://www.willwap.co.uk/Programs/vbrfix.php

Maybe it's of help and you can read the information from the header after fixing it. Otherwise try googling for mp3 vbr length.
__________________________
Cheers
AGU


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group