Skip to content

Instantly share code, notes, and snippets.

@absindx
Last active September 17, 2022 14:32
Show Gist options
  • Save absindx/70ae1e56d1ec3839322adcc07501dad3 to your computer and use it in GitHub Desktop.
Save absindx/70ae1e56d1ec3839322adcc07501dad3 to your computer and use it in GitHub Desktop.
OBS Script - Text Timer
--------------------------------------------------
-- OBS Text Timer
--------------------------------------------------
--------------------------------------------------
-- Setting
--------------------------------------------------
local TimerFormat_Full = "%Y/%m/%d %H:%M:%S"
local TimerFormat_Time = "%H:%M:%S"
local TimerSignature_Full = "@TimerFull"
local TimerSignature_Time = "@TimerTime"
local UpdateTick = 100 -- [ms]
--------------------------------------------------
-- Functions
--------------------------------------------------
local scriptEnabled = false
local lastDisplayTime = nil
local textSourceIds = {
"text_gdiplus_v2",
"text_gdiplus",
"text_ft2_source",
}
function getFormattedTimer()
return os.date(TimerFormat_Full), os.date(TimerFormat_Time)
end
function updateSourceText(source, text)
local data = obslua.obs_data_create()
obslua.obs_data_set_string(data, "text", text)
obslua.obs_source_update(source, data)
obslua.obs_data_release(data)
end
function updateTimerTexts()
local timerTextFull, timerTextTime = getFormattedTimer()
if(timerTextFull == lastDisplayTime)then
return
end
lastDisplayTime = timerTextFull
local sources = obslua.obs_enum_sources()
if(sources == nil)then
return
end
for sourcesIndex, source in ipairs(sources) do
local sourceId = obslua.obs_source_get_id(source)
for textSourceIdsIndex,textSource in ipairs(textSourceIds) do
if(sourceId == textSource)then
local sourceName = obslua.obs_source_get_name(source)
if( string.find(sourceName, TimerSignature_Full))then
updateSourceText(source, timerTextFull)
elseif(string.find(sourceName, TimerSignature_Time))then
updateSourceText(source, timerTextTime)
end
end
break
end
end
obslua.source_list_release(sources)
end
function scriptTick()
if(scriptEnabled)then
updateTimerTexts()
else
-- remove tick callback
obslua.timer_remove(scriptTick)
end
end
--------------------------------------------------
-- OBS scripting interface
--------------------------------------------------
function script_load(settings)
-- Parameters:
-- settings ? Settings associated with the script.
-- set tick callback
obslua.timer_add(scriptTick, UpdateTick)
scriptEnabled = true
end
function script_unload()
scriptEnabled = false
end
--function script_save(settings)
-- -- Parameters:
-- -- settings ? Settings associated with the script.
--end
--
--function script_defaults(settings)
-- -- Parameters:
-- -- settings ? Settings associated with the script.
--end
--
--function script_update(settings)
-- -- Parameters:
-- -- settings ? Settings associated with the script.
--end
--
--function script_properties()
-- -- Returns:
-- -- obs_properties_t object created via obs_properties_create().
--end
--
--function script_tick(seconds)
-- -- Parameters:
-- -- seconds ? Seconds passed since previous frame.
--end
function script_description()
return [[Set the current time to a text source(s) containing the following specific strings: "@TimerFull" (yyyy/mm/dd hh:mm:ss) or "@TimerTime" (hh:mm:ss)]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment