OBS Studio Automatic Stop/Restart Video Input to Sync Audio

Automatically stop and restart video inputs periodically (every X minutes) to synchronize audio & video streams using AutoHotKey running under MS Windows

When you start streaming from a live video camera, the video and audio are wonderfully synchronized, but they can get out of sync. Typically, the video data takes longer for OBS to process than the audio. After a while, the video lags behind the audio because of the backlog of video data.

To fix it, stop and restart the lagging input source. In the Sources list, click the "eye" icon to shut off the input stream, wait 2 seconds, and click it again to restart the stream. This clears out the data queue and restarts the video/audio stream, freshly synchronized.

Eye icon in sources list

To avoid the need to run back to your computer every 15 minutes to click the "eye" icon, you can automate the process using the AutoHotKey application running under Windows. First do the following in OBS:
  1. OBS Settings > Hotkeys
  2. Scroll down and find your camera input stream.
  3. Click in the Stop field and type Ctrl-Shift-F4
  4. Click in the Restart field and type Ctrl-Shift-F5
  5. Click OK.
OBS hotkey settings

Now you can use the keyboard shortcuts Ctrl-Shift-F4 and Ctrl-Shift-F5 to stop and restart the input stream. If you have multiple inputs, you can do the same for each one using a different key sequence, such as Ctrl-Shift-F6 and Ctrl-Shift-F7.

To install and configure AutoHotKey:
  1. Download and install the AutoHotKey application, version v1.1.
  2. In a blank area of the desktop, right-click and choose New > AutoHotKey Script.
  3. A new AutoHotKey ".ahk" icon appears on your desktop. Change its name to ResetOBS.ahk

  4. AutoHotKey desktop icon

  5. Right-click the icon and choose Edit Script. The script is opened in Notepad.
  6. Copy the whole block of text in the box below, then paste it into the Notepad editor below the existing few lines of the script.
  7. Read the script so you can understand it. You might want to edit the numbers in the "SetTimer" and "Click" instructions.
  8. In Notepad, execute File > Save, then File > Exit.

  9. Note: The script is in AutoHotKey version v1.1 format. If a volunteer writes the same script in version v2.0 format and sends it to me, I'll post it here.

    ; ------------ Stop & Restart OBS input streams ------------- Gosub, RestartInputs ; ------------------------------------------------------------ #Persistent ; Runs forever. To stop it, right-click "H" icon ; in system tray and choose Exit ; Set time interval between input restarts SetTimer, RestartInputs, 900000 ; 900000 msec = 15 minutes ; 1200000 msec = 20 minutes ; 1800000 msec = 30 minutes Return ; ----------------------------------------------------------------- RestartInputs: SoundBeep 650, 400 ; warning beeps that restart is about to happen SoundBeep 750, 500 Sleep 4000 ; 4 seconds delay -- pause your mouse usage CoordMode, Mouse, Screen ; Use screen (not window) coordinates Click, 1600 50 ; Click OBS window at upper-right of screen: ; 1600 pixels from left side of screen ; 50 pixels down from top of screen ; (coordinates for OBS window location) Sleep 1000 ; Brief wait before stopping first input stream ; Beforehand, configure keystrokes in OBS: ; Settings > Hotkeys > subwindow name > Stop > keystroke ; Settings > Hotkeys > subwindow name > Restart > keystroke Send, ^+{F4} ; Control-Shift-F4 key, stop input stream Sleep 2000 ; Delay 2 seconds Send, ^+{F5} ; Control-Shift-F5 key, restart input stream Sleep 5000 ; Pause to wait for first subwindow to reappear Send, ^+{F6} ; repeat for second OBS subwindow input stream Sleep 2000 ; (can delete these 3 lines if you have only Send, ^+{F7} ; one input stream, no harm in leaving them in) Return ; End of RestartInputs subroutine

To use the script, double-click the ResetOBS.ahk icon. You hear 2 beeps, then the mouse moves to the upper-right area of your desktop and clicks there to bring the OBS window to the front, making it the current window (in case it's not already). Then it automatically types the keys to stop and restart the streams. It repeats all this once every 15 minutes.

To stop the script from running, go to the green "H" icon in the system tray, right-click it, and choose Exit.

Exit AutoHotKey



© 2022 Gray Chang
Comments? Questions? SFBayNestCam [at] gmail [dot] com