0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OBS起動後に自動でテキストソースのテキストを更新する

Last updated at Posted at 2024-09-01

動機

方法1

OBSのフロントエンドの読み込み終了イベントに、テキストソースを更新するコールバックを登録します。

sol1.lua
local TARGET_TEXT_SOURCE_NAME = "TestText"

function script_load(settings)
	obslua.obs_frontend_add_event_callback(on_event)
end

function on_event(event)
    -- https://github.com/obsproject/obs-studio/blob/release/30.2/UI/window-basic-main.cpp#L2526-L2529
    if event == obslua.OBS_FRONTEND_EVENT_FINISHED_LOADING then
        update_text()

        obslua.obs_frontend_remove_event_callback(on_event)
    end
end

function update_text()
    local source = obslua.obs_get_source_by_name(TARGET_TEXT_SOURCE_NAME)
    if source ~= nil then
        local settings = obslua.obs_data_create()
        obslua.obs_data_set_string(settings, "text", get_text())
        obslua.obs_source_update(source, settings)
        obslua.obs_data_release(settings)
        obslua.obs_source_release(source)
    end
end

-- 解説用のサンプル処理
function get_text()
    local text = ""
    -- = を含む行を逆順にtextに追加していく
    for line in io.popen([[type "C:\mylog.log" | sort /r]]):lines() do
        if line:find("=") then
            text = text .. line .. "\n"
        end
    end
    return text
end

OBS_FRONTEND_EVENT_FINISHED_LOADING イベントを使用しない場合、 script_load 内でテキストソースを更新することはできません。名前からも分かるように、OBS_FRONTEND_EVENT_FINISHED_LOADING イベントが発行される前はソースが読み込まれていないため、編集できません。

方法2

スケジューラやBatファイルなどを用いて、OBSの起動前にテキスト整形スクリプトを実行し、OBS側でテキストソースの「ファイルから読み取り(Read from file)」機能を使用します。

image.png

上記の画像では C:\mylog.log を選択していますが、このファイルを更新すると、OBSは起動中であっても自動的に再読み込みしてくれます。Luaに慣れていない場合は、この方法をおすすめします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?