iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⛵

Setting up an OBS Environment for AITubers (WSL2 Edition)

に公開
2023/12/05

Introduction

I recently bought the book titled "AITuber wo Tsukuttemitara Seisei AI Programming ga Yoku Wakatta Ken" (How Building an AITuber Taught Me Everything About Generative AI Programming)!
https://bookplus.nikkei.com/atcl/catalog/23/10/31/01079/
In this article, I tried the chapter on connecting a program with an application called OBS Studio as preparation for an AI character's YouTube stream.
I wanted to try connecting from WSL2 to the OBS WebSocket server, so I did some research on my own. I struggled with the connection to the WebSocket server on Windows because I didn't quite grasp the characteristics of the WSL2 environment, but I managed to solve it by asking GPT-4.

Current Development Environment

Click here for how to install Python on WSL2:
https://zenn.dev/yasuna/articles/b3eae0ed00e665
Click here for how to set up a virtual environment with venv on WSL2:
https://zenn.dev/yasuna/articles/a02113a90e2b39

Connecting from WSL2 to OBS on Windows

As preparation for YouTube streaming, I will try to see if I can run Python on WSL2 and connect to the OBS WebSocket server.
By the way, OBS Studio (OBS) is software that allows you to stream.
https://obsproject.com/ja/download

The steps in the AITuber book assume the code is executed from a Windows shell like PowerShell, but I tried running it from WSL2.
The following error appeared:

# Run code to connect to OBS by executing Python from WSL2
$ python3 obs_adapter.py
ConnectionRefusedError: [Errno 111] Connection refused
Traceback (most recent call last):
  File "/home/yasuna/projects/aituber/.venv/lib/python3.11/site-packages/obsws_python/baseclient.py", line 41, in __init__
    self.ws.connect(f"ws://{self.host}:{self.port}", timeout=self.timeout)
# (Omitted)

An error ConnectionRefusedError: [Errno 111] Connection refused is occurring.
So, let's use Cursor to solve it!
Here is the previous article I wrote about Cursor:
https://zenn.dev/yasuna/articles/15ab406c334997

Solving the Error with Cursor

I submitted this error to the GPT-4 chat in the AI-powered editor, Cursor.
The Python execution file is already open in the editor.

# Prompt
I got an error when running this file. Please solve it.
(Paste error message)
# Cursor Chat's Answer
Looking at the error message, a ConnectionRefusedError: [Errno 111] Connection refused has occurred. This indicates that the connection to the OBS (Open Broadcaster Software) WebSocket server was refused.

This issue can mainly occur for the following reasons:

1. OBS is not running, or the WebSocket plugin is not installed.
2. The environment variables OBS_WS_HOST, OBS_WS_PORT, and OBS_WS_PASSWORD are not set correctly.
3. You cannot connect to OBS due to network issues.

Solution:

1. Start OBS and ensure that the WebSocket plugin is installed.
2. Confirm that the environment variables are set correctly. Especially check if OBS_WS_HOST and OBS_WS_PORT point to the correct hostname and port number of the OBS WebSocket server.
3. Check your network connection and fix it if necessary.

Please try these steps. If the problem still persists, more detailed information might be needed.

I confirmed that 1 and 2 were correct, so I decided to test the network connection in step 3.

# Prompt
 Please tell me how to check the network connection.
# Cursor Chat's Answer
 Common ways to check network connections are as follows:

1. Use the Ping command: The Ping command is a basic tool for diagnosing network connection issues. Open a terminal and use the ping command to see if you can connect to the host where OBS is running. For example, if OBS_WS_HOST is localhost, run the following command:

ping localhost

If there is a response, the network connection is normal. If there is no response, there may be a problem with the network.

2. Use the Telnet command: Telnet is useful for checking if you can connect to a remote host on a specific port. For example, if OBS_WS_HOST is localhost and OBS_WS_PORT is 4444, run the following command:

telnet localhost 4444

If the connection is successful, the network connection is normal. If the connection is refused, that port may not be open, or there may be a problem with the network.

Please use these commands to check your network connection. If the problem still persists, more detailed information might be needed.

I started with step 1. Ping returned a response, so the network is connected normally.

(.venv) yasuna:~/projects/aituber $ ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.

When I tried to connect to the remote host by specifying the port in step 2, it didn't seem to work.
I got the same "Connection refused" as when I ran Python. This is the evidence (fact supporting the cause of the error).

(.venv) yasuna:~/projects/aituber $ telnet localhost 4444
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Formulating a Hypothesis Based on Evidence

Now that I have discovered the evidence—the fact supporting the cause of the error—I will formulate a hypothesis based on it.

Asking ChatGPT In-depth Questions

Since the cause of the error was the inability to connect to the port, I asked a more in-depth question. Because Cursor has a limit of 500 GPT-4 uses per month, I used ChatGPT for this follow-up.

# Prompt
How can I access a WebSocket port started by the OBS application on Windows from WSL2?
# ChatGPT's Answer
To access a Windows application from WSL2, you need to connect using the Windows IP address.

1) To find the Windows IP address, run the following command in the WSL2 terminal:

cat /etc/resolv.conf
As a result, you will see something like this:

nameserver 172.19.240.1
The IP address written after "nameserver" (in this example, 172.19.240.1) is the Windows IP address.

2) In your WebSocket client settings, specify this IP address and the port number as the connection destination.

For example, if you are using the Node.js websocket library, the code would look like this:

const WebSocket = require('ws');
const ws = new WebSocket('ws://172.19.240.1:PortNumber');
By following the method above, it is possible to access the WebSocket port started by the OBS application on Windows from WSL2.

It returned a wonderful solution. As a result, I was able to solve the problem with this.

Solution

As per ChatGPT's answer, the problem was solved by changing the localhost part to the Windows IP address.
It took me about two hours to get this far, but I managed to solve it using my own efforts and the power of GPT-4.
One needs to be careful when connecting to a WebSocket for an application running on Windows from WSL2.

Summary

When an error occurs, it's a chance to grow! I can think positively like that after solving it, but the time spent troubleshooting the error is definitely nerve-wracking.

By effectively using Cursor—with its GPT brains—and ChatGPT, I was somehow able to find a clue to the solution this time.

If I encounter another error, I want to keep moving forward with the help of AI in this way!
Thank you for reading this far.

GitHubで編集を提案

Discussion