Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8906

node.jsからWebsockets経由でOBSを操作する

$
0
0

動画配信ソフトのOBSをコードから操作して自動化したいなと思ったので調べたメモです。

obs-websocket

WebSocketsでOBSを操作出来るようにするプラグインです。
https://github.com/Palakis/obs-websocket

インストーラーが用意されているのでGUIからインストールするだけでOBSメニューの「ツール」にWebSockets Server Settingsという項目が追加されます。

obs-websocket-js

上記のWebsockets APIのJSラッパーです。
https://github.com/haganbmj/obs-websocket-js

READMEに書いてある通りに書けば動きます。
exampleそのままですがコメント付きのサンプルコードも乗せておきます。

// ライブラリのimportconstOBSWebSocket=require('obs-websocket-js');// インスタンス初期化constobs=newOBSWebSocket();// OBSに接続してPromiseを受け取るobs.connect({address:'localhost:4444',password:'$up3rSecretP@ssw0rd'})// 接続成功.then(()=>{console.log(`Success! We're connected & authenticated.`);// シーンの一覧を取得するリクエストreturnobs.send('GetSceneList');}).then(data=>{console.log(`${data.scenes.length} Available Scenes!`);// シーンの一覧から現在のシーンを探すdata.scenes.forEach(scene=>{if(scene.name!==data.currentScene){console.log(`Found a different scene! Switching to Scene: ${scene.name}`);// 現在のシーンを切り替えるリクエストobs.send('SetCurrentScene',{'scene-name':scene.name});}});}).catch(err=>{// Promise convention dicates you have a catch on every chain.console.log(err);});// シーンが切り替わったイベントをObserveするobs.on('SwitchScenes',data=>{console.log(`New Active Scene: ${data.sceneName}`);});// エラーをObserveするobs.on('error',err=>{console.error('socket error:',err);});

API リファレンス

ここです。ここに書かれているものは基本的にobs-websocket-jsからでも使えるようです。
https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md

使用例

https://note.com/kirimin_chan/n/n0d44d734d3c4


Viewing all articles
Browse latest Browse all 8906

Trending Articles