Resource icon

OBS Lua WebcamReload: Fix webcam or other USB video device freezes

-- WebcamReload v1.1
-- A simple Lua script to reload your OBS Studio video source (webcam, other UVC sources...) every x minutes.
-- From FlyingFathead (https://github.com/FlyingFathead)

-- For years, OBS has had a problem where UVC video sources (such as USB webcams) are freezing up intermittently.
-- This is "somewhat-of-a-workaround" script to reload a USB video source such as a UVC webcam to prevent freezes.

-- I've ran this in Ubuntu 22.04 & OBS Studio 29.0.2, and it's been working well with a dual webcam setup.
-- Note that you need one script per video source that needs to be reloaded.

-- In addition to occasional webcam freezes, the script might help in i.e. resetting your camera exposure/aperture.
-- This is practical in scenarios where your webcam source isn't re-adjusting to changes in lighting conditions.
-- Or, where these conditions change over time (i.e. outdoors).

-- Install: open up a plaintext editor, copy-paste this code in and save it as (i.e.): ReloadWebcam.lua

-- default directory for OBS scripts in typical Ubuntu Linux use scenarios is:
-- /usr/share/obs/obs-plugins/frontend-tools/scripts/

-- [start of lua code]

obs = obslua

-- Replace 'UVC Webcam' with the name of your video capture source
local source_name = 'UVC Webcam'

-- The value below (default: 1) is the reload interval in minutes.
-- Change as needed.
local reload_interval_minutes = 1

local function reload_source()
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
obs.obs_source_inc_showing(source)
obs.timer_add(function() obs.obs_source_dec_showing(source) end, 500)

-- the value above that says "500" is a 500ms reconnection delay
-- this is because the reload cycle can sometimes fail if there is no delay

-- NOTE: you need to try out and see what works best for you;
-- in some cases you might even be ok with no delay at all.
-- note that this might mean visible dropouts, especially with higher values.

obs.obs_source_release(source)
end
end

local function timer_callback()
reload_source()
obs.remove_current_callback()
obs.timer_add(timer_callback, reload_interval_minutes * 60 * 1000)
end

function script_load(settings)
obs.timer_add(timer_callback, reload_interval_minutes * 60 * 1000)
end

function script_unload()
-- stop timers when script is unloaded
obs.timer_remove(timer_callback)
end


-- [end of lua code]

-- How to run it:
-- Open OBS Studio and create or select the desired scene.
-- Make sure you have set the name of your video/webcam source the same as the `local source_name` variable in the script.
-- Add in the script:

-- In the OBS Studio menu, go to 'Tools' > 'Scripts.'
-- Click the '+' button in the 'Scripts' window to add a new script.
-- Open the script (e.g., "ReloadWebcam.lua") and click 'Save.'

-- You should be all done! Enjoy!
Author
FlyingFathead
Views
3,494
First release
Last update
Rating
0.00 star(s) 0 ratings
Top