OBS crashes when executing Python script

Carrara

New Member
Hello,

I need to set up a script to record at certain times. I setup the below script. However the latest version of OBS does not like it. It crashes with nothing pertinent in the log to explain why. Actually it just hangs indefinitely, once I click something in the OBS window it then asks me to shutdown the process.
Python:
import os
import time
import subprocess
import schedule

# Set the start and end times for the first recording session
session1_start_time = "08:28"
session1_end_time = "17:00"

# Set the start and end times for the second recording session
session2_start_time = "17:30"
session2_end_time = "03:30"

# Set the duration of the recording in seconds
duration = 36000

# Set the path to your OBS executable
obs_path = 'C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe'

# Set the path to your OBS recording profile
profile_path = 'C:\\Users\\admcarrara\\AppData\\Roaming\\obs-studio\\basic\\profiles\\Untitled'

# Set the path to the folder where you want to save your recordings
output_folder = 'C:\\Users\\admcarrara\\Videos'

# Function to start the recording
def start_recording():
    # Get the current time
    now = time.strftime("%H:%M", time.localtime())

    # Start the first session of recording if the current time matches the session 1 start time
    if now == session1_start_time:
        # Set the start time for the recording in seconds since epoch
        start_time_sec = time.time()

        # Start the recording
        subprocess.call([obs_path, '--startrecording', '--minimize-to-tray', '--profile', profile_path, '--output', os.path.join(output_folder, 'Session1_Recording_{0}.mp4'.format(time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(start_time_sec))))])

        # Wait for the duration of the recording
        time.sleep(duration)

        # Stop the recording
        subprocess.call([obs_path, '--stoprecording'])

    # Start the second session of recording if the current time matches the session 2 start time
    elif now == session2_start_time:
        # Set the start time for the recording in seconds since epoch
        start_time_sec = time.time()

        # Start the recording
        subprocess.call([obs_path, '--startrecording', '--minimize-to-tray', '--profile', profile_path, '--output', os.path.join(output_folder, 'Session2_Recording_{0}.mp4'.format(time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(start_time_sec))))])

        # Wait for the duration of the recording
        time.sleep(duration)

        # Stop the recording
        subprocess.call([obs_path, '--stoprecording'])

# Schedule the recording sessions to run every day
schedule.every().day.at(session1_start_time).do(start_recording)
schedule.every().day.at(session2_start_time).do(start_recording)

# Run the scheduled tasks
while True:
    schedule.run_pending()
    time.sleep(1)
 

Attachments

  • 2023-05-04 21-36-08.txt
    5.3 KB · Views: 5
  • 2023-05-04 21-40-23.txt
    5.3 KB · Views: 3
  • 2023-05-04 21-43-05.txt
    5.3 KB · Views: 2
  • 2023-05-04 21-47-34.txt
    5.3 KB · Views: 2
  • 2023-05-04 21-52-05.txt
    10.1 KB · Views: 2

R1CH

Forum Admin
Developer
It looks like this script should be run outside of OBS as it does not use the OBS API in any way.
 

AaronD

Active Member
Different approach: use macros in the Advanced Scene Switcher plugin.

In your case, you'd have either:
  • 4 macros, one for each time, 2 to start and 2 to stop
or
  • 2 macros, 1 to start and 1 to stop, each for Time1 OR Time2

Here's how the 2nd option would look:
1683305124347.png

Of course, you'd also need an End macro, with the same structure. Or you could have 2 of each (4 total) without the OR condition.
 
Top