Giter Site home page Giter Site logo

startautomating / obs-powershell Goto Github PK

View Code? Open in Web Editor NEW
49.0 4.0 4.0 3.04 MB

Script OBS with PowerShell

Home Page: https://obs-powershell.start-automating.com

License: MIT License

PowerShell 100.00%
obs openbroadcaster openbroadcastersoftware powershell powershell-module obs-studio obs-websocket websocket websocket-client

obs-powershell's Introduction

obs-powershell is a PowerShell module that lets you script Open Broadcast Studio.

Getting Started

Installing and importing

obs-powershell is available on the PowerShell gallery, so you can use these two simple lines to install / import

Install-Module obs-powershell -Scope CurrentUser -Force
Import-Module obs-powershell -PassThru -Force

Getting Connected

Before you can use obs-powershell, you'll need to Connect-OBS.

You should only need to do this once: obs-powershell will cache this information.

You can find your WebSocketToken in obs studio in Tools -> WebSocket Server Settings -> Show Connect Info.

Copy the Server Password and set it into a variable, $myToken

Connect-OBS -WebSocketToken $myToken

After you've done this once, obs-powershell will attempt to connect every time the module is loaded.

A Quick Example

Once you're connected, check out this nifty short sample of what you can do:

# Show-OBS lets you show all sorts of things.
# It will return a scene item.
$Stars = Show-OBS -Uri "https://pssvg.start-automating.com/Examples/Stars.svg"
Start-Sleep -Milliseconds 50
# We can .Hide/.Disable scene items
$Stars.Hide()
Start-Sleep -Milliseconds 50
# We can .Show/.Enable scene items
$Stars.Show()
Start-Sleep -Milliseconds 50
# We can make an item small
$Stars.Scale(0.1)
Start-Sleep -Milliseconds 50
# We can fit it to the screen
$stars.FitToScreen()
Start-Sleep -Milliseconds 50
# and we can make it big again, with an animation
$Stars.Scale("1%","100%","00:00:01")
Start-Sleep -Seconds 1

# We can do even more broad animations, like moving things across the screen.
$Stars.Animate(@{
    X = "-25%"
    Y = "50%"
    Scale = "20%"
}, @{
    X = "125%"
    Y = "50%"
    Scale = "50%"
    Rotation = 180
}, "00:00:05")
Start-Sleep -Seconds 1

# To see what any object can do in obs-powershell, run Get-Member.
$stars | Get-Member

OBS-PowerShell Effects

obs-powershell gives you the ability to store and create effects that run over time.

For instance, we can FadeIn our stars

# Start the FadeIn effect on 'Stars.svg'
$Stars | Start-OBSEffect -EffectName "FadeIn"
Start-sleep -seconds 1

# Start the FadeOut effect on 'Stars.svg'
$Stars | Start-OBSEffect -EffectName "FadeOut"
Start-sleep -seconds 1

# Start the Colorloop effect on 'Stars.svg'
$Stars | Start-OBSEffect -EffectName "ColorLoop"
Start-sleep -seconds 1

# You can get all loaded effects with Get-OBSEffect
Get-OBSEffect

Examples

Getting all scenes

Get-OBSScene

Getting all inputs

Get-OBSInput

Getting OBS Stats

Get-OBSStats

Getting all kinds of available inputs

Get-OBSInputKind

Getting all monitors

Get-OBSMonitor

Getting Recording Status

Get-OBSRecordStatus

Starting Recording

Start-Recording # an alias to Start-OBSRecord 

Stopping Recording

Stop-Recording # an alias to Stop-OBSRecord

Start Recording, Wait 5 seconds, Stop Recording, and Play the Recording.

Start-OBSRecord

Start-Sleep -Seconds 5

Stop-OBSRecord |
    Invoke-Item

Enabling all sources in all scenes

Get-OBSScene |
    Select-Object -ExpandProperty Scene
    Get-OBSSceneItem |
    Set-OBSSceneItemEnabled -sceneItemEnabled

Disabling all sources in all scenes

Get-OBSScene |
    Select-Object -ExpandProperty Scene
    Get-OBSSceneItem |
    Set-OBSSceneItemEnabled -sceneItemEnabled:$false

