Last active
January 8, 2021 20:35
-
-
Save SCG82/bc4a6fbbb68e1b568050d270c49f8c8d to your computer and use it in GitHub Desktop.
Lua script for OBS to display current time and a frame counter in a text source
This file contains 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
script_ver = 2.5 | |
obs = obslua | |
source_name = "" | |
last_text = "" | |
current_frame = 0 | |
time_format_string = "" | |
output_format_string = "" | |
output_fomart_string_vars = { | |
time = { | |
desc = "Output from the <strong>Time Format String</strong>", | |
func = function() return os.date(time_format_string) end | |
}, | |
cfn = { | |
desc = "Current frame number", | |
func = function() return string.format("%02d", current_frame) end | |
} | |
} | |
ffi = require("ffi") | |
ffi.cdef[[ | |
struct video_output; | |
typedef struct video_output video_t; | |
double video_output_get_frame_rate(const video_t *video); | |
video_t *obs_get_video(void); | |
]] | |
local obsffi | |
if ffi.os == "OSX" then | |
obsffi = ffi.load("obs.0.dylib") | |
else | |
obsffi = ffi.load("obs") | |
end | |
-- Function to set the time text | |
function set_text() | |
local text = string.gsub(output_format_string, "%%(%w+)", function(var) | |
if (output_fomart_string_vars[var]) then | |
return output_fomart_string_vars[var].func() | |
else | |
return "%"..var | |
end | |
end) | |
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 to set the frame number | |
function set_frame() | |
current_frame = current_frame + 1 | |
if current_frame > video_output_get_frame_rate() then | |
current_frame = 1 | |
end | |
end | |
function obs_get_video() | |
return obsffi.obs_get_video() | |
end | |
function video_output_get_frame_rate() | |
return obsffi.video_output_get_frame_rate(obs_get_video()); | |
end | |
function reset(pressed) | |
if not pressed then | |
return | |
end | |
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) | |
end | |
end | |
---------------------------------------------------------- | |
-- Called every frame (by OBS) | |
function script_tick(sec) | |
set_frame() | |
set_text() | |
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" or source_id == "text_ft2_source" or source_id == "text_ft2_source_v2" 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, "time_format_string", "Time Format String", obs.OBS_TEXT_DEFAULT) | |
obs.obs_properties_add_text(props, "output_format_string", "Output Format String", obs.OBS_TEXT_DEFAULT) | |
return props | |
end | |
function script_description() | |
local desc = [[ | |
<style> | |
#output_vars { | |
margin: 10px 0 10px 10px; | |
} | |
#output_vars td { | |
padding: 5px; | |
border: 1px solid #888; | |
} | |
#author, #script_var { | |
margin-top: 5px; | |
} | |
</style> | |
<h3>Sets a text source to act as a date/time and frame count.</h3> | |
Available <strong>Output Format String</strong> Variables | |
<table id="output_vars"> | |
<tbody> | |
]] | |
for name,value in pairs(output_fomart_string_vars) do | |
desc = desc .. [[ | |
<tr> | |
<td width="55"><code>%]] .. name .. [[</code></td> | |
<td>]] .. value.desc .. [[</td> | |
</tr> | |
]] | |
end | |
desc = desc .. [[ | |
</tbody> | |
</table> | |
<div id="author">Made by Ragowit and modified by us -- Use at your own risk!</div> | |
<div id="script_var"><small>Version: ]] .. script_ver .. [[</small></div>]] | |
return desc | |
end | |
-- A function named script_update will be called when settings are changed | |
function script_update(settings) | |
source_name = obs.obs_data_get_string(settings, "source") | |
time_format_string = obs.obs_data_get_string(settings, "time_format_string") | |
output_format_string = obs.obs_data_get_string(settings, "output_format_string") | |
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, "time_format_string", "%H:%M:%S") | |
obs.obs_data_set_default_string(settings, "output_format_string", "%time [%cfn]") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment