docs » OBS

Control OBS and react to its events, via the obs-websocket plugin.

Install and configure the obs-websocket plugin first from their project

Note: This Spoon will only work with Hammerspoon 0.9.100 or later.

Note: This Spoon will only work with obs-websocket 5.0.1 or later, which also requires OBS Studio v27 or later.

Download: https://github.com/Hammerspoon/Spoons/raw/master/Spoons/OBS.spoon.zip

Example:

-- This example will start OBS, connect to it, and then start streaming once connected
obs = hs.loadSpoon("OBS")
obsCallback = function(eventType, eventIntent, eventData)
  print(eventType)
  print(eventIntent)
  print(hs.inspect(eventData))

  if eventType == "SpoonOBSConnected" then
    obs:request("StartStream")
  end
end
obs:init(obsCallback, "localhost", 4444, "password")
obs:start()

API Overview

API Documentation

Constants

eventSubscriptionValues
Signature OBS.eventSubscriptionValues
Type Constant
Description

A table of the possible values for the eventSubscriptions parameter to OBS:init()

Notes
  • The keys are:
    • None - No events
    • General - General events
    • Config - Configuration events
    • Scenes - Scene events
    • Inputs - Input events
    • Transitions - Transition events
    • Filters - Filter events
    • Outputs - Output events
    • SceneItems - Scene item events
    • MediaInputs - Media input events
    • Vendors - Vendor events
    • UI - UI events
    • All - All of the above events
    • InputVolumeMeters - Input volume meter events
    • InputActiveStateChanged - Input active state changed events
    • InputShowStateChanged - Input show state changed events
    • SceneItemTransformChanged - Scene item transform changed events
  • For more information about these event categories and the events they contain, see the obs-websocket documentation
  • To combine these as a bitmask, use boolean operators, e.g. OBS.eventSubscriptionValues.General | OBS.eventSubscriptionValues.Scenes
  • The final four values are considered "high volume" events, so are not included in OBS.eventSubscriptionValues.All by default
Source Source/OBS.spoon/init.lua line 58

Variables

reconnectDelay
Signature OBS:reconnectDelay
Type Variable
Description

Controls how long to wait, in seconds, before attempting to reconnect to OBS. Defaults to 5

Source Source/OBS.spoon/init.lua line 112
shouldReconnect
Signature OBS:shouldReconnect
Type Variable
Description

Controls whether the websocket connection should be re-established if it is lost. Defaults to true

Source Source/OBS.spoon/init.lua line 105

Methods

addEventSubsciption
Signature OBS:addEventSubsciption(event)
Type Method
Description

Adds an event subscription

Parameters
Returns
  • None
Notes
  • If you wish to add multiple event subscriptions you can use the | operator to combine them, e.g. spoon.OBS:addEventSubscription(spoon.OBS.eventSubscriptionValues.Config | spoon.OBS.eventSubscriptionValues.Scenes)
Source Source/OBS.spoon/init.lua line 309
init
Signature OBS:init(eventCallback, host, port[, password, eventSubscriptions])
Type Method
Description

Initialisation method

Parameters
  • eventCallback - A function to be called when an event is received from OBS. The function will be passed a table containing the event data. The keys of the table are:
    • eventType - The type of event, e.g. StudioModeStateChanged
    • eventIntent - The event subscription value that caused this event to be sent, e.g. OBS.eventSubscriptionValues.General
    • eventData - A table containing the event data, e.g. { "studioModeEnabled": true }
  • host - The hostname or IP address of the machine running OBS
  • port - The port number that obs-websocket is listening on
  • password - An optional password string that obs-websocket is configured to use
  • eventSubscriptions - An optional number containing the bitmask of the events to subscribe to, see OBS.eventSubscriptionValues
Returns
  • None
