obs = obslua
source_name =""last_text =""
format_string =""
activated =false-- 此函数用于获取精确的毫秒级时间戳,适用于 Linux 系统localfunctiongetMillisTimestamp()local sec, usec = os.time(), os.clock()local millis = math.floor((sec + usec)*1000)return millis
end-- Function to set the time textfunctionset_time_text()-- local text = os.date(format_string)-- local date_table = os.date("*t")-- local ms = string.format("%.3f", os.clock()) -- local hour, minute, second = date_table.hour, date_table.min, date_table.sec-- local year, month, day = date_table.year, date_table.month, date_table.day -- local text = string.format("%s", ms)local date_table = os.date("*t")local hour, minute, second = date_table.hour, date_table.min, date_table.seclocal year, month, day = date_table.year, date_table.month, date_table.day-- 获取毫秒级时间戳local millisTimestamp =getMillisTimestamp()-- 提取毫秒部分local ms = millisTimestamp %1000-- 格式化时间字符串local text = string.format("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, ms)if text ~= last_text thenlocal source = obs.obs_get_source_by_name(source_name)if source ~=nilthenlocal 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)endendlast_text = text
endfunctiontimer_callback()set_time_text()endfunctionactivate(activating)if activated == activating thenreturnendactivated = activatingif activating thenset_time_text()obs.timer_add(timer_callback,1)elseobs.timer_remove(timer_callback)endend-- Called when a source is activated/deactivatedfunctionactivate_signal(cd, activating)local source = obs.calldata_source(cd,"source")if source ~=nilthenlocal name = obs.obs_source_get_name(source)if(name == source_name)thenactivate(activating)endendendfunctionsource_activated(cd)activate_signal(cd,true)endfunctionsource_deactivated(cd)activate_signal(cd,false)endfunctionreset(pressed)ifnot pressed thenreturnendactivate(false)local source = obs.obs_get_source_by_name(source_name)if source ~=nilthenlocal active = obs.obs_source_active(source)obs.obs_source_release(source)activate(active)endend------------------------------------------------------------ A function named script_properties defines the properties that the user-- can change for the entire script module itselffunctionscript_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 ~=nilthenfor _, source inipairs(sources)dosource_id = obs.obs_source_get_id(source)if source_id =="text_gdiplus"or source_id =="text_ft2_source"thenlocal name = obs.obs_source_get_name(source)obs.obs_property_list_add_string(p, name, name)endendendobs.source_list_release(sources)obs.obs_properties_add_text(props,"format_string","Format String", obs.OBS_TEXT_DEFAULT)return props
end-- A function named script_description returns the description shown to-- the userfunctionscript_description()return"Sets a text source to act as a date/time text when the source is active.\n\nMade by Ragowit"end-- A function named script_update will be called when settings are changedfunctionscript_update(settings)activate(false)source_name = obs.obs_data_get_string(settings,"source")format_string = obs.obs_data_get_string(settings,"format_string")reset(true)end-- A function named script_defaults will be called to set the default settingsfunctionscript_defaults(settings)obs.obs_data_set_default_string(settings,"format_string","%Y-%m-%d %X")end-- a function named script_load will be called on startupfunctionscript_load(settings)-- Connect activation/deactivation signal callbacks---- NOTE: These particular script callbacks do not necessarily have to-- be disconnected, as callbacks will automatically destroy themselves-- if the script is unloaded. So there's no real need to manually-- disconnect callbacks that are intended to last until the script is-- unloaded.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