obs = obslua -- User settings settings = { text_source_name = "", prefix = "", suffix = "", seconds_option = "show", test_mode = false } start_time = nil update_interval = 1 -- Update every second timer_active = false test_mode_active = false -- Script description function script_description() return "K_STYER's Dynamic Uptimer\n\nZeigt die Stream-Laufzeit (Uptime) in einem Text-Quellobjekt an.\n\nEinstellungen:\n- Präfix und Suffix\n- Sekundenanzeige Optionen\n- Testmodus\n\nAnwendung:\nLade das Skript in OBS, konfiguriere die Textquelle und wähle die gewünschten Optionen aus." end -- Script properties function script_properties() local props = obs.obs_properties_create() obs.obs_properties_add_text(props, "text_source_name", "Textquelle", obs.OBS_TEXT_DEFAULT) obs.obs_properties_add_text(props, "prefix", "Präfix", obs.OBS_TEXT_DEFAULT) obs.obs_properties_add_text(props, "suffix", "Suffix", obs.OBS_TEXT_DEFAULT) local seconds_options = obs.obs_properties_add_list(props, "seconds_option", "Sekundenanzeige", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING) obs.obs_property_list_add_string(seconds_options, "Anzeigen", "show") obs.obs_property_list_add_string(seconds_options, "Ausblenden", "hide") obs.obs_property_list_add_string(seconds_options, "Nach der ersten Minute ausblenden", "hide_after_first_minute") obs.obs_properties_add_bool(props, "test_mode", "Testmodus") return props end -- Script update function script_update(new_settings) settings.text_source_name = obs.obs_data_get_string(new_settings, "text_source_name") settings.prefix = obs.obs_data_get_string(new_settings, "prefix") settings.suffix = obs.obs_data_get_string(new_settings, "suffix") settings.seconds_option = obs.obs_data_get_string(new_settings, "seconds_option") settings.test_mode = obs.obs_data_get_bool(new_settings, "test_mode") -- Restart the timer with updated settings if timer_active then obs.timer_remove(update_text_source) obs.timer_add(update_text_source, update_interval * 1000) end -- Handle test mode activation and deactivation if settings.test_mode and not test_mode_active then start_time = os.time() test_mode_active = true if not timer_active then obs.timer_add(update_text_source, update_interval * 1000) timer_active = true end elseif not settings.test_mode and test_mode_active then start_time = nil test_mode_active = false if timer_active then obs.timer_remove(update_text_source) timer_active = false end end -- Update the text immediately after settings change update_text_source() end -- Script defaults function script_defaults(settings) obs.obs_data_set_default_string(settings, "prefix", "") obs.obs_data_set_default_string(settings, "suffix", "") obs.obs_data_set_default_string(settings, "seconds_option", "show") obs.obs_data_set_default_bool(settings, "test_mode", false) end -- Update the text source with the uptime function update_text_source() if not settings.text_source_name or settings.text_source_name == "" then return end local source = obs.obs_get_source_by_name(settings.text_source_name) if source then local uptime = get_uptime() local text = settings.prefix .. uptime .. settings.suffix local settings_data = obs.obs_data_create() obs.obs_data_set_string(settings_data, "text", text) obs.obs_source_update(source, settings_data) obs.obs_data_release(settings_data) obs.obs_source_release(source) end end -- Get the formatted uptime function get_uptime() if not start_time then return settings.seconds_option == "hide" and "00:00" or "00:00:00" end local now = os.time() local elapsed = now - start_time if settings.test_mode then elapsed = os.difftime(now, start_time) end local hours = math.floor(elapsed / 3600) local minutes = math.floor((elapsed % 3600) / 60) local seconds = elapsed % 60 if settings.seconds_option == "hide" or (settings.seconds_option == "hide_after_first_minute" and elapsed >= 60) then return string.format("%02d:%02d", hours, minutes) else return string.format("%02d:%02d:%02d", hours, minutes, seconds) end end -- Start the timer when streaming starts function on_event(event) if event == obs.OBS_FRONTEND_EVENT_STREAMING_STARTED then start_time = os.time() if not timer_active then obs.timer_add(update_text_source, update_interval * 1000) timer_active = true end elseif event == obs.OBS_FRONTEND_EVENT_STREAMING_STOPPED then start_time = nil if timer_active then obs.timer_remove(update_text_source) timer_active = false end update_text_source() -- Reset the display to 00:00 or 00:00:00 end end -- Script load function script_load(settings) obs.obs_frontend_add_event_callback(on_event) end -- Script unload function script_unload() if timer_active then obs.timer_remove(update_text_source) end end