Lua Script for automation[recording] not working

arthur_morgan

New Member
I have written a small Lua script to record a few files with a predefined duration (these are mock durations). The script is only recording 7 files and these recordings are not of predefined duration. First and second file records fine then everything is a mess. Please help. Im on Windows 11 and OBS Studio 30.1 Beta 1.

Lua:
obs = obslua

-- Define the durations of each clip in seconds
local clip_durations = {10, 3, 4, 4, 4, 2, 2, 3}
local current_clip = 1

-- Flag to ensure stop_recording doesn't schedule more clips once done
local recording_completed = false

function script_load(settings)
    obs.timer_add(start_initial_delay, 10000) -- 10 seconds initial delay
end

function start_initial_delay()
    obs.timer_remove(start_initial_delay) -- Ensure the timer is removed
    record_clip()
end

function record_clip()
    if current_clip > #clip_durations then
        recording_completed = true -- Prevent further recordings
        print("All clips have been recorded. Stopping the script.")
        return
    end

    -- Start recording
    obs.obs_frontend_recording_start()
    print("Recording clip " .. current_clip .. " for " .. clip_durations[current_clip] .. " seconds.")
    -- Schedule to stop recording after the duration
    obs.timer_add(stop_recording, clip_durations[current_clip] * 1000)
end

function stop_recording()
    if recording_completed then
        -- Prevent execution if recording is completed
        return
    end
    
    -- Stop recording
    obs.obs_frontend_recording_stop()
    print("Clip " .. current_clip .. " recorded.")

    -- Increment to move to the next clip
    current_clip = current_clip + 1

    if current_clip <= #clip_durations then
        -- Wait for 2 seconds before starting the next recording
        obs.timer_add(delay_before_next_clip, 2000)
    else
        recording_completed = true -- Ensure no further recordings are scheduled
        print("Recording of all clips completed.")
    end
end

-- Introduce a separate function to handle delay before the next clip
function delay_before_next_clip()
    obs.timer_remove(delay_before_next_clip) -- Clear the timer to prevent re-trigger
    record_clip() -- Proceed to the next clip
end

function script_unload()
    -- Cleanup if needed when the script is unloaded
end
 
Top