Created
April 24, 2022 06:48
-
-
Save frabarz/e704ba3b9a2e79efa7cfd1c5282ec7c8 to your computer and use it in GitHub Desktop.
Quick OBS script in lua to write the local date/time to a text source.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obs = obslua | |
module_description = "Sets a text source to act as a digital clock." | |
source_name = "" | |
date_format = "%c" | |
last_text = "" | |
last_timer = 30 | |
function set_time_text() | |
local text = os.date(date_format) | |
if text ~= last_text then | |
local source = obs.obs_get_source_by_name(source_name) | |
if source ~= nil then | |
if obs.obs_source_active(source) 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) | |
end | |
obs.obs_source_release(source) | |
end | |
end | |
last_text = text | |
end | |
function script_load(settings) | |
last_timer = obs.obs_data_get_int(settings, "timer") | |
set_time_text() | |
obs.timer_add(set_time_text, last_timer * 1000) | |
end | |
function script_update(settings) | |
source_name = obs.obs_data_get_string(settings, "source") | |
date_format = obs.obs_data_get_string(settings, "format") | |
local timer = obs.obs_data_get_int(settings, "timer") | |
if timer ~= last_timer then | |
obs.timer_remove(set_time_text) | |
obs.timer_add(set_time_text, timer * 1000) | |
last_timer = timer | |
end | |
end | |
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_unversioned_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) | |
obs.obs_properties_add_int_slider(props, "timer", "Update frequency", 1, 60, 1) | |
local p = obs.obs_properties_add_text(props, "format", "Date format", obs.OBS_TEXT_DEFAULT) | |
obs.obs_property_set_long_description(p, "Lua date format tags:\ | |
%a abbreviated weekday name (e.g., Wed) \ | |
%A full weekday name (e.g., Wednesday) \ | |
%b abbreviated month name (e.g., Sep) \ | |
%B full month name (e.g., September) \ | |
%c date and time (e.g., 09/16/98 23:48:10) \ | |
%d day of the month [01-31] \ | |
%H hour, using a 24-hour clock [00-23] \ | |
%I hour, using a 12-hour clock [01-12] \ | |
%M minute [00-59] \ | |
%m month [01-12] \ | |
%p either `am` or `pm` \ | |
%S second [00-61] \ | |
%w weekday number [0-6 = Sunday-Saturday] \ | |
%x date (e.g., 09/16/98) \ | |
%X time (e.g., 23:48:10) \ | |
%Y full year \ | |
%y two-digit year [00-99] \ | |
%% the character `%´") | |
return props | |
end | |
function script_defaults(settings) | |
obs.obs_data_set_default_string(settings, "format", "%c") | |
obs.obs_data_set_default_int(settings, "timer", 30) | |
end | |
function script_description() | |
return module_description | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment