Skip to content

Instantly share code, notes, and snippets.

@Juraji
Created January 25, 2018 21:17
Show Gist options
  • Save Juraji/88a9d5b5a4bf277e3f9e2be5ee9fa5e8 to your computer and use it in GitHub Desktop.
Save Juraji/88a9d5b5a4bf277e3f9e2be5ee9fa5e8 to your computer and use it in GitHub Desktop.
An OBS script for turning a text source into a 24h digital clock with timezone designation. (Supports dst)
--
-- Created by IntelliJ IDEA.
-- User: Juraji
-- Date: 25-1-2018
-- Time: 19:51
--
-- Binds to a text source and shows the current time in 24h format
-- appended with the current timezone designation.
-- The timezone designations for both dst and non-dst can be changed
-- by updating the dst_ variables below
--
local obs = obslua
local source_name = ""
local is_activated = false
local dst_text = "CEST"
local dst_not_text = "CET"
local previous_text = ""
-- Update the source, identified by source_name, with the current time
function set_clock_text()
local time_text = os.date("%H:%M")
local dst_append = ""
if os.date("*t").isdst then
dst_append = dst_text
else
dst_append = dst_not_text
end
local time_text = string.format("%s %s", time_text, dst_append)
if time_text ~= previous_text then
-- Keep a reference of the current text, and evaluate changes
-- as getting the source and updating it is costly
previous_text = time_text
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", time_text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
end
end
end
-- Check if the activation signal is for the source identified by source_name
-- and activate/deactivate the clock if so
function activate_signal(cd, activating)
local source = obs.calldata_source(cd, "source")
if source ~= nil and activating ~= is_activated then
local name = obs.obs_source_get_name(source)
if name == source_name then
is_activated = activating
if activating then
set_clock_text()
obs.timer_add(set_clock_text, 1000)
else
obs.timer_remove(set_clock_text)
end
end
end
end
-- A source was activated (signal handler)
function source_activated(cd)
activate_signal(cd, true)
end
-- A source was deactivated (signal handler)
function source_deactivated(cd)
activate_signal(cd, false)
end
-- [Called by OBS] Define script properties
function script_properties()
local properties = obs.obs_properties_create()
local p = obs.obs_properties_add_list(properties, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
local sources = obs.obs_enum_sources()
-- List text type sources in the combobox, for ease of use
if sources ~= nil then
for _, source in ipairs(sources) do
local source_id = obs.obs_source_get_id(source)
if 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)
return properties
end
-- [Called by OBS] Describe script
function script_description()
return "A digital clock for CET/CEST locales in 24h format\n"
.. "Locales can be changed by updating the dst_ values at the top of the script\n\n"
.. "Created by Juraji"
end
-- [Called by OBS] Settings were updated
function script_update(settings)
source_name = obs.obs_data_get_string(settings, "source")
end
-- [Called by OBS] Load script
function script_load()
local sig = obs.obs_get_signal_handler()
obs.signal_handler_connect(sig, "source_activate", source_activated)
obs.signal_handler_connect(sig, "source_deactivate", source_deactivated)
print("Digital clock script loaded")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment