obs studio start and record Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
tangrila

obs studio start and record  Topic is solved

26 Dec 2016, 06:13

Hello so this code here is meant to start obs if it isnt already and then send it a obs configured hotkey in order to start recording
on every loop it checks its recording by reading the obs log file if it isn't - it starts. The problem is that it runs fine with the first instance of obs, but when it is closed and reopened, the RunWait line doesn't really wait for the new instance to load and goes directly to this - If (process_PID != ErrorLevel) - which screws up the next if - it executes the hotkey before the program is loaded.

Any ideas why it ignores the Wait in RunWait ?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#persistent
SetTitleMatchMode,2

/*
	Summary
		The script runs indefinetely every N seconds
			Checks if obs is running if not, runs it
				Starts recording
			Checks the latest RECORDING line in
			the latest log created by obs
			If said line contains STOP
				Starts recording
*/

LoopEveryThisSeconds = 10000 ; set the intervals the script should run
RecordHotKey = F9 ; set configured hotkey from obs here
ObsBit = 32 ; set bit version here 32/64
ObsPath = D:\Programs\obs-studio\bin\%ObsBit%bit ; set your path to obs executable here
LogPath = %A_AppData%\obs-studio\logs
line_regex = Recording\s(\w+)
InitialRun := false
; MsgBox %LogPath% ; debug path

SetTimer, obs, %LoopEveryThisSeconds%
obs:
	; runs obs if not already running
	Process,Exist, obs%ObsBit%.exe ; Sets errorlevel to process PID
    	IfWinNotExist, % "ahk_pid " errorlevel ; Expression for ahk_pid
	{
		RunWait, %ObsPath%\obs%ObsBit%.exe, , , ErrorLevel
	}
	
	If (process_PID != ErrorLevel)
	{
		InitialRun := false
	}
	; gets the process PID 
	process_PID = %ErrorLevel%
	
	; finds the latest log file
	Loop %LogPath%\*.*, 0, 1
	{
		MostRecentLog := (A_Index = 1 ? A_LoopFileFullPath : MostRecentLog)
		FileGetTime MostRecentLogTime, %MostRecentLog%, C
		MostRecentLog := (A_LoopFileTimeModified > MostRecentLogTime ? A_LoopFileFullPath : MostRecentLog)
	}
	; MsgBox %MostRecentLog% debug latest log path
	
	; reads every line from the latest log
	Loop, read, %MostRecentLog%
	{
		RegExMatch(A_LoopReadLine, line_regex, line)
		; line1 holds the captured group 
	}
	
	; MsgBox %line1% ; debug regex match\
	If (line1 == "Stop" || !InitialRun)
	{
		InitialRun := true
		ControlSend, ,{%RecordHotKey%},ahk_pid %process_PID%
	}
User avatar
jeeswg
Posts: 6395
Joined: 19 Dec 2016, 01:58
Location: UK

Re: obs studio start and record

26 Dec 2016, 08:47

One possibility, maybe this line requires 'DetectHiddenWindows, On'.

Code: Select all

    	IfWinNotExist, % "ahk_pid " errorlevel ; Expression for ahk_pid
I wrote a script that did something similar, to start/stop OBS Studio recording, from within Internet Explorer.

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

#IfWinActive ahk_class IEFrame ;internet explorer
Numpad7:: ;test - internet explorer (obs studio) - click button
Numpad9:: ;test - internet explorer (obs studio) - click button
NumpadEnd:: ;test - internet explorer (obs studio) - click button
NumpadHome:: ;test - internet explorer (obs studio) - click button
WinGet, hWnd, ID, OBS 0. ahk_class Qt5QWindowIcon
ControlGet, hCtl, Hwnd,, Qt5QWindowIcon10, % "ahk_id " hWnd
vAccPath := "client.push_button2"
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hCtl)
vText := oAcc.accName(0)

if InStr(vText, "Start")
	if A_ThisHotkey contains % "Home,7"
	{
		oAcc.accDoDefaultAction(0)
		ToolTip, STARTED
	}
	else
		MsgBox, % "error"

if InStr(vText, "Stop")
	if A_ThisHotkey contains % "End,1"
	{
		oAcc.accDoDefaultAction(0)
		ToolTip, ENDED
		Sleep, 2000
		Loop, 10
		{
			Sleep, 1000
			vText := oAcc.accName(0)
			if (vText = "Start Recording")
				break
		}
		ToolTip
	}
	else
		MsgBox, % "error"
return
#IfWinActive
Last edited by jeeswg on 03 Dec 2018, 17:45, edited 3 times 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
tangrila
Posts: 1
Joined: 27 Dec 2016, 20:20

Re: obs studio start and record

27 Dec 2016, 20:30

thanks for the input. Haven't tried with DetectHiddenWindows,
but I did manage to make it work by putting two If statements -
  • one checking if ErrorLevel is 0
  • one to check if obs exists before trying to start recording
here's the code now

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#persistent
SetTitleMatchMode,2

