Skip to content

Instantly share code, notes, and snippets.

@FlyingFathead
Last active December 17, 2023 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlyingFathead/c95cb13a0a1fee6b60e1c9d34b02ebcd to your computer and use it in GitHub Desktop.
Save FlyingFathead/c95cb13a0a1fee6b60e1c9d34b02ebcd to your computer and use it in GitHub Desktop.
OBS Studio Script: WebcamReload v1.1
-- 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!
@RepathZT
Copy link

This doesn't seem to work for me. It turns off my webcam, but never turns it back on so it just leaves a black screen forever after 1 minute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment