winform-obs
-
安裝 OBS軟體 目前使用版本27.0.1
-
為本機OBS安裝 websocket插件,這樣WinForm才有辦法連接發指令。obs-websocket API文件
-
開啟obs設定port位及密碼
-
設定輸出錄影格式
-
Windows Forms App(.NET Framework 4.6.1)
- 設定Form1.cs寬高
- 啟動OBS調整顯示器擷取範圍
-
建立WebView
- 下載 EO.Browser 相關檔案並安裝
- 安裝完畢照說明操作VisualStudio
- 開啟VisualStudio後, 先確認工具箱是否有EO.WebBrowser.WebControl、WebView
- 沒有就從"工具->選擇工具箱項目->瀏覽",找到安裝目錄(C:\Program Files\Essential Objects\EO.Total 2021)內的dll套件
- 再看看專案"參考"是否存在EO.WebBrowser、EO.WebBrowser.WinForm
- 若有缺漏,右鍵點擊"參考"加入參考,同樣瀏覽dll檔案並加入
- 在工具箱EO.WebBrowser找到WebControl,拖到視窗內同時自動創建WebView,設定寬高、名稱(預設webView1)
-
obs-websocket
- 直接使用NuGet安裝 obs-weboscket-dotnet4.9.0套件
- 對Form1.cs介面雙擊生成基本代碼,在From1建構式內指定URL
webView1.Url = "https://google.com.tw";
- 設定記憶體上限(否則會有記憶體不足問題)
EO.Base.Runtime.EnableEOWP = true;
- 建立OBS實體並監聽連接事件,開始連線
_obs = new OBSWebsocket(); _obs.Connected += onConnect; _obs.Connect("ws://127.0.0.1:4444", "123456");
- 連線成功開始取得OBSScene實體並設定
private void onConnect(object sender, EventArgs e) { //更換影片放置資料夾(目前測試無法更換...) _obs.SetRecordingFolder("D:\\git\\VisualStudio\\WebViewTest\\WebViewTest\\video"); string profile = _obs.GetCurrentProfile(); OBSScene scene = _obs.GetCurrentScene(); this.isConnect = true; }
- WebView添加自定義function讓頁面能驅動WinForm(Form1.Designer.cs)
//對webview註冊自定義function this.webView1.RegisterJSExtensionFunction("demoAbout", new JSExtInvokeHandler(WebView_JSDemoAbout));
- 接著讓webview內的網頁在適當時機呼叫對外function並帶入參數
window['demoAbout']("start_record");
- WinForm接收通知後開始/停止錄影
void WebView_JSDemoAbout(object sender, JSExtInvokeArgs e) { if (this.isConnect == false) { Console.WriteLine("尚未連線"); } string command = e.Arguments[0] as string; if (command == "start_record") { _obs.StartRecording(); } else { _obs.StopRecording(); //等待5秒存檔,並上傳FTP wait(5000); this.UploadFtpFile("Client\\Test\\Ray", "D:\\git\\VisualStudio\\WebViewTest\\WebViewTest\\video\\aaa.ts"); } }