How to get length of an audio file?

Get help with using AutoHotkey and its commands and hotkeys
kadhri
Posts: 49
Joined: 12 Nov 2015, 06:52

How to get length of an audio file?

15 Nov 2017, 14:43

Hi all,
Q1: How can I get length of a Audio file(.wav) file
, and select only top 10 , whose files length more than 5minutes

Q2: I have 3 variabls stored date in syear, smonth and sday (through loop)
Now I want to compare this with today(is a variable for a_now)

Code: Select all

if syear-smonth_sday := today
returns error

How can i compare those

This is for the application https://autohotkey.com/boards/viewtopic.php?f=5&t=39459 I am currentlt working on
Many thanks
BoBo
Posts: 6482
Joined: 13 May 2014, 17:15

Re: How to get length of an audio file?

15 Nov 2017, 15:49

If not extractable via its meta-data, what about doing some math? Filesize, compression rate, ... :?:

https://autohotkey.com/board/topic/9821 ... layerocx7/

Google: site:autohotkey.com wav duration

Code: Select all

sYear := 2017
sMonth := 11
sDay := 15
FormatTime, today,, yyyyMMdd
If (sYear . sMonth . sDay = today)
   MsgBox % "Happy Birthday!"
User avatar
Gio
Posts: 1230
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: How to get length of an audio file?

15 Nov 2017, 16:53

Q1: How can I get length of a Audio file(.wav) file

Code: Select all

FileSelectFile, WAV_FILE,,,Please select an audio file (.wav), *wav
DURATION_IN_THOUSANDTHS_OF_SEC := GetAudioDuration( WAV_FILE )
DURATION_IN_SECONDS := FLOOR(DURATION_IN_THOUSANDTHS_OF_SEC / 1000)
% ((DURATION_IN_SECONDS > 3600) ? (HOURS := floor(DURATION_IN_SECONDS / 3600)) : (HOURS := "00"))
% ((DURATION_IN_SECONDS - (HOURS * 3600) > 0) ? (MINUTES := floor((DURATION_IN_SECONDS - (HOURS * 3600)) / 60)) : (MINUTES := "00"))
MINUTES := SubStr("00" . MINUTES, -1, 2)
% ((DURATION_IN_SECONDS - (HOURS * 3600) - (MINUTES * 60) > 0) ? (SECONDS := floor(DURATION_IN_SECONDS - (HOURS * 3600) - (MINUTES * 60))) : (SECONDS := "00"))
SECONDS := SubStr("00" . SECONDS, -1, 2)
FINAL_DURATION := HOURS . ":" . MINUTES . ":" . SECONDS
MSGBOX % FINAL_DURATION

Return
GetAudioDuration( mFile ) { ; SKAN [url] www.autohotkey.com/forum/viewtopic.php?p=361791#361791[url]

 VarSetCapacity( DN,16 ), DLLFunc := "winmm.dll\mciSendString" ( A_IsUnicode ? "W" : "A" )
 DllCall( DLLFunc, Str,"open " """" mFile """" " Alias MP3", UInt,0, UInt,0, UInt,0 )
 DllCall( DLLFunc, Str,"status MP3 length", Str,DN, UInt,16, UInt,0 )
 DllCall( DLLFunc, Str,"close MP3", UInt,0, UInt,0, UInt,0 )
 
Return DN
}
Max for code above is 99 hours.
, and select only top 10 , whose files length more than 5minutes
Put the code in a loop with the files, have a conditional (if statement) with a continue line to skip any files whose DURATION_IN_THOUSANDTHS_OF_SEC < 300000 and than collect the individual values for the durations in a variable to use the Sort command. Than collect the 10 biggest values from the sorted values.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to get length of an audio file?

15 Nov 2017, 18:06

Here's another way.

Code: Select all

q:: ;files - get length (duration)
vDir1 := "C:\Users\" A_UserName "\Videos"
vOutput := ""
Loop, Files, % vDir1 "\*", F
{
	vPath := A_LoopFileFullPath
	vLength := JEE_FileGetDetail(vPath, "Length") ;this term (e.g. 'Length') may vary depending on your locale
	if !(vLength = "")
		vOutput .= vLength "`t" vPath "`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return

;==================================================

JEE_FileGetDetail(vPath, vDetail)
{
	if !FileExist(vPath)
		return
	SplitPath, vPath, vName, vDir
	oShell := ComObjCreate("Shell.Application")
	oFolder := oShell.Namespace(vDir "\")
	oFilename := oFolder.Parsename(vName)
	Loop
	{
		vDetail2 := oFolder.GetDetailsOf(oFolder.Items, A_Index-1)
		if (vDetail2 = "")
			break
		if (vDetail = vDetail2)
		{
			vValue := oFolder.GetDetailsOf(oFilename, A_Index-1)
			break
		}
	}
	oShell := oFolder := oFilename := ""
	return vValue
}
[EDIT:] New version of the JEE_FileGetDetail function, optimised for retrieving information from multiple files.

Code: Select all

JEE_FileGetDetail(vPath, vDetail)
{
	static oArray := {}
	if !FileExist(vPath)
		return
	SplitPath, vPath, vName, vDir
	oShell := ComObjCreate("Shell.Application")
	oFolder := oShell.Namespace(vDir "\")
	oFilename := oFolder.Parsename(vName)
	if oArray.HasKey("z" vDetail)
	{
		vValue := oFolder.GetDetailsOf(oFilename, oArray["z" vDetail])
		oShell := oFolder := oFilename := ""
		return vValue
	}
	Loop
	{
		vDetail2 := oFolder.GetDetailsOf(oFolder.Items, A_Index-1)
		if (vDetail2 = "")
			break
		if (vDetail = vDetail2)
		{
			vValue := oFolder.GetDetailsOf(oFilename, A_Index-1)
			oArray["z" vDetail] := A_Index-1
			break
		}
	}
	oShell := oFolder := oFilename := ""
	return vValue
}
Last edited by jeeswg on 31 Jan 2018, 02:41, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
kadhri
Posts: 49
Joined: 12 Nov 2015, 06:52

Re: How to get length of an audio file?

15 Nov 2017, 20:53

BoBo wrote:If not extractable via its meta-data, what about doing some math? Filesize, compression rate, ... :?:

https://autohotkey.com/board/topic/9821 ... layerocx7/

Google: site:autohotkey.com wav duration
This is what exactly I am doing right now, but
But, for some files u need accurate length :)
Thanks you BoBo

And jio amd jersey, thanks a ton

Return to “Ask For Help”

Who is online

Users browsing this forum: arczi_87, Bing [Bot], Boss55, Chunjee, Green Astronaut, inseption86, mikeyww, one1tick, XMCQCX and 61 guests