How it Works

obs-powershell communicates with OBS with the obs websocket.

obs-powershell has a command for every websocket request.

Because the obs-websocket cleanly documents it's protocol, most commands in obs-powershell are automatically generated with PipeScript.

Depdendencies

To use obs-powershell, you'll need OBS and PowerShell Core. You can run both of these on any operating system.

Specific Command Requirements

Some commands in obs-powershell will not work unless you have additional software installed.

Commands Dependency
Add/Set-OBSVLCSource VLC
Add/Set-OBS3DFilter VLC
Add/Set-OBSShaderFilter obs-shaderfilter 2.0+

obs-powershell's People

Contributors

i-am-jakoby avatar nyanhp avatar startautomating avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

obs-powershell's Issues

Get-OBSInput should be formatted

The return currently contains two effectively duplicated columns: inputKind and unversionedInputKind.

It should only display one of these.

Integrate with devices

While there may be no shortage of built-in controls for obs for StreamDeck and LoupeDeck, these are often limited, and the fine grain control of scripting is always good to surface at the click of a button.

Add-OBSDisplaySource

In order to make screen capture a bit easier, Add-OBSDisplaySource should exist.

This should:

  • Inherit from Add-OBSInput (hiding inputKind, inputName, and sceneName)
  • Always use -inputKind monitor_source.
  • Allow -Name (alias 'InputName') to name the input
  • Default -Name to the -Monitor number
  • Allow -Scene (alias 'SceneName') to set the scene
  • Default -Scene to the CurrentProgramSceneName

Add-OBSBrowserSource

In order to make browser sources easier to create, there should be Add-OBSBrowserSource.

This should:

  • Inherit from Add-OBSInput (hiding inputKind, inputName, and sceneName)
  • New Parameters
    • -URI/-URL/-FilePath/-FullName
    • -Width
    • -Height
    • -CSS
    • -ShutdownWhenHidden
    • -RerouteAudio
    • -RestartWhenActive
    • -FramesPerSecond/-FPS
  • Default -Width/-Height to video outputWidth/outputHeight
  • Set .url / .filepath / .is_local_file based off of -Uri's contents
  • Always use -inputKind browser_source.
  • Allow -Name (alias 'InputName') to name the input
  • Default -Name to the last segment of the URI
  • Allow -Scene (alias 'SceneName') to set the scene
  • Default -Scene to the CurrentProgramSceneName

obs-powershell generated commands must display errors

The obs-websocket returns errors slightly differently than it does normal responses see their specification for more details.

As such, obs-powershell 0.1 did not yet handle the display of errors.

There are two parts to fixing this:

  1. Connect-OBS and the obs watcher job it creates should forward comments field when requestStatus.result -eq $false
  2. Each command's expecting response code should slightly differ, and Write-Error with the response message.

Send-OBS: Better Batch Processing

When more than one message is piped to Send-OBS, it will try to send them as a batch.

This currently will not quite work, due to case-sensitivity of the payload.

Which brings up point one of improvements to batch processing:

  • Fix case-sensitivity of batched requests

Additionally, Send-OBS should expose parameters that grant control of the batch.

  • haltOnFailure should be exposed as a parameter in some form.
  • executionType should be exposed as a parameter in some form.

Finally, because batch requests with small sleeps can be used for smooth manipulation, we want to:

  • Add a parameter to sleep between each batch (not in PowerShell, in obs)

Connect-OBS

Before getting too far ahead of ourselves, we need to be able to connect to the OBS websocket.

Connect-OBS is the core command responsible for connecting to an OBS websocket.

Disconnect-OBS

We want to be able to cleanly disconnect from OBS when the module unloads (or whenever we decide to).

Therefore, we need Disconnect-OBS.

Receive-OBS

To simplify each generated command, Receive-OBS should exist.

Receive-OBS should inspect a message payload and receive results

OBS Scene Items should be able to FitToScreen()

Though this should do this slightly differently than in the UI.

In the UI, when things are fit to the screen, they are still transformed from the upper left. We should transform from Center.

Attempt Action Creation

If obs can be invoked within a GitHub action, this opens a lot of doors for automated video processing and streaming within a build.