/*
	Summary
		The script runs indefinetely every N seconds
			Checks if obs is running if not, runs it
				Starts recording
			Checks the latest RECORDING line in
			the latest log created by obs
			If said line contains STOP
				Starts recording
	Note: put the script in the folder that
		contains the obs executable.
		To make the script run upon startup place a
		shortcut of it in: 
			(OS DRIVE)C:\users\%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
*/

LoopEveryThisSeconds = 10000 ; set the intervals the script should run
RecordHotKey = F9 ; set configured hotkey from obs here
ObsBit = 32 ; set bit version here 32/64
ObsPath = C:\Program Files (x86)\obs-studio\bin\%ObsBit%bit ; set your path to obs executable here
LogPath = %A_AppData%\obs-studio\logs
line_regex = Recording\s(\w+)
InitialRun := false
; MsgBox %LogPath% ; debug path

SetTimer, obs, %LoopEveryThisSeconds%
obs:
	; runs obs if not already running
	If (ErrorLevel == 0)
	{
		Process,Exist, obs%ObsBit%.exe ; Sets ErrorLevel to process PID
		IfWinNotExist, % "ahk_pid " ErrorLevel
		{
			RunWait, %ObsPath%\obs%ObsBit%.exe, , Min, ErrorLevel
			If (process_PID != ErrorLevel)
			{
				Reload
			}
		}
	}
	
	; gets the process PID 
	process_PID = %ErrorLevel%
	
	; finds the latest log file
	Loop %LogPath%\*.*, 0, 1
	{
		MostRecentLog := (A_Index = 1 ? A_LoopFileFullPath : MostRecentLog)
		FileGetTime MostRecentLogTime, %MostRecentLog%, C
		MostRecentLog := (A_LoopFileTimeModified > MostRecentLogTime ? A_LoopFileFullPath : MostRecentLog)
	}
	; MsgBox %MostRecentLog% debug latest log path
	
	; reads every line from the latest log
	Loop, read, %MostRecentLog%
	{
		RegExMatch(A_LoopReadLine, line_regex, line)
		; line1 holds the captured group 
	}
	
	; MsgBox %line1% ; debug regex match\
	If (line1 == "Stop" || !InitialRun)
	{
		IfWinExist, % "ahk_pid " process_PID
		{
			InitialRun = true
			ControlSend, ,{%RecordHotKey%}, ahk_pid %process_PID%
		}
	}
	
znewman

Re: obs studio start and record

08 Feb 2017, 16:45

Hi Everyone, New to AHK and this code that you provided does a great job at starting the recording but would you guys be able to help me out by showing me how to make OBS continuously start a recording then stop it after 20sec then start a new recording and repeat? "RecordHotKey2" is my stop key.

Thanks in advance!

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#persistent
SetTitleMatchMode,2

/*
	Summary
		The script runs indefinetely every N seconds
			Checks if obs is running if not, runs it
				Starts recording
			Checks the latest RECORDING line in
			the latest log created by obs
			If said line contains STOP
				Starts recording
	Note: put the script in the folder that
		contains the obs executable.
		To make the script run upon startup place a
		shortcut of it in: 
			(OS DRIVE)C:\users\%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
*/
LoopEveryThisSeconds = 20000 ; set the intervals the script should run
RecordHotKey = F9 ; set configured hotkey from obs here
RecordHotKey2 = F8 ; set configured hotkey from obs here
ObsBit = 64 ; set bit version here 32/64
ObsPath = C:\Program Files (x86)\obs-studio\bin\64bit ; set your path to obs executable here
LogPath = C:\Users\znewman\AppData\Roaming\obs-studio\logs
line_regex = Recording\s(\w+)
InitialRun := false
; MsgBox %LogPath% ; debug path

SetTimer, obs, %LoopEveryThisSeconds%
obs:
	; runs obs if not already running
	If (ErrorLevel == 0)
	{
		Process,Exist, obs%ObsBit%.exe ; Sets ErrorLevel to process PID
		IfWinNotExist, % "ahk_pid " ErrorLevel
		{
			RunWait, %ObsPath%\obs%ObsBit%.exe, , Min, ErrorLevel
			If (process_PID != ErrorLevel)
			{
				Reload
			}
		}
	}
	
	; gets the process PID 
	process_PID = %ErrorLevel%
	
	; finds the latest log file
	Loop %LogPath%\*.*, 0, 1
	{
		MostRecentLog := (A_Index = 1 ? A_LoopFileFullPath : MostRecentLog)
		FileGetTime MostRecentLogTime, %MostRecentLog%, C
		MostRecentLog := (A_LoopFileTimeModified > MostRecentLogTime ? A_LoopFileFullPath : MostRecentLog)
	}
	; MsgBox %MostRecentLog% debug latest log path
	
	; reads every line from the latest log
	Loop, read, %MostRecentLog%
	{
		RegExMatch(A_LoopReadLine, line_regex, line)
		; line1 holds the captured group 
	}
	
	; MsgBox %line1% ; debug regex match\
	If (line1 == "Stop" || !InitialRun)
	{
		IfWinExist, % "ahk_pid " process_PID
		{
			InitialRun = true
			ControlSend, ,{%RecordHotKey%}, ahk_pid %process_PID%
		}
	}
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
GitHub: 4GForce

