Advertisement
Guest User

OBSCommand latest source

a guest
Sep 23rd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 21.48 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Text
  3. Imports OBSWebsocketDotNet
  4. Imports Newtonsoft.Json.Linq
  5.  
  6. Module Main
  7.  
  8.     Private _obs As OBSWebsocket
  9.  
  10.     Sub Main()
  11.  
  12.         Dim args As String() = Environment.GetCommandLineArgs
  13.         Dim password As String = ""
  14.         Dim server As String = "ws://127.0.0.1:4444"
  15.  
  16.         Dim profile As String = ""
  17.         Dim scene As String = ""
  18.         Dim hidesource As String = ""
  19.         Dim showsource As String = ""
  20.         Dim togglesource As String = ""
  21.         Dim toggleaudio As String = ""
  22.         Dim mute As String = ""
  23.         Dim unmute As String = ""
  24.         Dim setvolume As String = ""
  25.         Dim stopstream As Boolean = False
  26.         Dim startstream As Boolean = False
  27.         Dim startrecording As Boolean = False
  28.         Dim stoprecording As Boolean = False
  29.         Dim command As String = ""
  30.         Dim delay As String = ""
  31.         Dim setdelay As Double
  32.  
  33.         If args.Count = 1 Then
  34.             PrintUsage()
  35.             End
  36.         End If
  37.  
  38.         Dim myout As TextWriter = Console.Out
  39.         Dim builder As StringBuilder = New StringBuilder()
  40.         Dim writer As TextWriter = New StringWriter(builder)
  41.         Console.SetOut(writer)
  42.  
  43.         Dim isInitialized As Boolean = False
  44.         Dim skipfirst As Boolean = False
  45.         Dim argsindex As Integer
  46.  
  47.         For Each arg As String In args
  48.             argsindex += 1
  49.             If skipfirst = False Then
  50.                 skipfirst = True
  51.                 Continue For
  52.             End If
  53.  
  54.             ' Clear variables
  55.             profile = ""
  56.             scene = ""
  57.             command = ""
  58.             hidesource = ""
  59.             showsource = ""
  60.             togglesource = ""
  61.             toggleaudio = ""
  62.             mute = ""
  63.             unmute = ""
  64.             setvolume = ""
  65.             stopstream = False
  66.             startstream = False
  67.             startrecording = False
  68.             stoprecording = False
  69.             delay = ""
  70.  
  71.             If arg = "?" Or arg = "/?" Or arg = "-?" Or arg = "help" Or arg = "/help" Or arg = "-help" Then
  72.                 PrintUsage()
  73.                 End
  74.             End If
  75.  
  76.             If arg.StartsWith("/server=") Then
  77.                 server = "ws://" & arg.Replace("/server=", "")
  78.                 Continue For ' get credentials first before trying to connect!
  79.             End If
  80.             If arg.StartsWith("/password=") Then
  81.                 password = arg.Replace("/password=", "")
  82.                 Continue For ' get credentials first before trying to connect!
  83.             End If
  84.  
  85.             If arg.StartsWith("/setdelay=") Then
  86.                 Dim tmp As String = arg.Replace("/setdelay=", "")
  87.                 If Not IsNumeric(tmp) Then
  88.                     Console.SetOut(myout)
  89.                     Console.WriteLine("Error: setdelay is not numeric")
  90.                     Continue For
  91.                 Else
  92.                     setdelay = Double.Parse(tmp, System.Globalization.CultureInfo.InvariantCulture)
  93.                     Continue For
  94.                 End If
  95.             End If
  96.  
  97.             If arg.StartsWith("/delay=") Then
  98.                 Dim tmp As String = arg.Replace("/delay=", "")
  99.                 If Not IsNumeric(tmp) Then
  100.                     Console.SetOut(myout)
  101.                     Console.WriteLine("Error: delay is not numeric")
  102.                     Continue For
  103.                 Else
  104.                     Threading.Thread.Sleep(Double.Parse(tmp, System.Globalization.CultureInfo.InvariantCulture) * 1000)
  105.                     Continue For
  106.                 End If
  107.             End If
  108.  
  109.             If arg.StartsWith("/profile=") Then
  110.                 profile = arg.Replace("/profile=", "")
  111.             End If
  112.             If arg.StartsWith("/scene=") Then
  113.                 scene = arg.Replace("/scene=", "")
  114.             End If
  115.             If arg.StartsWith("/command=") Then
  116.                 command = arg.Replace("/command=", "")
  117.             End If
  118.             If arg.StartsWith("/hidesource=") Then
  119.                 hidesource = arg.Replace("/hidesource=", "")
  120.             End If
  121.             If arg.StartsWith("/showsource=") Then
  122.                 showsource = arg.Replace("/showsource=", "")
  123.             End If
  124.             If arg.StartsWith("/togglesource=") Then
  125.                 togglesource = arg.Replace("/togglesource=", "")
  126.             End If
  127.             If arg.StartsWith("/toggleaudio=") Then
  128.                 toggleaudio = arg.Replace("/toggleaudio=", "")
  129.             End If
  130.             If arg.StartsWith("/mute=") Then
  131.                 mute = arg.Replace("/mute=", "")
  132.             End If
  133.             If arg.StartsWith("/unmute=") Then
  134.                 unmute = arg.Replace("/unmute=", "")
  135.             End If
  136.             If arg.StartsWith("/setvolume=") Then
  137.                 setvolume = arg.Replace("/setvolume=", "")
  138.             End If
  139.             If arg = "/startstream" Then
  140.                 startstream = True
  141.             End If
  142.             If arg = "/stopstream" Then
  143.                 stopstream = True
  144.             End If
  145.             If arg = "/startrecording" Then
  146.                 startrecording = True
  147.             End If
  148.             If arg = "/stoprecording" Then
  149.                 stoprecording = True
  150.             End If
  151.             Try
  152.  
  153.                 If isInitialized = False Then
  154.                     isInitialized = True
  155.                     _obs = New OBSWebsocket()
  156.                     _obs.WSTimeout = New TimeSpan(0, 0, 0, 3)
  157.                     _obs.Connect(server, password)
  158.                 End If
  159.  
  160.                 If profile <> "" Then
  161.                     Dim fields As New JObject()
  162.                     fields.Add("profile-name", profile)
  163.                     _obs.SendRequest("SetCurrentProfile", fields)
  164.                 End If
  165.                 If scene <> "" Then
  166.                     Dim fields As New JObject
  167.                     fields.Add("scene-name", scene)
  168.                     Dim response As JObject = _obs.SendRequest("SetCurrentScene", fields)
  169.                 End If
  170.                 If command <> "" Then
  171.                     Dim myParameter As Object = Nothing
  172.  
  173.                     Try
  174.  
  175.                         If command.Contains(",") Then
  176.                             Dim tmp As String() = command.Split(",")
  177.  
  178.  
  179.                             Dim fields As New JObject
  180.                             For a = 1 To tmp.Count - 1
  181.                                 Dim tmpsplit As String() = tmp(a).Split("=")
  182.                                 If tmpsplit.Count < 2 Then
  183.                                     Console.SetOut(myout)
  184.                                     Console.WriteLine("Error with command """ & command & """: " & "Missing a = in Name=Type")
  185.                                 End If
  186.                                 If IsNumeric(tmpsplit(1)) Then
  187.                                     If tmpsplit(1).Contains(".") Then
  188.                                         fields.Add(tmpsplit(0), Double.Parse(tmpsplit(1), System.Globalization.CultureInfo.InvariantCulture))
  189.                                     Else
  190.                                         fields.Add(tmpsplit(0), CInt(tmpsplit(1)))
  191.                                     End If
  192.                                 ElseIf tmpsplit(1).ToUpper = "TRUE" Or tmpsplit(1).ToUpper = "FALSE" Then
  193.                                     fields.Add(tmpsplit(0), CBool(tmpsplit(1)))
  194.                                 Else
  195.                                     fields.Add(tmpsplit(0), tmpsplit(1))
  196.                                 End If
  197.                             Next
  198.                             _obs.SendRequest(tmp(0), fields)
  199.  
  200.                         Else
  201.                             _obs.SendRequest(command)
  202.                         End If
  203.  
  204.                     Catch ex As Exception
  205.                         Console.SetOut(myout)
  206.                         Console.WriteLine("Error with command """ & command & """: " & ex.Message.ToString())
  207.                     End Try
  208.                 End If
  209.                 If hidesource <> "" Then
  210.                     If hidesource.Contains("/") Then
  211.                         Dim tmp As String() = hidesource.Split("/")
  212.  
  213.                         ' scene/source
  214.                         If tmp.Count = 2 Then
  215.                             '_obs.SetSourceRender(tmp(1), False, tmp(0))
  216.                             Dim fields As New JObject
  217.                             fields.Add("source", tmp(1))
  218.                             fields.Add("render", False)
  219.                             fields.Add("scene-name", tmp(0))
  220.                             _obs.SendRequest("SetSourceRender", fields)
  221.                         End If
  222.                     Else
  223.                         '_obs.SetSourceRender(hidesource, False)
  224.                         Dim fields As New JObject
  225.                         fields.Add("source", hidesource)
  226.                         fields.Add("render", False)
  227.                         _obs.SendRequest("SetSourceRender", fields)
  228.                     End If
  229.                 End If
  230.                 If showsource <> "" Then
  231.                     If showsource.Contains("/") Then
  232.                         Dim tmp As String() = showsource.Split("/")
  233.  
  234.                         ' scene/source
  235.                         If tmp.Count = 2 Then
  236.                             '_obs.SetSourceRender(tmp(1), True, tmp(0))
  237.                             Dim fields As New JObject
  238.                             fields.Add("source", tmp(1))
  239.                             fields.Add("render", True)
  240.                             fields.Add("scene-name", tmp(0))
  241.                             _obs.SendRequest("SetSourceRender", fields)
  242.                         End If
  243.                     Else
  244.                         '_obs.SetSourceRender(showsource, True)
  245.                         Dim fields As New JObject
  246.                         fields.Add("source", showsource)
  247.                         fields.Add("render", True)
  248.                         _obs.SendRequest("SetSourceRender", fields)
  249.                     End If
  250.  
  251.                 End If
  252.                 If togglesource <> "" Then
  253.                     If togglesource.Contains("/") Then
  254.                         Dim tmp As String() = togglesource.Split("/")
  255.  
  256.                         ' scene/source
  257.                         If tmp.Count = 2 Then OBSToggleSource(tmp(1), tmp(0))
  258.                     Else
  259.                         OBSToggleSource(togglesource)
  260.                     End If
  261.                 End If
  262.                 If toggleaudio <> "" Then
  263.                     '_obs.ToggleMute(toggleaudio)
  264.                     Dim fields As New JObject
  265.                     fields.Add("source", toggleaudio)
  266.                     _obs.SendRequest("ToggleMute", fields)
  267.                 End If
  268.                 If mute <> "" Then
  269.                     '_obs.SetMute(mute, True)
  270.                     Dim fields As New JObject
  271.                     fields.Add("source", mute)
  272.                     fields.Add("mute", True)
  273.                     _obs.SendRequest("SetMute", fields)
  274.                 End If
  275.                 If unmute <> "" Then
  276.                     '_obs.SetMute(unmute, False)
  277.                     Dim fields As New JObject
  278.                     fields.Add("source", unmute)
  279.                     fields.Add("mute", False)
  280.                     _obs.SendRequest("SetMute", fields)
  281.                 End If
  282.                 If setvolume <> "" Then
  283.                     ' source/volume,delay
  284.                     Dim tmp As String() = setvolume.Split(",")
  285.                     If Not IsNumeric(tmp(1)) Then Throw New Exception("Volume value is not nummeric (0-100)!")
  286.                     If tmp.Count = 2 Then
  287.                         OBSSetVolume(tmp(0), tmp(1))
  288.                     ElseIf tmp.Count = 3 Then
  289.                         If Not IsNumeric(tmp(2)) Then Throw New Exception("Delay value is not nummeric (0-x)!")
  290.                         OBSSetVolume(tmp(0), tmp(1), tmp(2))
  291.                     End If
  292.                 End If
  293.                 If startstream = True Then
  294.                     '_obs.StartStreaming()
  295.                     _obs.SendRequest("StartStreaming")
  296.  
  297.                 End If
  298.                 If stopstream = True Then
  299.                     '_obs.StopStreaming()
  300.                     _obs.SendRequest("StopStreaming")
  301.                 End If
  302.                 If startrecording = True Then
  303.                     '_obs.StartRecording()
  304.                     _obs.SendRequest("StartRecording")
  305.                 End If
  306.                 If stoprecording = True Then
  307.                     '_obs.StopRecording()
  308.                     _obs.SendRequest("StopRecording")
  309.                 End If
  310.  
  311.                 If setdelay > 0 And argsindex < args.Count And argsindex > 1 Then
  312.                     Threading.Thread.Sleep(setdelay * 1000)
  313.                 End If
  314.  
  315.             Catch ex As Exception
  316.                 Try
  317.                     _obs.Disconnect()
  318.                 Catch ex2 As Exception
  319.  
  320.                 End Try
  321.                 Console.SetOut(myout)
  322.                 Console.WriteLine("Error: " & ex.Message.ToString())
  323.                 End
  324.             End Try
  325.         Next
  326.         Try
  327.             _obs.Disconnect()
  328.             Console.SetOut(myout)
  329.             Console.WriteLine("Ok")
  330.         Catch ex As Exception
  331.  
  332.         End Try
  333.  
  334.  
  335.     End Sub
  336.  
  337.     Private Sub EventReceived()
  338.  
  339.     End Sub
  340.  
  341.     Private Sub OBSToggleSource(ByVal source As String, Optional ByVal sceneName As String = "")
  342.  
  343.         Dim fields As New JObject
  344.         If sceneName <> "" Then fields.Add("scene-name", sceneName)
  345.         fields.Add("item", source)
  346.         Dim response As JObject = _obs.SendRequest("GetSceneItemProperties", fields)
  347.  
  348.         fields = New JObject
  349.         If sceneName <> "" Then fields.Add("scene-name", sceneName)
  350.         fields.Add("item", source)
  351.         fields.Add("visible", Not Convert.ToBoolean(response.GetValue("visible")))
  352.         _obs.SendRequest("SetSceneItemProperties", fields)
  353.  
  354.     End Sub
  355.  
  356.     Private Sub OBSSetVolume(ByVal source As String, ByVal volume As Integer, Optional ByVal delay As Integer = 0)
  357.         If delay = 0 Then
  358.             '_obs.SetVolume(source, volume / 100)
  359.             _obs.SendRequest("SetVolume", New JObject(source, volume / 100))
  360.  
  361.         Else
  362.             If delay > 10 Then delay = 10
  363.             If delay > 1000 Then delay = 1000
  364.             'Dim _VolumeInfo As VolumeInfo = _obs.GetVolume(source)
  365.             Dim fields As New JObject
  366.             fields.Add("source", source)
  367.  
  368.             Dim _VolumeInfo As JObject = _obs.SendRequest("GetVolume", fields)
  369.  
  370.             Dim startvolume As Integer = CInt(_VolumeInfo.GetValue("volume")) * 100
  371.  
  372.             If startvolume = volume Then
  373.                 Exit Sub
  374.             ElseIf startvolume < volume Then
  375.                 For a = startvolume To volume
  376.                     '_obs.SetVolume(source, (a / 100))
  377.                     fields = New JObject
  378.                     fields.Add("source", source)
  379.                     fields.Add("volume", a / 100)
  380.                     _obs.SendRequest("SetVolume", fields)
  381.                     Threading.Thread.Sleep(delay)
  382.                 Next
  383.             ElseIf startvolume > volume Then
  384.                 For a = startvolume To volume Step -1
  385.                     '_obs.SetVolume(source, (a / 100))
  386.                     fields = New JObject
  387.                     fields.Add("source", source)
  388.                     fields.Add("volume", a / 100)
  389.                     _obs.SendRequest("SetVolume", fields)
  390.                     Threading.Thread.Sleep(delay)
  391.                 Next
  392.             End If
  393.         End If
  394.     End Sub
  395.  
  396.     Private Sub PrintUsage()
  397.         Dim out As List(Of String) = New List(Of String)
  398.  
  399.         out.Add("OBSCommand v1.4.7 ©2018-2019 by FSC-SOFT (http://www.VoiceMacro.net)")
  400.         out.Add(vbCrLf)
  401.         out.Add("Usage:")
  402.         out.Add("------")
  403.         out.Add("OBSCommand.exe /server=127.0.0.1:4444 /password=xxxx /delay=0.5 /setdelay=0.05 /profile=myprofile /scene=myscene /hidesource=myscene/mysource /showsource=myscene/mysource /togglesource=myscene/mysource /toggleaudio=myaudio /mute=myaudio /unmute=myaudio /setvolume=mysource,volume,delay /startstream /stopstream /startrecording /stoprecording /command=mycommand,myparam1=myvalue1...")
  404.         out.Add(vbCrLf)
  405.         out.Add("Note: If Server is omitted, default 127.0.0.1:4444 will be used.")
  406.         out.Add("Use quotes if your item name includes spaces.")
  407.         out.Add("Password can be empty if no password is set in OBS Studio.")
  408.         out.Add("You can use the same option multiple times.")
  409.         out.Add("If you use Server and Password, those must be the first 2 options!")
  410.         out.Add(vbCrLf)
  411.         out.Add("This tool uses the obs-websocket plugin to talk to OBS Studio:")
  412.         out.Add("https://github.com/Palakis/obs-websocket/releases")
  413.         out.Add(vbCrLf)
  414.         out.Add("Dynamic link libraries used:")
  415.         out.Add("Json.NET ©2008 by James Newton-King")
  416.         out.Add("websocket-sharp ©2010-2016 by sta.blockhead")
  417.         out.Add("obs-websocket-dotnet ©2017 by Stéphane Lepin.")
  418.         out.Add(vbCrLf)
  419.         out.Add("Examples:")
  420.         out.Add("---------")
  421.         out.Add("OBSCommand.exe /scene=myscene")
  422.         out.Add("OBSCommand.exe /toggleaudio=""Desktop Audio""")
  423.         out.Add("OBSCommand.exe /mute=myAudioSource")
  424.         out.Add("OBSCommand.exe /unmute=""my Audio Source""")
  425.         out.Add("OBSCommand.exe /setvolume=Mic/Aux,0,50")
  426.         out.Add("OBSCommand.exe /setvolume=Mic/Aux,100")
  427.         out.Add("OBSCommand.exe /stopstream")
  428.         out.Add("OBSCommand.exe /profile=myprofile /scene=myscene /showsource=mysource")
  429.         out.Add("OBSCommand.exe /showsource=mysource")
  430.         out.Add("OBSCommand.exe /hidesource=myscene/mysource")
  431.         out.Add("OBSCommand.exe /togglesource=myscene/mysource")
  432.         out.Add("OBSCommand.exe /showsource=""my scene""/""my source""")
  433.         out.Add("OBSCommand.exe /command=SaveReplayBuffer")
  434.         out.Add("OBSCommand.exe /command=TakeSourceScreenshot,sourceName=MyScene,PictureFormat=png,saveToFilePath=C:\OBSTest.png")
  435.         out.Add("OBSCommand.exe /scene=mysource1 /delay=1.555 /scene=mysource2")
  436.         out.Add("OBSCommand.exe /setdelay=1.555 /scene=mysource1 /scene=mysource2")
  437.         out.Add(vbCrLf)
  438.         out.Add("Options:")
  439.         out.Add("--------")
  440.         out.Add("/server=127.0.0.1:4444            define server address and port")
  441.         out.Add("  Note: If Server is omitted, default 127.0.0.1:4444 will be used.")
  442.         out.Add("/password=xxxx                    define password (can be omitted)")
  443.         out.Add("/delay=n.nnn                      delay in seconds (0.001 = 1 ms)")
  444.         out.Add("/setdelay=n.nnn                   global delay in seconds (0.001 = 1 ms)")
  445.         out.Add("                                  (set it to 0 to cancel it)")
  446.         out.Add("/profile=myprofile                switch to profile ""myprofile""")
  447.         out.Add("/scene=myscene                    switch to scene ""myscene""")
  448.         out.Add("/hidesource=myscene/mysource      hide source ""scene/mysource""")
  449.         out.Add("/showsource=myscene/mysource      show source ""scene/mysource""")
  450.         out.Add("/togglesource=myscene/mysource    toggle source ""scene/mysource""")
  451.         out.Add("  Note:  if scene is omitted, current scene is used")
  452.         out.Add("/toggleaudio=myaudio              toggle mute from audio source ""myaudio""")
  453.         out.Add("/mute=myaudio                     mute audio source ""myaudio""")
  454.         out.Add("/unmute=myaudio                   unmute audio source ""myaudio""")
  455.         out.Add("/setvolume=myaudio,volume,delay   set volume of audio source ""myaudio""")
  456.         out.Add("                                  volume is 0-100, delay is in milliseconds")
  457.         out.Add("                                  between steps (min. 10, max. 1000) for fading")
  458.         out.Add("  Note:  if delay is omitted volume is set instant")
  459.         out.Add("/startstream                      starts streaming")
  460.         out.Add("/stopstream                       stop streaming")
  461.         out.Add("/startrecording                   starts recording")
  462.         out.Add("/stoprecording                    stops recording")
  463.         out.Add("")
  464.         out.Add("General User Command syntax:")
  465.         out.Add("/command=mycommand,myparam1=myvalue1,myparam2=myvalue2...")
  466.         out.Add("                                  issues user command,parameter(s) (optional)")
  467.         out.Add("")
  468.         out.Add("A full list of commands is available here https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md")
  469.         out.Add("")
  470.  
  471.         Dim i As Integer = 0
  472.         Dim z As Integer = 0
  473.  
  474.         Do While True
  475.  
  476.             Console.WriteLine(out(i))
  477.             If z = Console.WindowHeight - 6 Then
  478.                 Console.Write("Press any key to continue...")
  479.                 Console.ReadKey()
  480.                 ClearCurrentConsoleLine()
  481.                 z = 0
  482.             End If
  483.             i += 1
  484.             z += 1
  485.             If i >= out.Count Then Exit Do
  486.  
  487.             If out(i).Length > Console.WindowWidth Then
  488.                 z += 1
  489.             End If
  490.             If out(i).Length > Console.WindowWidth * 2 Then
  491.                 z += 1
  492.             End If
  493.         Loop
  494.  
  495.     End Sub
  496.  
  497.  
  498.     Public Sub ClearCurrentConsoleLine()
  499.         Dim currentLineCursor As Integer = Console.CursorTop
  500.         Console.SetCursorPosition(0, Console.CursorTop)
  501.         Console.Write(New String(" "c, Console.WindowWidth))
  502.         Console.SetCursorPosition(0, currentLineCursor)
  503.     End Sub
  504. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement