FlyingFathead
New Member
Hi there!
Today, I made a simple script to reload your desired USB webcam source every x minutes (user definable). I've noticed this works great in handling things like webcam freezes, or i.e. in cases where you're doing outdoor streams and the camera doesn't adjust to changes in lighting conditions.
I have added the code to this site's Resources-category (however, as of writing this, it's still waiting to be accepted):
obsproject.com
The code is also available from my Github pages as a gist.
gist.github.com
Or, you can just use this snippet below and save it as (i.e.) "WebcamReload.lua", change the interval and the 'UVC Webcam' source name to whatever name you have given your video source in OBS's video sources list. Here:
I know this is an extremely "dumb" workaround for OBS's webcam freeze-up issues, but it might be of high use to some, since it pretty much solved all of my webcam freezing (Ubuntu 22.04, OBS Studio 29.0.2), especially the ones that usually happen over time.
I have been running it on a multi-webcam setup for hours now and I don't even notice any reload drop-outs when the camera source is reloaded. And yes, I've experienced this same freeze-up issue on multiple computers and multiple webcams over the years with OBS, even with high-end workstations.
I hope this is of help to you, since it finally seemed to have worked things out for me. *knocks on wood*
Let me know if there's any improvements that the script could use.
Cheers!
Today, I made a simple script to reload your desired USB webcam source every x minutes (user definable). I've noticed this works great in handling things like webcam freezes, or i.e. in cases where you're doing outdoor streams and the camera doesn't adjust to changes in lighting conditions.
I have added the code to this site's Resources-category (however, as of writing this, it's still waiting to be accepted):
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...
The code is also available from my Github pages as a gist.

OBS Studio Script: WebcamReload v1.1
OBS Studio Script: WebcamReload v1.1. GitHub Gist: instantly share code, notes, and snippets.
Or, you can just use this snippet below and save it as (i.e.) "WebcamReload.lua", change the interval and the 'UVC Webcam' source name to whatever name you have given your video source in OBS's video sources list. Here:
Lua:
-- Install: open up a plaintext editor, copy-paste this code in and save it as (i.e.): ReloadWebcam.lua
-- [start of lua code]
obs = obslua
-- The name of the video source is what the title of it is in your OBS video sources list.
-- For clarity, you can i.e. rename your video source in OBS to 'Cam1', 'Cam2' etc and then just add a variation of this script per each camera that you want to refresh as a source every x minutes.
-- Replace 'UVC Webcam' value below with the name of your video capture source
local source_name = 'UVC Webcam'
-- Replace the value underneath (default: 1) with the number of minutes between source reloads
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.obs_source_dec_showing(source)
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]
-- 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.
-- Then, 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!
I know this is an extremely "dumb" workaround for OBS's webcam freeze-up issues, but it might be of high use to some, since it pretty much solved all of my webcam freezing (Ubuntu 22.04, OBS Studio 29.0.2), especially the ones that usually happen over time.
I have been running it on a multi-webcam setup for hours now and I don't even notice any reload drop-outs when the camera source is reloaded. And yes, I've experienced this same freeze-up issue on multiple computers and multiple webcams over the years with OBS, even with high-end workstations.
I hope this is of help to you, since it finally seemed to have worked things out for me. *knocks on wood*
Let me know if there's any improvements that the script could use.
Cheers!