AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function GetMP3Len

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
AGermanUser



Joined: 12 Feb 2005
Posts: 82

PostPosted: Tue Dec 26, 2006 6:35 am    Post subject: function GetMP3Len Reply with quote

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. Mr. Green
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
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7179

PostPosted: Tue Dec 26, 2006 6:53 am    Post subject: Reply with quote

Surprised

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, Smile
_________________
Suresh Kumar A N
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 537
Location: Texas, USA

PostPosted: Tue Dec 26, 2006 7:52 am    Post subject: Re: function GetMP3Len Reply with quote

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...
Back to top
View user's profile Send private message Send e-mail
AGU
Guest





PostPosted: Tue Dec 26, 2006 10:18 am    Post subject: Reply with quote

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group