Re: obs studio start and record

09 Feb 2017, 01:26

Did I miss something or thats not the purpose of RunWait ...

You are using it improperly ... Its not meant to wait for the called .exe to RUN .... It actually waits for it to END !!!

Like stated in the docs, RunWait doesn't seem to wait for the .exe to end because that .exe spawns other processes and ends himself instantly.
I don't know about obs but you should know by looking at the task manager.
You would probably need to make other checks to know If obs is actually running ... ( and I can't see where you kill the process either )
https://autohotkey.com/docs/commands/Run.htm wrote: Remarks

Unlike Run, RunWait will wait until Target is closed or exits, at which time ErrorLevel will be set to the program's exit code (as a signed 32-bit integer). Some programs will appear to return immediately even though they are still running; these programs spawn another process.
:terms:
User avatar
jeeswg
Posts: 6395
Joined: 19 Dec 2016, 01:58
Location: UK

Re: obs studio start and record

27 Dec 2018, 14:58

- I've tried to update this script to work for OBS Studio v22.0.2. The code sometimes works/sometimes fails. I haven't found a way to reliably click on the Start/Stop Recording button, in case anyone has any other ideas.
- accDoDefaultAction and WM_LBUTTONDOWN/WM_LBUTTONDBLCLK weren't working, although they did *something* to the button, changing its colour, and ControlClick was working often but not always.

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
;[JEE_AccGetTextAll function]
;Acc: get text from all window/control elements - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=40615

;tested on OBS Studio v22.0.2
;not 100% reliable, works intermittently
q:: ;OBS Studio - toggle Start Recording/Stop Recording
SoundBeep
WinGet, hWnd, ID, OBS ahk_class Qt5QWindowIcon
ControlGet, hCtl, Hwnd,, Qt5QWindowIcon1, % "ahk_id " hWnd
oAcc := Acc_Get("Object", "4.1.2", 0, "ahk_id " hCtl)
vText := oAcc.accName(0)
if (vText = "Start Recording")
	ToolTip, STARTED
else if !(vText = "Stop Recording")
{
	MsgBox, % "ERROR"
	return
}
oRect := Acc_Location(oAcc)

;not working:
;oAcc.accDoDefaultAction(0)

;works intermittently:
WinGetPos, vWinX, vWinY,,, % "ahk_id " hWnd
vPos := Format("x{:i} y{:i}", oRect.x+oRect.w/2-vWinX, oRect.y+oRect.h/2-vWinY)
ControlClick, % vPos, % "ahk_id " hWnd

;not working but WM_LBUTTONDOWN highlights button sometimes:
;WinGetClientPos(vWinX, vWinY,,, "ahk_id " hWnd)
;vPosX := oRect.x+oRect.w/2-vWinX
;vPosY := oRect.y+oRect.h/2-vWinY
;PostMessage, 0x201, 0, % (vPosX & 0xFFFF)|(vPosY<<16),, % "ahk_id " hWnd ;WM_LBUTTONDOWN := 0x201
;PostMessage, 0x202, 0, % (vPosX & 0xFFFF)|(vPosY<<16),, % "ahk_id " hWnd ;WM_LBUTTONUP := 0x202
;PostMessage, 0x203, 0, % (vPosX & 0xFFFF)|(vPosY<<16),, % "ahk_id " hWnd ;WM_LBUTTONDBLCLK := 0x203

if (vText = "Stop Recording")
	Loop, 100
	{
		Sleep, 100
		vText := oAcc.accName(0)
		if (vText = "Start Recording")
			break
	}
oAcc := oRect := ""
;MsgBox, % vText
SoundBeep
SoundBeep
return

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

/*
;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

WinGetClientPos(ByRef X:="", ByRef Y:="", ByRef Width:="", ByRef Height:="", WinTitle:="", WinText:="", ExcludeTitle:="", ExcludeText:="")
{
	local hWnd, RECT
	hWnd := WinExist(WinTitle, WinText, ExcludeTitle, ExcludeText)
	VarSetCapacity(RECT, 16, 0)
	DllCall("user32\GetClientRect", Ptr,hWnd, Ptr,&RECT)
	DllCall("user32\ClientToScreen", Ptr,hWnd, Ptr,&RECT)
	X := NumGet(&RECT, 0, "Int"), Y := NumGet(&RECT, 4, "Int")
	Width := NumGet(&RECT, 8, "Int"), Height := NumGet(&RECT, 12, "Int")
}
*/
- Re. OBS Studio, this script is useful for minimising the window to the system tray, and showing it again.
Sending keystrokes to a background process (hidden) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=59070
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: afe, Bing [Bot], TAC109 and 185 guests