Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active October 25, 2023 01:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mntone/5794013f959ec63aa94e6fab66cc2b5f to your computer and use it in GitHub Desktop.
Save mntone/5794013f959ec63aa94e6fab66cc2b5f to your computer and use it in GitHub Desktop.
Sets a text source to act as a clock when the source is active. The format matches `strftime`. Use only in OBS Studio 21.0 and later!
obs = obslua
source_name = ""
tick = 1000
last_text = ""
format = ""
activated = false
local s_unit = 1000 * 1000 * 1000 -- [ns]
-- Function to set the clock text
function set_clock_text()
local text = os.date(format)
if text ~= last_text then
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
end
end
last_text = text
end
function timer_callback()
set_clock_text()
end
function activate(activating)
if activated == activating then
return
end
activated = activating
if activating then
set_clock_text()
local offset = obs.os_gettime_ns() % s_unit
obs.os_sleepto_ns(offset)
obs.timer_add(timer_callback, tick)
else
obs.timer_remove(timer_callback)
end
end
-- Called when a source is activated/deactivated
function activate_signal(cd, activating)
local source = obs.calldata_source(cd, "source")
if source ~= nil then
local name = obs.obs_source_get_name(source)
if (name == source_name) then
activate(activating)
end
end
end
function source_activated(cd)
activate_signal(cd, true)
end
function source_deactivated(cd)
activate_signal(cd, false)
end
function reset(pressed)
if not pressed then
return
end
activate(false)
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local active = obs.obs_source_active(source)
obs.obs_source_release(source)
activate(active)
end
end
----------------------------------------------------------
-- A function named script_properties defines the properties that the user
-- can change for the entire script module itself
function script_properties()
local props = obs.obs_properties_create()
local p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, source in ipairs(sources) do
source_id = obs.obs_source_get_id(source)
if source_id == "text_gdiplus_v2" or source_id == "text_gdiplus" or source_id == "text_ft2_source" then
local name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
end
end
end
obs.source_list_release(sources)
obs.obs_properties_add_text(props, "format", "Format", obs.OBS_TEXT_DEFAULT)
return props
end
-- A function named script_description returns the description shown to
-- the user
function script_description()
return "Sets a text source to act as a clock when the source is active.\nThe format matches `strftime`.\n\nMade by mntone"
end
-- A function named script_update will be called when settings are changed
function script_update(settings)
activate(false)
source_name = obs.obs_data_get_string(settings, "source")
format = string.gsub(obs.obs_data_get_string(settings, "format"), "\\n", "\n")
reset(true)
end
-- A function named script_defaults will be called to set the default settings
function script_defaults(settings)
obs.obs_data_set_default_string(settings, "format", "%Y/%m/%d %H:%M:%S")
end
-- A function named script_save will be called when the script is saved
function script_save(settings)
end
-- a function named script_load will be called on startup
function script_load(settings)
-- Connect activation/deactivation signal callbacks
local sh = obs.obs_get_signal_handler()
obs.signal_handler_connect(sh, "source_activate", source_activated)
obs.signal_handler_connect(sh, "source_deactivate", source_deactivated)
end
@mntone
Copy link
Author

mntone commented Apr 11, 2020

Thanks, Yanorei-san. 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment