前言

老头环DLC上了,被拉塔恩虐的死去活来,就有了添加死亡次数记录的想法。

OBS基础功能

OBS的text(GDI+)输入源可以添加txt文本,每当txt文本变动时,显示的文字也会同步更新。
image.png

思路

写一个脚本,监听键盘上的+-按键,按下后更新TXT文本。

实现

一开始单独写了个Python,在每次直播前都得手动运行,非常的not elegant,后面发现OBS自己就可以加载Python脚本(工具->脚本)。
image.png

上代码:

import obspython as obs
import os

# 文件路径
counter_file = os.path.join(os.path.expanduser("~"), "obs_counter.txt")

# 计数器变量
counter = 0
hotkey_id_inc = None
hotkey_id_dec = None

# 更新计数器显示的函数
def update_counter_text():
    source = obs.obs_get_source_by_name("Counter")
    if source is not None:
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text", str(counter))
        obs.obs_source_update(source, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source)
        print(f"Counter updated: {counter}")

# 加载计数器值
def load_counter():
    global counter
    try:
        with open(counter_file, "r") as f:
            counter = int(f.read())
            print(f"Counter loaded from file: {counter}")
    except FileNotFoundError:
        counter = 0
        print("Counter file not found, starting from 0")

# 保存计数器值
def save_counter():
    with open(counter_file, "w") as f:
        f.write(str(counter))
    print(f"Counter saved to file: {counter}")

# 增加计数器的函数
def increment_counter(pressed):
    global counter
    if pressed:
        counter += 1
        update_counter_text()
        save_counter()
        print(f"Counter incremented: {counter}")

# 减少计数器的函数
def decrement_counter(pressed):
    global counter
    if pressed:
        counter -= 1
        update_counter_text()
        save_counter()
        print(f"Counter decremented: {counter}")

# 初始化脚本
def script_load(settings):
    global hotkey_id_inc, hotkey_id_dec
    load_counter()
    update_counter_text()
    
    # 注册增加计数的热键
    hotkey_id_inc = obs.obs_hotkey_register_frontend("increment_counter", "Increment Counter", increment_counter)
    hotkey_save_array_inc = obs.obs_data_get_array(settings, "increment_counter.hotkey")
    obs.obs_hotkey_load(hotkey_id_inc, hotkey_save_array_inc)
    obs.obs_data_array_release(hotkey_save_array_inc)
    
    # 注册减少计数的热键
    hotkey_id_dec = obs.obs_hotkey_register_frontend("decrement_counter", "Decrement Counter", decrement_counter)
    hotkey_save_array_dec = obs.obs_data_get_array(settings, "decrement_counter.hotkey")
    obs.obs_hotkey_load(hotkey_id_dec, hotkey_save_array_dec)
    obs.obs_data_array_release(hotkey_save_array_dec)
    
    print("Script loaded")

# 保存脚本设置
def script_save(settings):
    global hotkey_id_inc, hotkey_id_dec
    hotkey_save_array_inc = obs.obs_hotkey_save(hotkey_id_inc)
    obs.obs_data_set_array(settings, "increment_counter.hotkey", hotkey_save_array_inc)
    obs.obs_data_array_release(hotkey_save_array_inc)
    
    hotkey_save_array_dec = obs.obs_hotkey_save(hotkey_id_dec)
    obs.obs_data_set_array(settings, "decrement_counter.hotkey", hotkey_save_array_dec)
    obs.obs_data_array_release(hotkey_save_array_dec)
    
    print("Script settings saved")

# 设置脚本描述
def script_description():
    return """
    This script increments or decrements a counter with hotkeys and saves the count.
    
    The count_file location is "C:\\Users\\username\\obs_counter.txt"
    """

# 设置脚本默认参数
def script_defaults(settings):
    obs.obs_data_set_default_int(settings, "increment_counter", 0)
    obs.obs_data_set_default_int(settings, "decrement_counter", 0)

写完代码,把脚本添加到OBS内。

使用

初次使用OBS脚本,需要设置Python安装目录
image.png

  1. 设置热键,文件->设置->快捷键 设置increment_counter和decrement_counter的快捷键image.png
  2. 添加text(GDI+)来源是C:/User/{USERNAME}/obs_counter.txt ,其中USERNAME是电脑用户名,没有obs_counter.txt就手动新建一个,里面写个初始值。
  3. 正常情况下,现在键盘按下+键数字就会增加,-键数字减少。

成果展示

image.png

Q.E.D.


寄蜉蝣于天地,渺沧海之一粟