1+ function Show-OBS {
2+ <#
3+ . SYNOPSIS
4+ Shows content in OBS
5+ . DESCRIPTION
6+ Shows content in Open Broadcasting Studio
7+ . EXAMPLE
8+ '<svg viewBox="0 0 1 1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
9+ <polygon points="0 0 0 1 1 1 1 0" fill="blue" />
10+ </svg>' | Set-Content .\BlueRect.svg
11+ Show-OBS -FilePath .\BlueRect.svg
12+ . EXAMPLE
13+ Show-OBS -FilePath *excited* -RootPath $home\Pictures\Gif
14+ #>
15+ param (
16+ # The path or URI to show in OBS.
17+ [Parameter (Mandatory , ValueFromPipelineByPropertyName )]
18+ [Alias (' FullName' , ' Src' , ' Uri' , ' FileName' )]
19+ [string ]
20+ $FilePath ,
21+
22+ # The name of the source in OBS.
23+ # If this is not provided, it will be derived from the -FilePath.
24+ [Parameter (ValueFromPipelineByPropertyName )]
25+ [string ]
26+ $Name ,
27+
28+ # A root path.
29+ # If not provided, this will be the root of the -FilePath (if it is a filepath).
30+ # If the file path was a URI, the root path will be ignored.
31+ [Parameter (ValueFromPipelineByPropertyName )]
32+ [string ]
33+ $RootPath ,
34+
35+ # The name of the scene.
36+ # If no scene name is provided, the current program scene will be used.
37+ [Parameter (ValueFromPipelineByPropertyName )]
38+ [string ]
39+ $Scene ,
40+
41+ # The opacity to use for the input.
42+ # If not provided, will default to 2/3rds.
43+ # Will only be used when showing a browser source with a -FilePath
44+ [Parameter (ValueFromPipelineByPropertyName )]
45+ [double ]
46+ $Opacity = (2 / 3 ),
47+
48+ # Any parameters to pass to the source command.
49+ [Parameter (ValueFromPipelineByPropertyName )]
50+ [Collections.IDictionary ]
51+ $SourceParameter = [ordered ]@ {},
52+
53+ # If set, will check if the source exists in the scene before creating it and removing any existing sources found.
54+ # If not set, you will get an error if a source with the same name exists.
55+ [Parameter (ValueFromPipelineByPropertyName )]
56+ [switch ]
57+ $Force ,
58+
59+ # If set, will make the input become the size of the screen.
60+ [Parameter (ValueFromPipelineByPropertyName )]
61+ [switch ]
62+ $FitToScreen
63+ )
64+
65+ begin {
66+ filter FitToScreenAndOutput {
67+ if ($FitToScreen -and $_.FitToScreen ) {
68+ $_.FitToScreen ()
69+ }
70+ $_
71+ }
72+ }
73+
74+ process {
75+ # If we had a -RootPath
76+ if ($RootPath ) {
77+ # Look in the root path
78+ $imageFiles = @ (Get-ChildItem $RootPath - Recurse - File|
79+ # For files like this keyword
80+ Where-Object FullName -like " *$filePath *" |
81+ # that are extensions we could show
82+ Where-Object Extension -in ' .html' , ' .jpg' , ' .jpeg' , ' .gif' , ' .apng' , ' .png'
83+ )
84+ if ($imageFiles.Count ) {
85+ $FilePath = ($imageFiles | Get-Random ).FullName
86+ }
87+ }
88+
89+
90+
91+ # Determine if the thing we are showing will be a ffmpeg source.
92+ $IsMediaSource =
93+ # If it's an http path, it's not
94+ if ($FilePath -like ' http*' ) {
95+ $false
96+ } elseif (
97+ # If it's an HTML-friendly path, it's not
98+ $FilePath -match ' .(?>html?|gif|jpe?g|a?png|svg)$'
99+ ) {
100+ $false
101+ } else {
102+ # Otherwise, let's give it a try.
103+ $true
104+ }
105+
106+ if (-not $RootPath -and $filePath -notlike ' http*' ) {
107+ $RootPath = " $ ( $FilePath | Split-Path ) "
108+ }
109+
110+ # If we provided a scene
111+ if ($Scene ) {
112+ # pass it down.
113+ $SourceParameter.Scene = $Scene
114+ }
115+
116+ # If we want to use -Force
117+ if ($Force ) {
118+ # pass it down
119+ $SourceParameter.Force = $Force
120+ }
121+
122+ # If we do not want to create a media source,
123+ if (-not $IsMediaSource ) {
124+ # we create a browser source.
125+ $SourceParameter.Uri = $FilePath
126+ # If the path was not already HTML,
127+ if ($RootPath -and $FilePath -notmatch ' \.html{0,1}$' ) {
128+ # we make a minimal frame in a .html file
129+ $relativePath = $FilePath.Substring ($RootPath.Length + 1 )
130+ $htmlFrame = " <html>
131+ <body style='width:100%;height:100%'>
132+ <img src='$relativePath ' style='width:100%;height:100%' />'
133+ <body>
134+ </html>"
135+
136+ $leafPath = $filePath | Split-Path - Leaf
137+ $htmlPath = Join-Path $RootPath " $ ( $leafPath ) .html"
138+
139+ $htmlFrame | Set-Content - Path $htmlPath
140+ # And set up the CSS for that frame, passing down -Opacity.
141+ # (this may not work for all images)
142+ $css = "
143+ body {
144+ background-color: rgba(0, 0, 0, 0); margin: 0px auto; overflow: hidden;
145+ }
146+ img {
147+ width: 100%
148+ height: 100%;
149+ opacity: $opacity ;
150+ }
151+ "
152+ $SourceParameter.Uri = $htmlPath
153+ $SourceParameter.CSS = $css
154+ Add-OBSBrowserSource @SourceParameter | FitToScreenAndOutput
155+ } else {
156+ Add-OBSBrowserSource @SourceParameter | FitToScreenAndOutput
157+ }
158+
159+ } else {
160+ $SourceParameter.FilePath = $FilePath
161+ Add-OBSMediaSource @SourceParameter | FitToScreenAndOutput
162+ }
163+ }
164+ }
0 commit comments