Python - Modifying Window Capture Settings

bradleyp

New Member
I'm trying to write a script to automatically update the Window Capture window when a user changes the current foreground window in Windows. This would eliminate the need to constantly change the currently selected window for recording or for replay buffer. I'm able to use obs_data_set_string, but when I capture using replay buffer, the screen is black as if it's not actually updating.

Here is my script:
Python:
import obspython as obs
import time
import win32gui
import win32process
import psutil

source_name = "Window Capture"

def create_title(pid, window):
    return f'[{pid}]: {window}'

def get_active_window():
    fgWindow = win32gui.GetForegroundWindow()

    fgWindowName = win32gui.GetWindowText(fgWindow)
    pid = win32process.GetWindowThreadProcessId(fgWindow)
    process = psutil.Process(pid[-1]).name()

    title = create_title(process, fgWindowName)
    return title

def change_window_capture_source(window_title):
    source = obs.obs_get_source_by_name(source_name)

    settings = obs.obs_data_create()
    obs.obs_data_set_string(settings, "window", window_title)
    obs.obs_source_update(source, settings)

    temp = obs.obs_source_get_settings(source)
    print(obs.obs_data_get_json(temp))

    # cleanup
    obs.obs_data_release(settings)
    obs.obs_source_release(source)

def main():
    active_window = get_active_window()
    change_window_capture_source(active_window)

def script_description():
    return "Python script to update Window Capture based on active window"

obs.timer_add(main, 1000)

I'm looking for some direction, or if I'm on the right track, or if this is possible at all.

Thanks!
 

bradleyp

New Member
After a second look, I'm probably going in the wrong direction with my script. My idea was that the window capture source would update the screen it's "looking at" if you just updated the 'window' value in its settings with the current foreground window details, assuming the string value is formatted correctly "[program.exe]: window title". This might be trickier than I originally thought. Let me know, though, if my idea isn't totally off-base.
 

AaronD

Active Member
You might be able to do that with the Advanced Scene Switcher plugin:
It's become its own programming language, with its own native features specifically tailored to OBS.
 
Top