Notes
  • This method does not connect to OBS, it just sets up the connection parameters for later use
  • By default, no events are subscribed to, so you will need to set eventSubscriptions to something useful if you want to receive events
  • If you do not wish to supply an eventCallback, pass nil instead
  • The events that OBS can produce are documented in the obs-websocket documentation
  • There are some additional values for eventType, specific to this Spoon:
    • SpoonOBSConnected - This event is sent when the websocket connection to OBS is established. All other fields will be nil.
    • SpoonOBSDisconnected - This event is sent when the websocket connection to OBS is lost. All other fields will be nil. Return false to inhibit automatic reconnection.
    • SpoonRequestResponse - This event is sent to pass replies to requests made via OBS:request(). Its eventIntent will be nil and the format of its eventData table will be:
      • requestId - The ID of the request that this is a reply to
      • requestType - The request type that was made
      • requestStatus - A table containing:
        • result - A bool, true if the request succeeded, otherwise false
        • code - A number containing the response code
        • comment - An optional string that may contain additional information about the response
      • responseData - An table that contains the response data, if any
    • SpoonBatchRequestResponse - This event is sent to pass replies to batch requests made via OBS:requestBatch(). Its eventIntent will be nil and the format of its eventData table will be:
      • requestId - The ID of the request batch that this is a reply to
      • results - A table containing the results of each request in the batch. Each entry in the table will be a table with the same format as SpoonRequestResponse
Source Source/OBS.spoon/init.lua line 117
removeEventSubsciption
Signature OBS:removeEventSubsciption(event)
Type Method
Description

Removes an event subscription

Parameters
Returns
  • None
Notes
  • If you wish to remove multiple event subscriptions you can use the & operator to combine them, e.g. spoon.OBS:removeEventSubscription(spoon.OBS.eventSubscriptionValues.Config | spoon.OBS.eventSubscriptionValues.Scenes)
Source Source/OBS.spoon/init.lua line 326
request
Signature OBS:request(requestType[, requestData[, requestId]])
Type Method
Description

Sends a request to OBS

Parameters
  • requestType - A string containing the type of request to send
  • requestData - An optional table containing the data to send with the request, or nil
  • requestId - An optional string containing the ID of the request
Returns
  • The requestId that was sent
Notes
  • If requestId is not specified then a random UUID will be generated.
  • The requestId will be passed to your event callback (provided to OBS:init()) when the response is received.
  • Values for requestType can be found in the obs-websocket documentation
Source Source/OBS.spoon/init.lua line 343
requestBatch
Signature OBS:requestBatch(requests[, haltOnFailure])
Type Method
Description

Sends a batch of requests to OBS

Parameters
  • requests - A table containing the requests to send
  • haltOnFailure - An optional boolean indicating whether to halt the batch if a request fails, defaults to false
Returns
  • The requestId that was sent for the batch
Notes
  • Each request should be a table with the keys:
    • requestType - A string containing the type of request to send, see OBS:request()
    • requestData - An optional table containing the data to send with the request, or nil
    • requestId - An optional string containing a unique ID for the request
  • Unlike OBS:request() the requestId is an auto-generated UUID

Example:

spoon.OBS:requestBatch({
 {["requestType"] = "StartVirtualCam"},
 {["requestType"] = "SetCurrentProgramScene", ["requestData"] = { ["sceneName"] = "FancyScene" }}
})
Source Source/OBS.spoon/init.lua line 377
setLogLevel
Signature OBS:setLogLevel(level)
Type Method
Description

Sets the logging level for the OBS Spoon

Parameters
Returns
  • None
Source Source/OBS.spoon/init.lua line 419
start
Signature OBS:start()
Type Method
Description

Connects to OBS

Parameters
  • None
Returns
  • None
Source Source/OBS.spoon/init.lua line 161
stop
Signature OBS:stop()
Type Method
Description

Disconnects from OBS

Parameters
  • None
Returns
  • None
Source Source/OBS.spoon/init.lua line 274
updateEventSubscriptions
Signature OBS:updateEventSubscriptions(eventSubscriptions)
Type Method
Description

Updates the event subscriptions

Parameters
Returns
  • None
Source Source/OBS.spoon/init.lua line 289