obs = obslua -- Variables for monitoring the streaming time local stream_start_time = nil local stream_running = false local stream_counter = 0 local text_source_name = "" local counter_threshold = 30 local prefix = "" local suffix = "" local update_interval = 60000 -- in milliseconds (60 seconds) -- This function is called when the streaming starts function on_event(event) if event == obs.OBS_FRONTEND_EVENT_STREAMING_STARTING then stream_start_time = os.time() stream_running = true stream_counter = stream_counter + 1 update_text_source(stream_counter) elseif event == obs.OBS_FRONTEND_EVENT_STREAMING_STOPPING then stream_running = false local current_time = os.time() local elapsed_time = current_time - stream_start_time if elapsed_time < (counter_threshold * 60) then stream_counter = stream_counter - 1 update_text_source(stream_counter) end end end -- This function checks the streaming time function check_stream_time() if stream_running and stream_start_time then local current_time = os.time() local elapsed_time = current_time - stream_start_time if elapsed_time >= (counter_threshold * 60) then -- Convert counter_threshold minutes to seconds stream_running = false -- Keep the counter if the threshold is reached end end end -- This function updates the text source in OBS function update_text_source(counter) if text_source_name == "" then print("Warning: No text source name provided.") return end local source = obs.obs_get_source_by_name(text_source_name) if source then local settings = obs.obs_data_create() local formatted_text = prefix .. tostring(counter) .. suffix obs.obs_data_set_string(settings, "text", formatted_text) obs.obs_source_update(source, settings) obs.obs_data_release(settings) obs.obs_source_release(source) else print("Source not found: " .. text_source_name) end end -- Script update function, called regularly function script_tick() check_stream_time() end -- Script initialization function function script_load(settings) obs.obs_frontend_add_event_callback(on_event) obs.timer_add(script_tick, update_interval) -- Checks at regular intervals (60 seconds) end -- Script description function script_description() return "Counts the number of streams that lasted at least a specified number of minutes and updates a text source. If your stream ends below the set duration, then the counter corrects itself downwards." end -- Function to load the script settings function script_defaults(settings) obs.obs_data_set_default_int(settings, "stream_counter", 0) obs.obs_data_set_default_string(settings, "text_source_name", "") obs.obs_data_set_default_int(settings, "counter_threshold", 30) obs.obs_data_set_default_string(settings, "prefix", "") obs.obs_data_set_default_string(settings, "suffix", "") end -- Function to save the script settings function script_save(settings) obs.obs_data_set_int(settings, "stream_counter", stream_counter) obs.obs_data_set_string(settings, "text_source_name", text_source_name) obs.obs_data_set_int(settings, "counter_threshold", counter_threshold) obs.obs_data_set_string(settings, "prefix", prefix) obs.obs_data_set_string(settings, "suffix", suffix) end -- Function to create the user interface function script_properties() local props = obs.obs_properties_create() local current_counter = obs.obs_properties_add_text(props, "current_counter", "Current Counter", obs.OBS_TEXT_DEFAULT) obs.obs_property_set_enabled(current_counter, false) -- Read-only obs.obs_properties_add_button(props, "increase_counter", "Increase Counter", increase_counter) obs.obs_properties_add_button(props, "decrease_counter", "Decrease Counter", decrease_counter) obs.obs_properties_add_text(props, "text_source_name", "Text Source Name", obs.OBS_TEXT_DEFAULT) obs.obs_properties_add_int(props, "counter_threshold", "Counter Threshold (minutes)", 1, 120, 1) obs.obs_properties_add_text(props, "prefix", "Prefix", obs.OBS_TEXT_DEFAULT) obs.obs_properties_add_text(props, "suffix", "Suffix", obs.OBS_TEXT_DEFAULT) return props end -- Function to update the script settings function script_update(settings) stream_counter = obs.obs_data_get_int(settings, "stream_counter") text_source_name = obs.obs_data_get_string(settings, "text_source_name") counter_threshold = obs.obs_data_get_int(settings, "counter_threshold") prefix = obs.obs_data_get_string(settings, "prefix") suffix = obs.obs_data_get_string(settings, "suffix") -- Check if the text source exists if text_source_name ~= "" then local source = obs.obs_get_source_by_name(text_source_name) if not source then print("Warning: The specified text source was not found: " .. text_source_name) else obs.obs_source_release(source) end end -- Update the displayed counter value obs.obs_data_set_string(settings, "current_counter", "Stream # " .. tostring(stream_counter)) -- Update the text source in OBS update_text_source(stream_counter) end -- Function to manually increase the counter function increase_counter(props, p) stream_counter = stream_counter + 1 update_text_source(stream_counter) local settings = obs.obs_data_create() obs.obs_data_set_int(settings, "stream_counter", stream_counter) obs.obs_data_set_string(settings, "current_counter", "Stream # " .. tostring(stream_counter)) obs.obs_properties_apply_settings(script_properties(), settings) obs.obs_data_release(settings) return true end -- Function to manually decrease the counter function decrease_counter(props, p) stream_counter = stream_counter - 1 update_text_source(stream_counter) local settings = obs.obs_data_create() obs.obs_data_set_int(settings, "stream_counter", stream_counter) obs.obs_data_set_string(settings, "current_counter", "Stream # " .. tostring(stream_counter)) obs.obs_properties_apply_settings(script_properties(), settings) obs.obs_data_release(settings) return true end -- Debug function to log messages function print(message) obs.script_log(obs.LOG_INFO, message) end