I asked ChatGPT to create a script for me. It was fun, but unsuccessful

frogstar42

New Member
I wanted a simple script that found a folder on my desktop filled with videos. I wanted it to play each video one after another, and then check to see if new files had been added before it looped and started all over again. We played back and forth for an hour but it could not get past the errors.


[videoplaylist.lua] Error running file: ...ata/obs-plugins/frontend-tools/scripts/videoplaylist.lua:27: Error in obs_scene_find_source (arg 1), expected 'struct obs_scene *|obs_scene_t *' got 'struct obs_source *|obs_source_t *'

I was so optimistic and it was polite, patient and helpful to offer new versions but each failed and told me... go here and find one that exists already. (grin)

-- Load the OBS module
obs = require('obslua')

-- Set up the path to the folder
local folder_path = os.getenv("USERPROFILE") .. "\\Desktop\\obs"

-- Get a list of all the video files in the folder
local function get_file_list()
local file_list = {}
for _, file_name in ipairs(os.ls(folder_path)) do
local file_path = folder_path .. "\\" .. file_name
if string.sub(file_name, -4) == ".mp4" then
table.insert(file_list, file_path)
end
end
return file_list
end

-- Set up the media source to play the videos in the playlist
local media_source = obs.obs_source_create("ffmpeg_source", "Video Playlist", nil, nil)

-- Set the media source to loop
obs.obs_source_set_looping(media_source, true)

-- Add the media source to the scene
local scene = obs.obs_frontend_get_current_scene()
local scene_item = obs.obs_scene_find_source(scene, "Video Playlist")
if scene_item == nil then
obs.obs_scene_add(scene, media_source)
else
obs.obs_sceneitem_set_source(scene_item, media_source)
end

-- Loop through the playlist
local file_list = {}
while true do
if #file_list == 0 then
file_list = get_file_list()
end

if #file_list > 0 then
obs.obs_source_media_stop(media_source)

obs.obs_data_set_string(obs.obs_data_create(), "local_file", file_list[1])
obs.obs_source_update(media_source, obs.obs_data_create())
obs.obs_source_media_play(media_source)

for i = 2, #file_list do
obs.obs_data_set_string(obs.obs_data_create(), "local_file", file_list)
obs.obs_source_media_queue(media_source, obs.obs_data_create())
end

file_list = {}
end

obs.timer_sleep(1000)
end
 

DCStrato

Member
Frogstar42,

You have a long way to go. This probably is NOT a simple script. The error shown is because get_current_scene returns a source_t object not a scene_t object. Use obs_scene_from_source to get the actual scene object to use with find_source. Then remember you have to release the scene source returned from get_current_scene with obs_source_release(). Same with using data. You can't throw away a returned reference by just using obs_data_create(). You have to save the returned data reference and release that data after you are finished by using obs_data_release(). You also need to decide how you are going to link your script into OBS. Scripts have specific functions that get called by OBS so they actually run. For example, function script_load () gets called when your script is loaded, so if you don't have any User Interface property needs you could start here. Otherwise your script won't actually get a chance to do anything. I think you will also need some form of timed callback setup so you can continue to monitor your video folder and keep your playlist updated.

DC
 
Top