Trying to convert seconds to minutes Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Trying to convert seconds to minutes

30 Aug 2020, 01:23

Trying to convert seconds to minutes, making an mp3 player. Im very new to programming so im sorry for sloppiness and general bad coding.
This is just snippets, if more is needed just let me know
Thanks in advance to all

Code: Select all


Global AV2	:= ComObjCreate("WMPlayer.OCX") 

Gui 1: Add, Edit, c00FFFF x525 y134 w45 h20 vdisplayV2 ReadOnly +Center, 00.00

SongDuration()
{
	Global
	CurrentMedia := AV2.newMedia( Song_Dir Song )
	AV2duration := CurrentMedia.getItemInfo( "duration" )
	AV2duration /= 60			
	GuiControl,, displayV2, %  format("{:0.2f}", AV2duration)  				;; Ive tried many of the obvious things atleast to me, format time, format seconds function ive found. The closest thing i can get is the lame AV2duration  /= 60  
}

FormatSeconds(NumberOfSeconds)  ; Convert the specified number of seconds to hh:mm:ss format.    		;; Mentioned above
{
    time = 19990101  ; *Midnight* of an arbitrary date.
    time += %NumberOfSeconds%, seconds
    FormatTime, mmss, %time%, mm:ss
    return NumberOfSeconds//3600 ":" mmss
    /*
    ; Unlike the method used above, this would not support more than 24 hours worth of seconds:
    FormatTime, hmmss, %time%, h:mm:ss
    return hmmss
    */
}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to convert seconds to minutes

30 Aug 2020, 12:15

I'd guess it would help to post your full code?! :eh:
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Trying to convert seconds to minutes

30 Aug 2020, 12:29

RedFeather wrote:Trying to convert seconds to minutes
The FormatSeconds function that you posted converts seconds to hours:minutes:seconds. If that's not what you want, let us know exactly what you do want. Regards, Joe
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 13:13

Very well here is the full code. Again my problem is when i use CurrentMedia := AV2.newMedia( Song_Dir Song ) - AV2duration := CurrentMedia.getItemInfo( "duration" ) it returns in seconds i want to convert the seconds to minutes.

Code: Select all

