Click These:

Saturday, August 17, 2024

How to Create a Custom On Screen Timer Script for OBS that Resets with Scene Changes

If you're a streamer or video creator, having a custom timer in OBS (Open Broadcaster Software) can be incredibly useful. Whether you're timing events, managing segments, or just keeping track of time, a custom timer script can enhance your streaming experience. In this article, we'll walk you through creating a custom timer script in Python that integrates with OBS, automatically resetting each time the scene changes.

Why Use a Custom Timer?

A custom timer allows you to:

  • Track time spent on different scenes or segments.
  • Display time in a way that fits your stream's aesthetics.
  • Have a timer that resets automatically with scene changes, keeping your stream organized.

Requirements

  • OBS Studio
  • Python installed on your system
  • Tkinter library (usually included with Python)

The Script

Here's a Python script that creates a timer popup window for OBS. The timer resets every time the scene changes and always remains on top of other windows.

import obspython as obs
import time
from threading import Thread
import tkinter as tk

# Global variables
timer_running = False
start_time = None
timer_label = None
root = None

# Function to start the timer
def start_timer():
    global timer_running, start_time
    timer_running = True
    start_time = time.time()
    update_timer()

# Function to stop the timer
def stop_timer():
    global timer_running
    timer_running = False

# Function to reset the timer
def reset_timer():
    global start_time
    start_time = time.time()

# Function to update the timer
def update_timer():
    if timer_running:
        elapsed_time = time.time() - start_time
        hours, rem = divmod(elapsed_time, 3600)
        minutes, seconds = divmod(rem, 60)
        timer_label.config(text="{:02}:{:02}:{:02}".format(int(hours)\
        , int(minutes), int(seconds)))
        root.after(1000, update_timer)

# OBS callback when the scene changes
def on_event(event):
    if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED:
        reset_timer()

# Function to create the timer window
def create_timer_window():
    global root, timer_label
    root = tk.Tk()
    root.title("OBS Timer")

    # Set the timer window to always stay on top
    root.attributes("-topmost", True)

    # Set window size
    window_width = 280
    window_height = 80

    # Set the window size and position
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    x = screen_width - window_width - 10
    y = screen_height - window_height - 85

    root.geometry(f"{window_width}x{window_height}+{x}+{y}")

    # Set the background color to very dark grey
    root.configure(bg="#1a1a1a")

    timer_label = tk.Label(root, text="00:00:00", font=("Pricedown Bl", 48)\
    , fg="purple", bg="#1a1a1a")
    timer_label.pack()

    start_timer()

    root.mainloop()

# OBS script description
def script_description():
    return "Timer popup that resets on scene change, always visible on top \
    of other windows, and positioned in the lower right corner."

# OBS script load
def script_load(settings):
    obs.obs_frontend_add_event_callback(on_event)
    timer_thread = Thread(target=create_timer_window)
    timer_thread.start()

# OBS script unload
def script_unload():
    stop_timer()
    if root:
        root.quit()


 Instructions for Loading the Script into OBS

  1. Save the Script: Copy the provided script and save it as a .py file on your computer.

  2. Open OBS Studio: Launch OBS Studio on your system.

  3. Add the Script:

    • Go to Tools in the top menu.
    • Select Scripts from the dropdown.
    • In the Scripts dialog, click the + button to add a new script.
    • Browse to the location of your saved Python script and select it.
  4. Run the Script: The script will now start running, and you should see a timer window appear in the lower right corner of your screen. This timer will reset every time you switch scenes in OBS.

With this custom timer script, you can keep track of time more effectively during your streams or recordings. The integration with OBS ensures that your timer resets with each scene change, making it a valuable tool for managing live content. Enjoy your enhanced streaming setup!


Feel free to adjust or expand upon this draft as needed!

No comments:

Post a Comment