import obsws_python as obs import time import glob import os import sys import subprocess subprocess.run("obs-studio &",shell=True) time.sleep(5) # pass conn info if not in config.toml cl = obs.ReqClient(host='localhost', port=4455, password='AXB1hYYIeaCUQd9v', timeout=3) SOURCE_NAME = "..auto_game_dector_source" SCENE_NAME = "..auto_game_dector_scene" SOURCE_INPUT_TYPE = "xcomposite_input" GAMES_WM_CLASS_NAME_TO_DETECT = ["steam_app_1304930", "league of legends.exe", "steam_app_582010", "steam_app_739630", "steam_app_438490", "steam_app_321040", "steam_app_820520"] class SceneStatus: OK = 0 MISSING_SCENE = 1 SCENE_NOT_SELECTED = 2 class SourceStatus: OK = 0 MISSING_SOURCE = 1 UNEXPECTED_SOURCES_FOUND = 2 SOURCE_NOT_ENABLED = 3 SOURCE_NOT_CREATED_WITH_DIFFERENT_VALUES = 4 def verify_obs_setup(): scene_status = verify_scene() if scene_status is SceneStatus.MISSING_SCENE: init_scene() source_status = verify_source() if scene_status is SourceStatus.MISSING_SOURCE: init_source() def verify_scene() -> SceneStatus: get_scenes_response = cl.get_scene_list() scenes = get_scenes_response.scenes for scene in scenes: if scene['sceneName'] == SCENE_NAME: if scene['sceneName'] == get_scenes_response.current_program_scene_name: return SceneStatus.OK else: return SceneStatus.SCENE_NOT_SELECTED return SceneStatus.MISSING_SCENE def init_scene(): cl.create_scene(name=SCENE_NAME) cl.set_current_program_scene(name=SCENE_NAME) def verify_source() -> SourceStatus: sources = cl.get_input_list().inputs for source in sources: if source['inputName'] == SOURCE_NAME: return SourceStatus.OK return SourceStatus.MISSING_SOURCE def init_source(): cl.create_input(sceneName=SCENE_NAME,inputName=SOURCE_NAME,inputKind=SOURCE_INPUT_TYPE, inputSettings=None, sceneItemEnabled=True) def game_dector(): windows = cl.get_input_properties_list_property_items(SOURCE_NAME,"capture_window").property_items for window in windows: wmClassName = window['itemValue'].split("\r\n")[2] if wmClassName in GAMES_WM_CLASS_NAME_TO_DETECT: return window['itemValue'] return None verify_obs_setup() recording = cl.get_record_status().output_active def run_bg_app(): global recording global cl game_detected = game_dector() if game_detected is None: if recording: cl.stop_record() directory_path = cl.get_record_directory() list_of_files = glob.glob(directory_path.record_directory + "/*") # * means all if need specific format then *.csv latest_file = max(list_of_files, key=os.path.getctime) print(latest_file) recording = False return latest_file return None if not recording: cl.set_input_settings(name=SOURCE_NAME, settings={'capture_window': game_detected}, overlay=True) cl.start_record() recording = True return None if __name__ == '__main__': while True: run_bg_app() time.sleep(1)