#SingleInstance, Force
#Persistent
#KeyHistory 0
ListLines, off
SetBatchLines, -1
;-Variables---
Global PID := DllCall("GetCurrentProcessId")
Process, Priority, %PID%, RealTime
Global programName := "SongPlayer"
Global Song := "Enter Song To Play"
Global Song_Dir := "Set-Directory"
Global Musicformats := "wav,wma,mp3,mp4"
Global AV2	:= ComObjCreate("WMPlayer.OCX") 
Global SkipTo := "00.00"
Global PauseToggle := 0
Global f_Zip := "C:\SongPlayer\SongPlayer.zip"					; Zip file to be created
Global f_Unz := "C:\SongPlayer\"							; Directory to unzip files
;-------------
;---Start-----
CreateDirectory()
;-------------
;----TrayMenu----
Menu, Tray, NoStandard
Menu, Tray, Add, Reload, Reload
Menu, Tray, Add, Exit, Exit
;----------------
SetTimer, FindSong, 500 
return
;--Functions-----
Gui()
{
	Global 
	Gui 1: Color, c41057D
	Gui 1: +Border
	Gui 1: Margin,5,5
	Gui 1: Font, s9, Comic Sans MS
	Gui 1: Add, Picture, x0 y0 w600 h400, C:\SongPlayer\SongPlayerBG.png
	Gui 1: Add, Edit, x25 y25 w550 h20 vSong +Center,  %Song%
	Gui 1: Add, Button, x25 y60 w60 h20 gSongPath +Center, Play
	Gui 1: Add, Button, x95 y60 w60 h20 gButtonPause +Center, Pause
	Gui 1: Add, Button, x95 y60 w60 h20 gButtonResume +Center +Hidden, Resume
	Gui 1: Add, Button, x165 y60 w60 h20 gButtonStop +Center, Stop
	Gui 1: Add, Button, x365 y60 w90 h20 gFastForward +Center, FastForward
	Gui 1: Add, Button, x265 y60 w90 h20 gRewind +Center, Rewind
	Gui 1: Font, Underline
	Gui 1: Add, Text, c00FFFF x30 y94 gOpenSongDir +BackGroundTrans, Current Directory   :>
	Gui 1: Font, Norm
	Gui 1: Add, Edit, w245 h20 x161 y90 vSong_Dir, %Song_Dir%\
	Gui 1: Add, Button, w60 h20 x420 y90 gSetSongPlayerDirectoryButton, Set Dir
	Gui 1: Font, norm s11
	Gui 1: Add, Edit, c00FFFF x25 y180 w550 h210 ReadOnly +BackGroundTrans, Found Songs - %files%
	Gui 1: Font, norm s9
	Gui 1: Add, Button, w40 h20 x535 y60 gVolUp, Vol+
	Gui 1: Add, Button, w40 h20 x485 y60 gVolDown, Vol-
	Gui 1: Add, Text, c00FFFF x30 y137 +BackGroundTrans, Skip To Time   :>
	Gui 1: Add, Edit, w100 h20 x130 y135 vSkipTo, %SkipTo%
	Gui 1: Add, Button, w60 h20 x240 y135 gSkipToTime, Skip To
	Gui 1: Add, Text, c00FFFF x330 y135 +BackGroundTrans, Play Time :>
	Gui 1: Add, Edit, c00FFFF x398 y134 w55 h20 vdisplay ReadOnly +Center, 00:00
	Gui 1: Add, Text, c00FFFF x460 y135 +BackGroundTrans, Duration :>
	Gui 1: Add, Edit, c00FFFF x525 y134 w45 h20 vdisplayV2 ReadOnly +Center, 00.00
	Gui 1: Add, ActiveX, vAV, WMPLayer.OCX
	Sleep 250
	Gui 1: Show, w600 h400, SongPlayer
	enableGuiDrag(1) 
	SetAppVolume(PID, 20)
}
SongPath()
{
	Global 
	Gui 1: Submit, NoHide
	Sleep 50
	IfNotExist, %Song_Dir%\%Song%
	{
		tooltipN("!!Song Does NOT EXIST!!", mouseX, mouseY, 4000, 1)
	}
	else
	{
		Gui 1: Submit, NoHide
		AV.Url := Song_Dir Song
		if errorlevel = 1
		{
			tooltipN("!!Song NOT FOUND!!", mouseX, mouseY, 4000, 1)
		}
		else
		{
			tooltipN("Playing --   " Song, mouseX, mouseY, 4000, 1)
			Menu, Tray, Tip, % "Playing --   " . Song 
			SongDuration()
			SetTimer, SongTimeSub, 500
			SetTimer, FindSong, 500 ; update every 500 milliseconds
		}
	}
}
ButtonPause()
{
	Global 
	AV.controls.pause
	If !ErrorLevel
	{
	  GuiControl, Hide, Pause
	  GuiControl, Show, Resume
	}
}
ButtonResume()
{
	Global 
	AV.controls.play
	If !ErrorLevel
	{
	  GuiControl, Hide, Resume
	  GuiControl, Show, Pause
	}
}
ButtonStop()
{
	Global 
	AV.controls.Stop
	SetTimer, FindSong, Off
}
FastForward()
{
	Global 
	AV.Controls.currentPosition += 5
	tooltipN("FastForward : 5 Seconds", mouseX, mouseY, 4000, 1)
}
Rewind()
{
	Global 
	AV.Controls.currentPosition -= 5
	tooltipN("Rewound : 5 Seconds", mouseX, mouseY, 4000, 1)
}
SkipToTime()
{
	Global 
	Gui 1: Submit, Nohide
	AV.Controls.currentPosition:= SkipTo * 60
}
FindSong()
{
	Global 
	if AV.Controls.currentPosition = 0.00
	{
		Gui 1: Submit, Nohide
		If FileExist(Song_dir Song)
		{
			SongDuration()
			SongPath()
		}
	}
}
SongDuration()
{
	Global
	CurrentMedia := AV2.newMedia( Song_Dir Song )
	AV2duration := CurrentMedia.getItemInfo( "duration" )
	AV2duration /= 60
	GuiControl,, displayV2, %  format("{:0.2f}", AV2duration)
}
GetAppVolume(PID)
{	
    Local MasterVolume := ""

    IMMDeviceEnumerator := ComObjCreate("{BCDE0395-E52F-467C-8E3D-C4579291692E}", "{A95664D2-9614-4F35-A746-DE8DB63617E6}")
    DllCall(NumGet(NumGet(IMMDeviceEnumerator+0)+4*A_PtrSize), "UPtr", IMMDeviceEnumerator, "UInt", 0, "UInt", 1, "UPtrP", IMMDevice, "UInt")
    ObjRelease(IMMDeviceEnumerator)

    VarSetCapacity(GUID, 16)
    DllCall("Ole32.dll\CLSIDFromString", "Str", "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}", "UPtr", &GUID)
    DllCall(NumGet(NumGet(IMMDevice+0)+3*A_PtrSize), "UPtr", IMMDevice, "UPtr", &GUID, "UInt", 23, "UPtr", 0, "UPtrP", IAudioSessionManager2, "UInt")
    ObjRelease(IMMDevice)

    DllCall(NumGet(NumGet(IAudioSessionManager2+0)+5*A_PtrSize), "UPtr", IAudioSessionManager2, "UPtrP", IAudioSessionEnumerator, "UInt")
    ObjRelease(IAudioSessionManager2)

    DllCall(NumGet(NumGet(IAudioSessionEnumerator+0)+3*A_PtrSize), "UPtr", IAudioSessionEnumerator, "UIntP", SessionCount, "UInt")
    Loop % SessionCount
    {
        DllCall(NumGet(NumGet(IAudioSessionEnumerator+0)+4*A_PtrSize), "UPtr", IAudioSessionEnumerator, "Int", A_Index-1, "UPtrP", IAudioSessionControl, "UInt")
        IAudioSessionControl2 := ComObjQuery(IAudioSessionControl, "{BFB7FF88-7239-4FC9-8FA2-07C950BE9C6D}")
        ObjRelease(IAudioSessionControl)

        DllCall(NumGet(NumGet(IAudioSessionControl2+0)+14*A_PtrSize), "UPtr", IAudioSessionControl2, "UIntP", ProcessId, "UInt")
        If (PID == ProcessId)
        {
            ISimpleAudioVolume := ComObjQuery(IAudioSessionControl2, "{87CE5498-68D6-44E5-9215-6DA47EF883D8}")
            DllCall(NumGet(NumGet(ISimpleAudioVolume+0)+4*A_PtrSize), "UPtr", ISimpleAudioVolume, "FloatP", MasterVolume, "UInt")
            ObjRelease(ISimpleAudioVolume)
        }
        ObjRelease(IAudioSessionControl2)
    }
    ObjRelease(IAudioSessionEnumerator)

    Return Round(MasterVolume * 100)
}
SetAppVolume(pid, MasterVolume)
{
    IMMDeviceEnumerator := ComObjCreate("{BCDE0395-E52F-467C-8E3D-C4579291692E}", "{A95664D2-9614-4F35-A746-DE8DB63617E6}")
    DllCall(NumGet(NumGet(IMMDeviceEnumerator+0)+4*A_PtrSize), "UPtr", IMMDeviceEnumerator, "UInt", 0, "UInt", 1, "UPtrP", IMMDevice, "UInt")
    ObjRelease(IMMDeviceEnumerator)

    VarSetCapacity(GUID, 16)
    DllCall("Ole32.dll\CLSIDFromString", "Str", "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}", "UPtr", &GUID)
    DllCall(NumGet(NumGet(IMMDevice+0)+3*A_PtrSize), "UPtr", IMMDevice, "UPtr", &GUID, "UInt", 23, "UPtr", 0, "UPtrP", IAudioSessionManager2, "UInt")
    ObjRelease(IMMDevice)

    DllCall(NumGet(NumGet(IAudioSessionManager2+0)+5*A_PtrSize), "UPtr", IAudioSessionManager2, "UPtrP", IAudioSessionEnumerator, "UInt")
    ObjRelease(IAudioSessionManager2)

    DllCall(NumGet(NumGet(IAudioSessionEnumerator+0)+3*A_PtrSize), "UPtr", IAudioSessionEnumerator, "UIntP", SessionCount, "UInt")
    Loop % SessionCount
    {
        DllCall(NumGet(NumGet(IAudioSessionEnumerator+0)+4*A_PtrSize), "UPtr", IAudioSessionEnumerator, "Int", A_Index-1, "UPtrP", IAudioSessionControl, "UInt")
        IAudioSessionControl2 := ComObjQuery(IAudioSessionControl, "{BFB7FF88-7239-4FC9-8FA2-07C950BE9C6D}")
        ObjRelease(IAudioSessionControl)

        DllCall(NumGet(NumGet(IAudioSessionControl2+0)+14*A_PtrSize), "UPtr", IAudioSessionControl2, "UIntP", ProcessId, "UInt")
        If (pid == ProcessId)
        {
            ISimpleAudioVolume := ComObjQuery(IAudioSessionControl2, "{87CE5498-68D6-44E5-9215-6DA47EF883D8}")
            DllCall(NumGet(NumGet(ISimpleAudioVolume+0)+3*A_PtrSize), "UPtr", ISimpleAudioVolume, "Float", MasterVolume/100.0, "UPtr", 0, "UInt")
            ObjRelease(ISimpleAudioVolume)
        }
        ObjRelease(IAudioSessionControl2)
    }
    ObjRelease(IAudioSessionEnumerator)
}
CreateDirectory()
{
	Global
	RegExMatch(A_ScriptName, "^(.*?)\.", basename) 
	if Not InStr(FileExist("C:\" basename1), "D")    ; create appdata folder if doesnt exist
	FileCreateDir , %  "C:\" basename1		
	if !FileExist("C:\SongPlayer\SongPlayer.zip")
	{
		MsgBox,4100,, Files Required NOT Found!! Initiate Download?
		IfMsgBox, Yes
		{
			Run, https anonfiles.com /z6hccbQco7/SongPlayer_zip  Broken Link for safety
			MsgBox,4096,, 
			(ltrim
			Once downloaded just place zip file in C:\SongPlayer, on restart app will un-zip the files.
			Press ok to restart
			)
			IfMsgBox, ok
			{
				Reload
				Sleep 1000
			}
			else
			{
				ExitMessage()
			}
		}
		else
		{
			ExitMessage()
		}
	}
	if !FileExist("C:\SongPlayer\SongPlayerBG.png")
	{
		Unpack()

	}
	if FileExist("C:\SongPlayer\SongPlayerBG.png")
	{
		ReadSetSongDirIni() 
		SetSongPlayerDirectory()
		doGetSongs()
		StartMessage()
		Gui()
	}
}
ReadSetSongDirIni() 
{
	Global
	if FileExist("C:\SongPlayer\SetSongPlayerDir.ini")
	{
		IniRead, Song_Dir, C:\SongPlayer\SetSongPlayerDir.ini, CurrentDirectory, Directory, %Song_Dir%
	}
}
SetSongPlayerDirectory()
{
	Global 
	if (Song_Dir = "Set-Directory")
	{
		FileSelectFolder, Song_Dir,,0, Select Your Music Directory
		if ErrorLevel = 1
		{
			ExitMessage()
		}
		else
		{
			IniWrite, %Song_Dir%, C:\SongPlayer\SetSongPlayerDir.ini, CurrentDirectory, Directory
		}
	}
}
SetSongPlayerDirectoryButton()
{
	Global 
	Gui 1: Submit, NoHide
	tooltipN("Song Directory Set To : " Song_Dir, mouseX, mouseY, 1000, 1)
	FileSelectFolder, Song_Dir,,0, Choose Music Directory
	if ErrorLevel = 1
	{
		ExitApp
	}
	else
	{
		IniWrite, %Song_Dir%, C:\SongPlayer\SetSongPlayerDir.ini, CurrentDirectory, Directory
		Reload
	}
}
doGetSongs()
{
	Global 
	Loop % Song_Dir . "\*.*",, 1
	If (InStr(Musicformats,A_LoopFileExt) <> 0)
	{
		files = %files%`n%A_LoopFileName%
	}
}
StartMessage()
{
	Global
	Menu, Tray, Icon, shell32.dll, 138
	TrayTip, %programName%, Has Started!
}
ExitMessage()
{
	Global
	TrayTip, %programName%, Has Closed!
	ExitApp
}
Unpack()
{
	Unz(f_Zip, f_Unz)
	Sleep, 500
}
Unz(sZip, sUnz)
{
	psh := ComObjCreate("Shell.Application")
	psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
}
enableGuiDrag(GuiLabel=1) 
{
	WinGetPos,,,A_w,A_h,A
	Gui, %GuiLabel%:Add, Text, x0 y0 w%A_w% h%A_h% +BackgroundTrans gGUI_Drag
	return
	
	GUI_Drag:
	PostMessage 0xA1,2 
	return
}
ToolTipN(__tooltipText:="N/A", X := "", Y := "", __removeDelay:=0, __whichToolTip:=1) 
{
	static __n := 0

	Tooltip % __tooltipText, % X, % Y, % __whichToolTip
	__f := Func("removeToolTip").bind(__whichToolTip, ++__n) ; creates a function reference and binds it __whichToolTip  which will be passed as argument for the callback function below
	SetTimer, % __f, % -__removeDelay ; minus sign indicates settimer to run only once
	return
}
removeToolTip(__whichToolTip, __n) 
{
	tooltip,,,, % __whichToolTip ; if tooltip text argument is empty the toottip is removed
}
PauseToggle()
{
	Global
	PauseToggle:=!PauseToggle
	if PauseToggle = 1
	{
		PauseToggle = 1
		ButtonPause()
	}
	else
	{
		PauseToggle = 0
		ButtonResume()
	}
}
FormatSeconds(NumberOfSeconds)  ; Convert the specified number of seconds to hh:mm:ss format.
{
    time = 19990101  ; *Midnight* of an arbitrary date.
    time += %NumberOfSeconds%, seconds
    FormatTime, mmss, %time%, mm:ss
    return NumberOfSeconds//3600 ":" mmss
    /*
    ; Unlike the method used above, this would not support more than 24 hours worth of seconds:
    FormatTime, hmmss, %time%, h:mm:ss
    return hmmss
    */
}
;---------------
;--Labels-------
VolUp:
SetAppVolume(PID, GetAppVolume(PID) + 5)
tooltipN("SongPlayer Volume : " GetAppVolume(PID), mouseX, mouseY, 4000, 1)
return

VolDown:
SetAppVolume(PID, GetAppVolume(PID) - 5)
tooltipN("SongPlayer Volume : " GetAppVolume(PID), mouseX, mouseY, 4000, 1)
return

OpenSongDir:
Run, %Song_Dir%
return

GuiClose:
ExitMessage()
return

Reload:
Reload
return

Exit:
ExitMessage()
return

RemoveToolTip:
ToolTip
return

SongTimeSub:
AV.Controls.currentPositionString
GuiControl,, display, % AV.Controls.currentPositionString
return
;----------------
;----HotKeys-----
~*^C::
KeyWait, C
KeyWait, C, D, T 0.10
if Errorlevel = 1
return
else
{
	Sleep 50
	GuiControl,, Song, % Clipboard
	Gui 1: Submit, NoHide
}
return

~*Pause::
PauseToggle()
return

~*NumpadAdd::
SetAppVolume(PID, + 5)
return

~*NumpadSub::
SetAppVolume(PID, - 5)
return
;----------------
Last edited by RedFeather on 30 Aug 2020, 19:50, edited 1 time in total.
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Trying to convert seconds to minutes

30 Aug 2020, 13:52

RedFeather wrote:it returns in seconds i want to convert the seconds to minutes
I haven't looked at the code you posted, but what exactly do you want for "minutes"? For example, suppose the number of seconds is 165. Do you want the conversion to yield 2 (minutes truncated)? Or 3 (minutes rounded)? Or 2.75 (minutes with decimal places)? Or 2:45 (mm:ss)? Or something else?
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Trying to convert seconds to minutes  Topic is solved

30 Aug 2020, 14:39

example , seconds to > hh:mm:ss

Code: Select all

acc:=165
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%hours1%:%minutes1%:%seconds1%
msgbox,%acc%
return
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Trying to convert seconds to minutes

30 Aug 2020, 14:55

garry wrote:example , seconds to > hh:mm:ss
Hi Garry,
The FormatSeconds function in RedFeather's first post already does that conversion, i.e., seconds to hh:mm:ss...question is...is that what he wants? If not, what conversion does he want? Regards, Joe
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:10

aargh, yes , sorry , maybe he don't know how to use the function
otherwise use my complicated script and then use Guicontrol to display hh:mm:ss
but the easiest way is , how to use the function ...

Code: Select all

;- Trying to convert seconds to minutes 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80485

;AV2duration := CurrentMedia.getItemInfo( "duration" )
;- for test :
AV2duration:=165
acc:=AV2duration
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%hours1%:%minutes1%:%seconds1%
msgbox,%acc%
GuiControl,1:,displayV2, %aac%
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:34

JoeWinograd wrote:
30 Aug 2020, 13:52
RedFeather wrote:it returns in seconds i want to convert the seconds to minutes
I haven't looked at the code you posted, but what exactly do you want for "minutes"? For example, suppose the number of seconds is 165. Do you want the conversion to yield 2 (minutes truncated)? Or 3 (minutes rounded)? Or 2.75 (minutes with decimal places)? Or 2:45 (mm:ss)? Or something else?
I would just like it to display the standard mm:ss, I apologize if im not saying it in a correct way or just not being clear. So for example I play a 4 min 44 sec song, this particular bit "getItemInfo( "duration" )" will return 266.4 seconds. Ive not found a way to format the returned seconds in to mm:ss. I thought it would have been as simple as dividing the returned seconds by 60, the problem there is that it doesnt recognize that 60 seconds = 1 minute, it counts to 100 then rolls over. There could be a easy solution for this I just dont know it or how to achieve it. Again sorry if it sounds like i dont know what im talking about, thats because i dont.
Last edited by gregster on 30 Aug 2020, 15:47, edited 1 time in total.
Reason: This post was approved after Joe's following post.
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:39

garry wrote:don't know how to use the function
Hi Garry,
You may be right! Your comment inspired me to look at the code he posted and, sure enough, there is no call to the FormatSeconds function in it.

Hi RedFeather,
Based on garry's excellent comment, I noticed that you do not call the FormatSeconds function anywhere in your full code...you must do that to perform the conversion of seconds. Here's a code snippet showing how to do it (using the FormatSeconds function as you posted it):

Code: Select all

Seconds:=165
hms:=FormatSeconds(Seconds)
MsgBox % hms
ExitApp

FormatSeconds(NumberOfSeconds)
{
    time = 19990101  ; *Midnight* of an arbitrary date.
    time += %NumberOfSeconds%, seconds
    FormatTime, mmss, %time%, mm:ss
    return NumberOfSeconds//3600 ":" mmss
}
Regards, Joe
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:41

garry wrote:
30 Aug 2020, 15:10
aargh, yes , sorry , maybe he don't know how to use the function
otherwise use my complicated script and then use Guicontrol to display hh:mm:ss
but the easiest way is , how to use the function ...

Code: Select all

;- Trying to convert seconds to minutes 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80485

;AV2duration := CurrentMedia.getItemInfo( "duration" )
;- for test :
AV2duration:=165
acc:=AV2duration
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%hours1%:%minutes1%:%seconds1%
msgbox,%acc%
GuiControl,1:,displayV2, %aac%
This displayes 2:45 in the message box no matter the song, idk if i placed it in the right spot also does not display in the gui.

Code: Select all

SongDuration()
{
	Global
	CurrentMedia := AV2.newMedia( Song_Dir Song )
	AV2duration := CurrentMedia.getItemInfo( "duration" )
	;AV2duration /= 60
	AV2duration:=165
	acc:=AV2duration
	SetFormat, float, 02
	hours1   := (acc//3600)
	minutes1 := (acc - hours1 * 3600)// 60
	seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
	hours1  +=0.00
	minutes1+=0.00
	seconds1+=0.00
	acc=%hours1%:%minutes1%:%seconds1%
	msgbox,%acc%
	GuiControl,,displayV2, %aac%
}
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:48

JoeWinograd wrote:
30 Aug 2020, 15:39
garry wrote:don't know how to use the function
Hi Garry,
You may be right! Your comment inspired me to look at the code he posted and, sure enough, there is no call to the FormatSeconds function in it.

Hi RedFeather,
Based on garry's excellent comment, I noticed that you do not call the FormatSeconds function anywhere in your full code...you must do that to perform the conversion of seconds. Here's a code snippet showing how to do it (using the FormatSeconds function as you posted it):

Code: Select all

Seconds:=165
hms:=FormatSeconds(Seconds)
MsgBox % hms
ExitApp

FormatSeconds(NumberOfSeconds)
{
    time = 19990101  ; *Midnight* of an arbitrary date.
    time += %NumberOfSeconds%, seconds
    FormatTime, mmss, %time%, mm:ss
    return NumberOfSeconds//3600 ":" mmss
}
Regards, Joe
Thank you Garry and Joe!!! This works but it returns like this 0.000000:02:58 The very last bit 2:58 is correct for the song!!!!! How would i remove the first bit thought?
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Trying to convert seconds to minutes

30 Aug 2020, 15:52

must remove at least this or try example from JoeWinograd
AV2duration:=165 ;<- this was for test
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 16:00

garry wrote:
30 Aug 2020, 15:52
must remove at least this or try example from JoeWinograd
AV2duration:=165 ;<- this was for test
Thank you garry you were both right, i just dont know what im doing i.e i dont know how to use the function. So this works it just returns the hour as well it looks like 0.000000:02:58. How do i tell it to leave the hour out or make it to where to hour doesnt display so many zeros?

Code: Select all

	CurrentMedia := AV2.newMedia( Song_Dir Song )
	AV2duration := CurrentMedia.getItemInfo( "duration" )
	Seconds:=AV2duration
	ms:=FormatSeconds(Seconds)
	MsgBox % ms
	GuiControl,, displayV2, % 	ms	
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 16:07

garry wrote:
30 Aug 2020, 14:39
example , seconds to > hh:mm:ss

Code: Select all

acc:=165
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%hours1%:%minutes1%:%seconds1%
msgbox,%acc%
return
WOOWOO!! Got your code to work!! Seriously Thank you to Joe and Garry!! You guys are awesome. This works!

Code: Select all

	
	CurrentMedia := AV2.newMedia( Song_Dir Song )
	AV2duration := CurrentMedia.getItemInfo( "duration" )
	SetFormat, float, 02
	hours1   := (AV2duration//3600)
	minutes1 := (AV2duration - hours1 * 3600)// 60
	seconds1 := (AV2duration - hours1 * 3600 - minutes1 * 60)
	hours1  +=0.00
	minutes1+=0.00
	seconds1+=0.00
	AV2duration=%hours1%:%minutes1%:%seconds1%
	;msgbox,%acc%
	GuiControl,, displayV2, % AV2duration
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Trying to convert seconds to minutes

30 Aug 2020, 16:14

EDIT : I see it works , if wanted can remove the variable 'hours1'
I get here the correct result ( without the dot , dot must be removed maybe with stringreplace )

Code: Select all

acc:=00000165
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%minutes1%:%seconds1%
msgbox,%acc%
;GuiControl,1:,displayV2, %aac%
return
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 16:28

garry wrote:
30 Aug 2020, 16:14
EDIT : I see it works , if wanted can remove the variable 'hours1'
I get here the correct result ( without the dot , dot must be removed maybe with stringreplace )

Code: Select all

acc:=00000165
SetFormat, float, 02
hours1   := (acc//3600)
minutes1 := (acc - hours1 * 3600)// 60
seconds1 := (acc - hours1 * 3600 - minutes1 * 60)
hours1  +=0.00
minutes1+=0.00
seconds1+=0.00
acc=%minutes1%:%seconds1%
msgbox,%acc%
;GuiControl,1:,displayV2, %aac%
return
Thank you very much!
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Trying to convert seconds to minutes

30 Aug 2020, 16:55

RedFeather wrote:it returns like this 0.000000:02:58
The FormatSeconds function that you posted does not return that. Try the code snippet that I posted with Seconds:=178 — you'll see that it returns 0:02:58.

There are many ways to achieve objectives in AutoHotkey (or any programming language). Before I saw Garry's last post, I wrote this new FormatSeconds function for you that takes a different approach:

Code: Select all

; test calls on the new FormatSeconds function
Seconds:=3778
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 1:02:58
Seconds:=178
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 02:58
Seconds:=58
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 00:58
ExitApp

FormatSeconds(NumberOfSeconds)
{
  If (NumberOfSeconds<=0)
    Return 0
  Hours:=NumberOfSeconds//3600
  If (Hours=0)
    Hours:=""
  Else
    Hours:=Hours . ":"
  Seconds:=Mod(NumberOfSeconds,3600)
  Minutes:=Seconds//60
  Seconds:=Mod(NumberOfSeconds,60)
  Seconds:=SubStr("00" . Seconds,-1) ; force Seconds to two digits
  Minutes:=SubStr("00" . Minutes,-1) ; force Minutes to two digits
  Return Hours . Minutes . ":" . Seconds
}
Regards, Joe
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to convert seconds to minutes

30 Aug 2020, 17:33

@JoeWinograd - Thx for sharing your code :thumbup: I've reduced a few lines of it. :shifty:

Code: Select all

; test calls on the new FormatSeconds function
MsgBox % hms:=frmSec(3778)		; should show 01:02:58
MsgBox % hms:=frmSec(178)		; should show    02:58
MsgBox % hms:=frmSec(58)		; should show	 00:58
ExitApp

frmSec(noSec) {
	If (noSec<=0)
		Return 0
	Hours	:= (noSec//3600 = 0) ? "" : Format("{:02}", noSec//3600) . ":"	; force Hours to two digits
	Seconds := Format("{:02}", Mod(noSec,60))								; force Seconds to two digits
	Minutes := Format("{:02}", Mod(noSec,3600)//60) 						; force Minutes to two digits
	Return Hours . Minutes . ":" . Seconds
	}
RedFeather
Posts: 17
Joined: 23 Jan 2020, 23:38

Re: Trying to convert seconds to minutes

30 Aug 2020, 17:41

JoeWinograd wrote:
30 Aug 2020, 16:55
RedFeather wrote:it returns like this 0.000000:02:58
The FormatSeconds function that you posted does not return that. Try the code snippet that I posted with Seconds:=178 — you'll see that it returns 0:02:58.

There are many ways to achieve objectives in AutoHotkey (or any programming language). Before I saw Garry's last post, I wrote this new FormatSeconds function for you that takes a different approach:

Code: Select all

; test calls on the new FormatSeconds function
Seconds:=3778
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 1:02:58
Seconds:=178
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 02:58
Seconds:=58
hms:=FormatSeconds(Seconds)
MsgBox % hms ; should show 00:58
ExitApp

FormatSeconds(NumberOfSeconds)
{
  If (NumberOfSeconds<=0)
    Return 0
  Hours:=NumberOfSeconds//3600
  If (Hours=0)
    Hours:=""
  Else
    Hours:=Hours . ":"
  Seconds:=Mod(NumberOfSeconds,3600)
  Minutes:=Seconds//60
  Seconds:=Mod(NumberOfSeconds,60)
  Seconds:=SubStr("00" . Seconds,-1) ; force Seconds to two digits
  Minutes:=SubStr("00" . Minutes,-1) ; force Minutes to two digits
  Return Hours . Minutes . ":" . Seconds
}
Regards, Joe
Thank you for taking the time to do this!! Works great!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ishida20, jameswrightesq, Lem2001 and 402 guests