ChatGPT helped me code this, but we hit a road block. Please help!

tuna_tuna

New Member
Hi!

I'm fairly inexperienced with coding. However, ChatGPT has opened up a lot of opportunity for me as I'm much more capable of prompting it for code. Although, i've run into a road block with it that I cant seem to work around. In fact, ChatGPT recommend I post to this forum and ask for advice! HA!

Here is basically what i'm trying to do.
Using a .lua script I'd like to monitor a folder for the latest file being generated. It should then extract all characters after "tk_". The file name will have characters prior to "tk_" but they are not necessary for this. It should take those extracted characters and update a text source in OBS. The Scene to update is "Zoom tk counter" and the text source to update is "tk counter". I'd like the script to have a popup or variable to change which allows you to choose the folder path to scan. It should scan this folder every second.

We worked through a few error messages together and then it got to a point where the script keeps crashing OBS. Any ideas?

Here is the code it made.
Code:
obs = obslua

script_description = "Monitor a folder for the latest file being generated and extract characters after 'tk_'. Update a text source in OBS."

source_name = "tk counter"
scene_name = "Zoom tk counter"
timer_interval = 1

function script_properties()
    local props = obs.obs_properties_create()

    obs.obs_properties_add_path(props, "folder_path", "Folder Path", obs.OBS_PATH_DIRECTORY, "", "")
    obs.obs_properties_add_int(props, "timer_interval", "Scan Interval (seconds)", 1, 60, 1)

    return props
end

function script_update(settings)
    folder_path = obs.obs_data_get_string(settings, "folder_path")
    timer_interval = obs.obs_data_get_int(settings, "timer_interval")
end

function extract_tk_value(filename)
    local tk_start = string.find(filename, "tk_")
    if tk_start then
        return string.sub(filename, tk_start + 3)
    else
        return nil
    end
end

function update_text_source(tk_value)
    local scenesource = obs.obs_get_source_by_name(scene_name)
    if scenesource then
        local scene = obs.obs_scene_from_source(scenesource)
        local sceneitem = obs.obs_scene_find_source(scene, source_name)

        if sceneitem then
            local text_source = obs.obs_sceneitem_get_source(sceneitem)
            local settings = obs.obs_data_create()

            obs.obs_data_set_string(settings, "text", tk_value)
            obs.obs_source_update(text_source, settings)

            obs.obs_data_release(settings)
            obs.obs_source_release(text_source)
        end

        obs.obs_scene_release(scene)
        obs.obs_source_release(scenesource)
    end
end

function monitor_folder()
    local latest_time = 0
    local latest_file = nil

    local command = 'ls "' .. folder_path .. '"'
    local handle = io.popen(command)
    if not handle then return end
    
    for file in handle:lines() do
        local file_path = folder_path .. "/" .. file
        local handle_stat = io.popen('stat -f "%m" "' .. file_path .. '"')
        if not handle_stat then break end
        
        local attr = tonumber(handle_stat:read("*a"))
        handle_stat:close()

        if attr and attr > latest_time then
            local tk_value = extract_tk_value(file)
            if tk_value then
                latest_time = attr
                latest_file = file
            end
        end
    end
    handle:close()

    if latest_file then
        local tk_value = extract_tk_value(latest_file)
        update_text_source(tk_value)
    end
end

function script_load(settings)
    timer_id = obs.timer_add(monitor_folder, timer_interval * 1000)
end

function script_unload()
    if timer_id then
        obs.timer_remove(timer_id)
    end
end
 
Top