Elektordi / obs-websocket-py

Python library to communicate with an obs-websocket server (for OBS Studio)
MIT License
235 stars 59 forks source link

ConnectionRefusedError: Unable to make OBS and obs-websocket-py cooperate #87

Open austin-funk opened 1 year ago

austin-funk commented 1 year ago

Hi all, I'm currently working on a script that can dynamically save video segments to different folders based on the time of day. However, I'm getting stuck on the first step, and have very little networking experience to lean on. Any help would be greatly appreciated. I've tried using OBS version 29.0.0 and am currently using version 27.1.3. I was using Python 3.7 but switched to 3.6.8 to be compatible with the older version of OBS. I've tried two versions of obs-websocket, 5.0.0 and 4.9.1. I've tried turning off all firewalls and both using a password and not using a password. I was able to see and change options for the websocket under Tools in OBS 29.0.0, but even with multiple reinstalls I can't make it work for 27.1.3. Here is the error message from OBS when using OBS 27.1.3, Python 3.6.8, websocket 4.9.1, no firewalls, no authentication:

[####.py] Traceback (most recent call last):
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\obswebsocket\core.py", line 84, in connect
[####.py]     self.ws.connect(url)
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_core.py", line 250, in connect
[####.py]     options.pop('socket', None))
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_http.py", line 130, in connect
[####.py]     sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_http.py", line 206, in _open_socket
[####.py]     raise err
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_http.py", line 185, in _open_socket
[####.py]     sock.connect(address)
*[####.py] ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it*
[####.py]
[####.py] During handling of the above exception, another exception occurred:
[####.py] 
[####.py] Traceback (most recent call last):
[####.py]   File "C:/Users/####/Documents/OBScripts\####.py", line 18, in <module>
[####.py]     client.connect()
[####.py]   File "C:\Users\####\AppData\Local\Programs\Python\Python36\lib\site-packages\obswebsocket\core.py", line 108, in connect
[####.py]     raise exceptions.ConnectionFailure(str(e))
[####.py] obswebsocket.exceptions.ConnectionFailure: [WinError 10061] No connection could be made because the target machine actively refused it

Here is the code associated with this error message. I have commented out sections of it and found that the issue occurs when client.connect() is uncommented:

import time
import obspython as obs
import obswebsocket

def script_description():
    return """<center><h2>####</h2></center>
              <p>Continuously saves increments of videos and deletes
              old videos for storage reasons.</p>"""

base_path = "C:\\Users\\####\\Videos\\NewFolder"
seconds_passed = 0
socket_password = "RfdsH833NbKSDpl09DdieqwpmvnIJf121fJ8Njf8GFY8jdb3"

client = obswebsocket.obsws("localhost", 4455)
client.connect()
# client.call(obswebsocket.requests.SetRecordingFolder(base_path))
print(base_path)
client.disconnect()
iarspider commented 1 year ago

The error basically says "there is no process that expects connection on a specified port" (4455 in this case). I am not sure what version of OBS Websockets was on 27.1.3, probably the old one (v4), so you need to connect differently:

client = obswebsocket.obsws("localhost", 4445, legacy=True)

(you can always check the port in OBS-websocket settings)