OBS Japanブログ

本家OBSとは別の団体ですが,OBS Studioに関する情報を日本語で発信していきます。

【OBS Japan特集】OBS Studio 21.0 以降で実装された Lua を用いてタイムスタンプを表示する

 OBS Japan 独自記事第一弾として,Lua を用いたタイムスタンプを表示するスクリプトを紹介します。

※ OBS Studio 21.0 以降に付属してるスクリプト “countdown.lua” に近いスクリプトになります。

スクリプト解説

 ここからはスクリプトを作成する人のために簡単に流れを解説したいと思います。ただし,OBS Studio のスクリプトを作る場合,OBS API の知識が必要となるため,普通の Lua や Python は書ける方でもその辺りでつまづくかもしれません。

※ 単にスクリプトを利用したい方は「スクリプト全文」まで飛ばしてください

関数 script_description()

function script_description()
    return "Sets a text source to act as a clock when the source is active.\nThe format matches `strftime`.\n\nMade by mntone"
end

 このスクリプトの説明を記述しています。第3者に利用されるときにスクリプトがどういう働きであるかを説明するために使います。個人利用では特に深い意味はありません。

関数 script_properties()

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" 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, "format", "Format", obs.OBS_TEXT_DEFAULT)

    return props
end

 ここでは「スクリプト」ダイアログにおけるこのスクリプトの設定リストを定義します。ここでは [ Text Source ] に「text_gdiplus」と「text_ft2_source」のソースをリストボックスに追加し,[ Format ] というテキストボックスを追加しています。

 このスクリプトを実行することで次のような GUI になります。

f:id:mntone:20180215121229p:plain

関数 set_clock_text()

function set_clock_text()
    local text = os.date(format)

    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

 ここが肝となるタイムスタンプの更新部分です。OBS API が多用されているのでとてもわかりづらいとは思います。

 まず,os.date(format) の部分ですが,これは Lua API にある日時を取得する API です。format 引数はユーザーから入力されたフォーマットにより文字列が生成されます。

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

 この部分ですが,source_name とついた名前のソースを取得し,それが nil (null) でなければタイムスタンプを text にセットする,という流れになっています。

スクリプト全文

 Gist に公開しています。GNU General Public License v2.0下で利用されるなら煮るなり焼くなりしてください。

https://gist.github.com/mntone/5794013f959ec63aa94e6fab66cc2b5f

※ Windows 版 OBS Studio 21.1 より古いバージョンで実行した場合,不正なフォーマットを入力すると,MSVC ランタイムの関数 strftime により OBS Studio がクラッシュすることがあります。その場合,テキストソースをセットせず,まずフォーマットを編集すると問題なく動作します。

動作デモ

 最後に,動作デモを提示しこの記事を締めたいと思います。