OK, I'll just tell you what I'm trying (persistent timers)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
robinson
Posts: 210
Joined: 12 Sep 2019, 20:28

OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 13:38

Hi, noob here.
OK, I'll just explain what I was trying to accomplish in my previous post.
Because I can't get it to work myself. Can't seem to wrap my head around global variables
(if that's even what I'm supposed to be using).
I'm trying to speed up the process of writing my own subtitle (.srt) files for my own videos.

• First I record the video, featuring my voice.
• Then I listen to the video again, slowly, and type out what I said.
• Then I (theoretically) use AHK to help make the time stamps.
• Lastly I move each subtitle under its respective time stamp, and save the document as .srt

So, how to use AHK to help with the time stamps?
Well, I wanted to use just a single key,
which I press down when I hear myself start speaking the sentence,
and I lift up when I finish speaking the sentence.
And what AHK will do is spit out the starting and ending time for each sentence, down to the millisecond.
Here's a proof of concept, with nonsense time stamps, and without the milliseconds:

Code: Select all

z:: {
	global mySub
	t:=FormatTime(A_Now,"00:00:ss;000")
	SendInput(mySub++ "`n" t)
	KeyWait("z")
}
z UP:: {
	t:=FormatTime(A_Now,"00:00:ss;000")
	SendInput( " --> " t "`n`n")
}
Image

So in this case I'm just using [z] down for the start time stamp,
and [z] up for the ending time stamp,
but I guess I'd need to use another hotkey to start a “timer”,
which the [z] hotkey could go back and reference over and over.
Can someone help me out with this?
Thanks!
User avatar
boiler
Posts: 17180
Joined: 21 Dec 2014, 02:44

Re: OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 13:59

Code: Select all

#Requires AutoHotkey v2.0
z:: {
	static mySub := 1
	Send mySub++ '`n' FormatTime(, 'hh:mm:ss;') . A_MSec
	KeyWait('z')
	Send ' --> ' FormatTime(, 'hh:mm:ss;') . A_MSec '`n`n'
}

Among the things that might not jump out at you: It's much better to use a static variable for mySub rather than a global variable, especially if this were to be used as part of a larger script. Input is the default Send mode in v2, so no reason to use SendInput. Leaving the first parameter blank in FormatTime results in using the current time. There's really no need to use a variable like t when you can use the function result directly.
robinson
Posts: 210
Joined: 12 Sep 2019, 20:28

Re: OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 15:26

@boiler
Thanks for the help, but I actually wanted a timer,
that I can start at the same time as I start playing back my video,
which I can reference back to, over and over with the [z] hotkey.
The current time was just an irrelevant placeholder.
You know what I mean?
N
User avatar
Noitalommi_2
Posts: 265
Joined: 16 Aug 2023, 10:58

Re: OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 15:42

Hi.

Please try this.
Edit: I'm using SendEvent in this script because Notepad can't handle Send/SendInput for me. You can change this if necessary.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance

z:: {

	static Start := A_TickCount
	, Count := 0

	TimeStart := GetTime()
	KeyWait(ThisHotkey)
	TimeEnd := GetTime()

	SendEvent ++Count "`n" TimeStart " --> " TimeEnd "`n`n"

	GetTime() {

		End := A_TickCount
		Ms := (End-Start)/1000
		Time := FormatSeconds(Fl := Floor(Ms))
		Ms := SubStr(StrReplace(Ms - Fl, "0." ,,,, 1), 1, 3)
		return Time ";" ((L := StrLen(Ms))=1 ? Ms "00" : L=2 ? Ms "0" :Ms)

		FormatSeconds(NumberOfSeconds) {
			; https://www.autohotkey.com/docs/v2/lib/FormatTime.htm#ExFormatSec
			time := 19990101
			time := DateAdd(time, NumberOfSeconds, "Seconds")
			return NumberOfSeconds//3600 ":" FormatTime(time, "mm:ss")
		}
	}
}
robinson
Posts: 210
Joined: 12 Sep 2019, 20:28

Re: OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 17:26

@Noitalommi_2
That's really great, definitely getting close now.
Take this output for example:
9
0:00:43;203 --> 0:00:43;296

Is it possible when pressing the key down to get this part:
9
0:00:43;203 -->

and when lifting off to get 0:00:43;296 (and the two carriage returns) ?
Thanks!

---EDIT---

Oh! And also, would you mind changing the 0:00:00;000 format into 00:00:00,000 ?
Apparently it needs to be absolutely precise or it won't function.
Also, would it be possible to reset the numbering and the timer,
if nothing happens for 10 seconds?
Thanks!
User avatar
Noitalommi_2
Posts: 265
Joined: 16 Aug 2023, 10:58

Re: OK, I'll just tell you what I'm trying (persistent timers)

05 May 2024, 23:51

@robinson
I adjusted the formatting and the script reloads after 10 seconds of inactivity, resetting count and time to zero.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance

z:: {

	static Start := A_TickCount
	, Count := 0

	SetTimer Re()=>Reload(), 0
	SendText ++Count "`n" GetTime() " --> "
	KeyWait(ThisHotkey)
	SendText GetTime() "`n`n"
	SetTimer Re, -10000

	GetTime() {

		Time := FormatSeconds(Fl := Floor((Ms := (A_TickCount-Start)/1000)))
		return Time ";" ((L := StrLen(Ms := SubStr(Ms - Fl, 3, 3)))=1 ? Ms "00" : L=2 ? Ms "0" :Ms)
		FormatSeconds(NumberOfSeconds) {
			; https://www.autohotkey.com/docs/v2/lib/FormatTime.htm#ExFormatSec
			time := 19990101
			time := DateAdd(time, NumberOfSeconds, "Seconds")
			return ((r := NumberOfSeconds//3600)<10?"0" r : r) ":" FormatTime(time, "mm:ss")
		}
	}
}
robinson
Posts: 210
Joined: 12 Sep 2019, 20:28

Re: OK, I'll just tell you what I'm trying (persistent timers)

06 May 2024, 07:24

@Noitalommi_2
That's amazing, thanks so much.
I just realised usually it demands a , not a ;,
but no big deal, I can change that myself.
Thanks again!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, Google [Bot], robinson, Smile_ and 29 guests