Clear-OBSScene

One should be able to clear all of the items from a given scene.

One should prompt for confirmation before doing this.

Watch-OBS

To assist #18 , the core logic for watching OBS should be put into a distinct function.

Add-OBSMediaSource

This should:

  • Inherit from Add-OBSInput (hiding inputKind, inputName, and sceneName)
  • Always use -inputKind ffmpeg_source.
  • New Parameters
    • -FilePath/-Fullname/-LocalFile/-Local_file
    • -CloseWhenInactive
    • -Loop
    • -UseHardwareDecoding
    • -ClearOnMediaEnd
    • -FFMpegOption/-FFMpegOptions/-FFMpeg_Options
  • Allow -Name (alias 'InputName') to name the input
  • Default -Name to the -FilePath's name
  • Allow -Scene (alias 'SceneName') to set the scene
  • Default -Scene to the CurrentProgramSceneName

Rasterizing SVGs with obs-powershell

SVG is an amazing graphics format and a great web standard.

Unfortunately, it's also a standard that's not fully implemented by many imaging tools.

If, for instance, you want to rasterize an SVG with ImageMagik, go ahead and kiss your symbols goodbye and forget even using CSS classes. Many imaging apps can't handle large chunks of the SVG standard, which leads to omitted text and elements.

And don't even get me started on animated SVGs.

Luckily, OBS has a browser source, and lets us easily capture an SVG as an image or a video exactly as it would render in Chrome.

Let's cover images first.

Because SVGs are scalable, and raster images are not, step one is to get our dimensions right.

SVGs have a Viewbox, which contains the width and height they'll want to render at.

Create a browser source of that width / height, then pipe it to Save-OBSScreen.

Add-OBSBrowserSource -Uri https://4bitcss.com/Batman.svg -Width 640 -Height 240 |
    Save-OBSSourceScreenshot -ImageFormat png -ImageFilePath (Join-Path $pwd "Batman.png")

Because I'm Batman

Because of the Object Pipeline in PowerShell, we don't even need to pass along the -ImageHeight and -ImageWidth to Save-OBSSourceScreenshot (the browser source has an .ImageHeight and .ImageWidth property).

It gets even better.

The file that Save-OBSSourceScreenshot returns packs a little bit of extra info:

  • What source generated it
  • What scene it was in

Which means we can actually just take a snapshot and remove it:

Add-OBSBrowserSource -Uri https://4bitcss.com/Batman.svg -Width 640 -Height 240 |
    Save-OBSSourceScreenshot -ImageFormat png -ImageFilePath (Join-Path $pwd "Batman.png") |
    Remove-OBSSceneItem

Hope this Helps!

Send-OBS

In order to simplify the implementation of each autogenerated command, and to make it easier to work with the OBS WebSocket, there should be a high-level function, Send-OBS.

Send-OBS should accept any object from the pipeline.

  • If only one object was provided, it should be sent to OBS
  • If the object lacks an opcode, it should be treated as a broadcast event
  • If the object has authentication, it should be made
  • If a single object has requestData, it should be made into the correct payload for an obs request
  • If multiple objects were passed and each had request data, they should be made into a batch request
  • If multiple objects were passed and did not each have a request data, it should be sent as a series of single objects

Receive-OBS should decorate typename with .InputKind when found

Various changes can only be made to particular kinds of input. Therefore, whenever Receive-OBS receives a message with an .InputKind property, that .InputKind should be added to the typenames.

This will allow us to make specific script methods and properties for this kind of input.

Generate more "Watch-" functions

There are many events sent by the obs-websocket, and, while those can currently be cause by Register-EngineEvent, the average PowerShell user is unlikely to know about Register-EngineEvent without prompting.

Therefore, there should be a Watch- function for every event surfaced within obs.

Add-OBSSceneSource

In order to make scene sources a smidgeon easier, we should be able to Add-OBSSceneSource.

This can be accomplished via aliasing Add-OBSSceneItem.

Command parameter names should be capitalized

This it the standard for most PowerShell commands, and we have already abstracted the parameter name away from the "bound" property, using [ComponentModel.DefaultBindingProperty("realName")].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.