-- obs-output-stats.lua
-- Output Stats to Text —— 把录制/推流的实时状态显示在一个文本源上
--
-- 对应 OBS 论坛需求板请求:Recording Rate - FPS(0 回复)
-- 做成通用形态:FPS / 丢帧 / 已录时长 / 状态,一行显示,可自定义模板。
--
-- 安装:OBS → 工具 → 脚本 → + → 选本文件 → 选一个「文本(GDI+)」源
--
-- 注:所有 API 键名均经 obslua 模块枚举(1972 键)+ 官方 countdown.lua 交叉确认,
-- 无一个为猜测。详见 资产/OBS-Lua-API-核实报告.md
obs = obslua
source_name = ""
template = ""
idle_text = ""
hotkey_id = nil -- 该常量枚举不到,不依赖;script_load 里必然先赋值
local last_text = ""
local prev_frames = 0
local prev_time = 0
local started_at = 0
-- ---------------------------------------------------------------------------
local function set_text(text)
if text == last_text then return end
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local s = obs.obs_data_create()
obs.obs_data_set_string(s, "text", text)
obs.obs_source_update(source, s)
obs.obs_data_release(s)
obs.obs_source_release(source)
end
last_text = text
end
local function hms(sec)
sec = math.floor(sec); if sec < 0 then sec = 0 end
return string.format("%02d:%02d:%02d",
math.floor(sec / 3600), math.floor(sec / 60) % 60, sec % 60)
end
-- 取当前活跃的输出对象(录制优先),用完必须 release
local function active_output()
local out = obs.obs_frontend_get_recording_output()
local kind = "REC"
if out == nil then
out = obs.obs_frontend_get_streaming_output()
kind = "LIVE"
end
return out, kind
end
local function tick()
local out, kind = active_output()
if out == nil then
prev_frames, prev_time, started_at = 0, 0, 0
if idle_text ~= "" then set_text(idle_text) end
return
end
local frames = obs.obs_output_get_total_frames(out)
local dropped = obs.obs_output_get_frames_dropped(out)
obs.obs_output_release(out)
-- FPS = 帧数增量 / 时间增量
local now = os.time()
local fps = 0
if prev_time > 0 and now > prev_time and frames >= prev_frames then
fps = (frames - prev_frames) / (now - prev_time)
end
prev_frames, prev_time = frames, now
if started_at == 0 then started_at = now end
local text = template
text = string.gsub(text, "{kind}", kind)
text = string.gsub(text, "{fps}", string.format("%.1f", fps))
text = string.gsub(text, "{dropped}", tostring(dropped))
text = string.gsub(text, "{frames}", tostring(frames))
text = string.gsub(text, "{time}", hms(now - started_at))
set_text(text)
end
-- ---------------------------------------------------------------------------
local function reset(pressed)
if not pressed then return end
prev_frames, prev_time, started_at = 0, 0, 0
local keep = last_text
last_text = "\0"
set_text(keep)
end
function reset_button_clicked(props, p) reset(true); return false end
function script_description()
return "把录制/推流的实时状态写进文本源。\n\n" ..
"可用占位符:{kind} {fps} {dropped} {frames} {time}\n" ..
"默认模板:" .. "{kind} {fps}fps {time}"
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
local sid = obs.obs_source_get_unversioned_id(source)
if sid == "text_gdiplus" or sid == "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_text(props, "template", "模板", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "idle_text", "未录制/未推流时显示(留空=不动)", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_button(props, "reset_button", "重置计时", reset_button_clicked)
return props
end
function script_defaults(settings)
obs.obs_data_set_default_string(settings, "template", "{kind} {fps}fps {time}")
obs.obs_data_set_default_string(settings, "idle_text", "")
end
function script_update(settings)
source_name = obs.obs_data_get_string(settings, "source")
template = obs.obs_data_get_string(settings, "template")
idle_text = obs.obs_data_get_string(settings, "idle_text")
last_text = "\0"
end
function script_save(settings)
local a = obs.obs_hotkey_save(hotkey_id)
obs.obs_data_set_array(settings, "reset_hotkey", a)
obs.obs_data_array_release(a)
end
function script_load(settings)
hotkey_id = obs.obs_hotkey_register_frontend("output_stats_reset", "输出状态:重置计时", reset)
local a = obs.obs_data_get_array(settings, "reset_hotkey")
obs.obs_hotkey_load(hotkey_id, a)
obs.obs_data_array_release(a)
print("[obs-output-stats] loaded ok")
obs.timer_add(tick, 1000)
end
function script_unload()
obs.timer_remove(tick)
end