Advertisement
Guest User

OBSCommand source

a guest
Feb 1st, 2019
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.69 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.  
  30.         If args.Count = 1 Then
  31.             PrintUsage()
  32.             End
  33.         End If
  34.  
  35.         For Each arg As String In args
  36.             If arg = "?" Or arg = "/?" Or arg = "-?" Or arg = "help" Or arg = "/help" Or arg = "-help" Then
  37.                 PrintUsage()
  38.                 End
  39.             End If
  40.             If arg.StartsWith("/server=") Then
  41.                 server = "ws://" & arg.Replace("/server=", "")
  42.             End If
  43.             If arg.StartsWith("/password=") Then
  44.                 password = arg.Replace("/password=", "")
  45.             End If
  46.             If arg.StartsWith("/profile=") Then
  47.                 profile = arg.Replace("/profile=", "")
  48.             End If
  49.             If arg.StartsWith("/scene=") Then
  50.                 scene = arg.Replace("/scene=", "")
  51.             End If
  52.             If arg.StartsWith("/hidesource=") Then
  53.                 hidesource = arg.Replace("/hidesource=", "")
  54.             End If
  55.             If arg.StartsWith("/showsource=") Then
  56.                 showsource = arg.Replace("/showsource=", "")
  57.             End If
  58.             If arg.StartsWith("/togglesource=") Then
  59.                 togglesource = arg.Replace("/togglesource=", "")
  60.             End If
  61.             If arg.StartsWith("/toggleaudio=") Then
  62.                 toggleaudio = arg.Replace("/toggleaudio=", "")
  63.             End If
  64.             If arg.StartsWith("/mute=") Then
  65.                 mute = arg.Replace("/mute=", "")
  66.             End If
  67.             If arg.StartsWith("/unmute=") Then
  68.                 unmute = arg.Replace("/unmute=", "")
  69.             End If
  70.             If arg.StartsWith("/setvolume=") Then
  71.                 setvolume = arg.Replace("/setvolume=", "")
  72.             End If
  73.             If arg = "/startstream" Then
  74.                 startstream = True
  75.             End If
  76.             If arg = "/stopstream" Then
  77.                 stopstream = True
  78.             End If
  79.             If arg = "/startrecording" Then
  80.                 startrecording = True
  81.             End If
  82.             If arg = "/stoprecording" Then
  83.                 stoprecording = True
  84.             End If
  85.         Next
  86.         Dim myout As TextWriter = Console.Out
  87.         Try
  88.  
  89.             Dim builder As StringBuilder = New StringBuilder()
  90.             Dim writer As TextWriter = New StringWriter(builder)
  91.  
  92.             Console.SetOut(writer)
  93.  
  94.             _obs = New OBSWebsocket()
  95.             _obs.WSTimeout = New TimeSpan(0, 0, 0, 3)
  96.             _obs.Connect(server, password)
  97.  
  98.             If profile <> "" Then
  99.                 _obs.SetCurrentProfile(profile)
  100.             End If
  101.             If scene <> "" Then
  102.                 _obs.SetCurrentScene(scene)
  103.             End If
  104.             If hidesource <> "" Then
  105.                 If hidesource.Contains("/") Then
  106.                     Dim tmp As String() = hidesource.Split("/")
  107.  
  108.                     ' scene/source
  109.                     If tmp.Count = 2 Then _obs.SetSourceRender(tmp(1), False, tmp(0))
  110.                 Else
  111.                     _obs.SetSourceRender(hidesource, False)
  112.                 End If
  113.             End If
  114.             If showsource <> "" Then
  115.                 If showsource.Contains("/") Then
  116.                     Dim tmp As String() = showsource.Split("/")
  117.  
  118.                     ' scene/source
  119.                     If tmp.Count = 2 Then _obs.SetSourceRender(tmp(1), True, tmp(0))
  120.                 Else
  121.                     _obs.SetSourceRender(showsource, True)
  122.                 End If
  123.  
  124.             End If
  125.             If togglesource <> "" Then
  126.                 If togglesource.Contains("/") Then
  127.                     Dim tmp As String() = togglesource.Split("/")
  128.  
  129.                     ' scene/source
  130.                     If tmp.Count = 2 Then OBSToggleSource(tmp(1), tmp(0))
  131.                 Else
  132.                     OBSToggleSource(togglesource)
  133.                 End If
  134.             End If
  135.             If toggleaudio <> "" Then
  136.                 _obs.ToggleMute(toggleaudio)
  137.             End If
  138.             If mute <> "" Then
  139.                 _obs.SetMute(mute, True)
  140.             End If
  141.             If unmute <> "" Then
  142.                 _obs.SetMute(unmute, False)
  143.             End If
  144.             If setvolume <> "" Then
  145.                 ' source/volume,delay
  146.                 Dim tmp As String() = setvolume.Split(",")
  147.                 If Not IsNumeric(tmp(1)) Then Throw New Exception("Volume value is not nummeric (0-100)!")
  148.                 If tmp.Count = 2 Then
  149.                     OBSSetVolume(tmp(0), tmp(1))
  150.                 ElseIf tmp.Count = 3 Then
  151.                     If Not IsNumeric(tmp(2)) Then Throw New Exception("Delay value is not nummeric (0-x)!")
  152.                     OBSSetVolume(tmp(0), tmp(1), tmp(2))
  153.                 End If
  154.             End If
  155.             If startstream = True Then
  156.                 _obs.StartStreaming()
  157.             End If
  158.             If stopstream = True Then
  159.                 _obs.StopStreaming()
  160.             End If
  161.             If startrecording = True Then
  162.                 _obs.StartRecording()
  163.             End If
  164.             If stoprecording = True Then
  165.                 _obs.StopRecording()
  166.             End If
  167.  
  168.             _obs.Disconnect()
  169.             Console.SetOut(myout)
  170.             Console.WriteLine("Ok")
  171.  
  172.         Catch ex As Exception
  173.             Console.SetOut(myout)
  174.             Console.WriteLine("Error: " & ex.Message.ToString())
  175.         End Try
  176.  
  177.  
  178.     End Sub
  179.  
  180.     Private Sub OBSToggleSource(ByVal source As String, Optional ByVal sceneName As String = "")
  181.         Dim _obsScene As OBSScene
  182.         Dim obsSettings As SourceSettings
  183.         If sceneName = "" Then
  184.             _obsScene = _obs.GetCurrentScene()
  185.             sceneName = _obsScene.Name
  186.         End If
  187.  
  188.         obsSettings = _obs.GetSourceSettings(sceneName)
  189.  
  190.         Dim counter As Integer = 0
  191.  
  192.         For Each item As JProperty In obsSettings.sourceSettings
  193.             Debug.WriteLine(item)
  194.             If item.Name = "id_counter" Then
  195.                 counter = item.Value
  196.                 Continue For
  197.             End If
  198.  
  199.             Dim found As Boolean = False
  200.             Dim wrongname As Boolean = False
  201.  
  202.             For Each child As JToken In item.Children.Children
  203.                 For Each _item As JProperty In child
  204.                     'Debug.WriteLine(_item.ToString())
  205.                     If _item.Name.ToString() = "name" Then
  206.                         If _item.Value.ToString() <> source Then
  207.                             wrongname = True
  208.                         End If
  209.                         found = True
  210.                     End If
  211.                     If wrongname = True Then
  212.                         wrongname = False
  213.                         Exit For
  214.                     End If
  215.                     If found = False Then Continue For
  216.                     If _item.Name.ToString() = "visible" Then
  217.                         If _item.Value.ToString() = "True" Then
  218.                             Debug.WriteLine(sceneName & "/" & source & " is visible")
  219.                             _obs.SetSourceRender(source, False, sceneName)
  220.                         Else
  221.                             Debug.WriteLine(sceneName & "/" & source & " is not visible")
  222.                             _obs.SetSourceRender(source, True, sceneName)
  223.                         End If
  224.                     End If
  225.                 Next
  226.             Next
  227.  
  228.         Next
  229.     End Sub
  230.  
  231.     Private Sub OBSSetVolume(ByVal source As String, ByVal volume As Integer, Optional ByVal delay As Integer = 0)
  232.         If delay = 0 Then
  233.             _obs.SetVolume(source, volume / 100)
  234.         Else
  235.             If delay > 10 Then delay = 10
  236.             If delay > 1000 Then delay = 1000
  237.             Dim _VolumeInfo As VolumeInfo = _obs.GetVolume(source)
  238.             Dim startvolume As Integer = _VolumeInfo.Volume * 100
  239.  
  240.             If startvolume = volume Then
  241.                 Exit Sub
  242.             ElseIf startvolume < volume Then
  243.                 For a = startvolume To volume
  244.                     _obs.SetVolume(source, (a / 100))
  245.                     Threading.Thread.Sleep(delay)
  246.                 Next
  247.             ElseIf startvolume > volume Then
  248.                 For a = startvolume To volume Step -1
  249.                     _obs.SetVolume(source, (a / 100))
  250.                     Threading.Thread.Sleep(delay)
  251.                 Next
  252.             End If
  253.         End If
  254.     End Sub
  255.  
  256.     Private Sub PrintUsage()
  257.         Dim out As List(Of String) = New List(Of String)
  258.  
  259.         out.Add("OBSCommand v1.3 ©2018 by FSC-SOFT (http://www.VoiceMacro.net)")
  260.         out.Add(vbCrLf)
  261.         out.Add("Usage:")
  262.         out.Add("------")
  263.         out.Add("OBSCommand.exe /server=127.0.0.1:4444 /password=xxxx /profile=myprofile /scene=myscene /hidesource=myscene/mysource /showsource=myscene/mysource /toggleaudio=myaudio /mute=myaudio /unmute=myaudio /setvolume=mysource,volume,delay /startstream /stopstream /startrecording /stoprecording")
  264.         out.Add(vbCrLf)
  265.         out.Add("Note: If Server is omitted, default 127.0.0.1:4444 will be used.")
  266.         out.Add("Use quotes if your item name includes spaces.")
  267.         out.Add("Password can be empty if no password is set in OBS Studio.")
  268.         out.Add(vbCrLf)
  269.         out.Add("This tool uses the obs-websocket plugin to talk to OBS Studio:")
  270.         out.Add("https://github.com/Palakis/obs-websocket/releases")
  271.         out.Add(vbCrLf)
  272.         out.Add("Dynamic link libraries used:")
  273.         out.Add("Json.NET ©2008 by James Newton-King")
  274.         out.Add("websocket-sharp ©2010-2016 by sta.blockhead")
  275.         out.Add("obs-websocket-dotnet ©2017 by Stéphane Lepin.")
  276.         out.Add(vbCrLf)
  277.         out.Add("Examples:")
  278.         out.Add("---------")
  279.         out.Add("OBSCommand.exe /scene=myscene")
  280.         out.Add("OBSCommand.exe /toggleaudio=""Desktop Audio""")
  281.         out.Add("OBSCommand.exe /mute=myAudioSource")
  282.         out.Add("OBSCommand.exe /unmute=""my Audio Source""")
  283.         out.Add("OBSCommand.exe /setvolume=Mic/Aux,0,50")
  284.         out.Add("OBSCommand.exe /setvolume=Mic/Aux,100")
  285.         out.Add("OBSCommand.exe /stopstream")
  286.         out.Add("OBSCommand.exe /profile=myprofile /scene=myscene /showsource=mysource")
  287.         out.Add("OBSCommand.exe /showsource=mysource")
  288.         out.Add("OBSCommand.exe /hidesource=myscene/mysource")
  289.         out.Add("OBSCommand.exe /showsource=""my scene""/""my source""")
  290.         out.Add(vbCrLf)
  291.         out.Add("Options:")
  292.         out.Add("--------")
  293.         out.Add("/server=127.0.0.1:4444            define server address and port")
  294.         out.Add("  Note: If Server is omitted, default 127.0.0.1:4444 will be used.")
  295.         out.Add("/password=xxxx                    define password (can be omitted)")
  296.         out.Add("/profile=myprofile                switch to profile ""myprofile""")
  297.         out.Add("/scene=myscene                    switch to scene ""myscene""")
  298.         out.Add("/hidesource=myscene/mysource      hide source ""scene/mysource""")
  299.         out.Add("/showsource=myscene/mysource      show source ""scene/mysource""")
  300.         out.Add("  Note:  if scene is omitted, current scene is used")
  301.         out.Add("/toggleaudio=myaudio              toggle mute from audio source ""myaudio""")
  302.         out.Add("/mute=myaudio                     mute audio source ""myaudio""")
  303.         out.Add("/unmute=myaudio                   unmute audio source ""myaudio""")
  304.         out.Add("/setvolume=myaudio,volume,delay   set volume of audio source ""myaudio""")
  305.         out.Add("                                  volume is 0-100, delay is in milliseconds")
  306.         out.Add("                                  between steps (min. 10, max. 1000) for fading")
  307.         out.Add("  Note:  if delay is omitted volume is set instant")
  308.         out.Add("/startstream                      starts streaming")
  309.         out.Add("/stopstream                       stop streaming")
  310.         out.Add("/startrecording                   starts recording")
  311.         out.Add("/stoprecording                    stops recording")
  312.         out.Add("")
  313.  
  314.         Dim i As Integer = 0
  315.         Dim z As Integer = 0
  316.  
  317.         Do While True
  318.  
  319.             Console.WriteLine(out(i))
  320.             If z = Console.WindowHeight - 6 Then
  321.                 Console.Write("Press any key to continue...")
  322.                 Console.ReadKey()
  323.                 ClearCurrentConsoleLine()
  324.                 z = 0
  325.             End If
  326.             i += 1
  327.             z += 1
  328.             If i >= out.Count Then Exit Do
  329.  
  330.             If out(i).Length > Console.WindowWidth Then
  331.                 z += 1
  332.             End If
  333.             If out(i).Length > Console.WindowWidth * 2 Then
  334.                 z += 1
  335.             End If
  336.         Loop
  337.  
  338.     End Sub
  339.  
  340.  
  341.     Public Sub ClearCurrentConsoleLine()
  342.         Dim currentLineCursor As Integer = Console.CursorTop
  343.         Console.SetCursorPosition(0, Console.CursorTop)
  344.         Console.Write(New String(" "c, Console.WindowWidth))
  345.         Console.SetCursorPosition(0, currentLineCursor)
  346.     End Sub
  347. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement