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+
60+ process {
61+ # If we had a -RootPath
62+ if ($RootPath ) {
63+ # Look in the root path
64+ $imageFiles = @ (Get-ChildItem $RootPath - Recurse - File|
65+ # For files like this keyword
66+ Where-Object FullName -like " *$filePath *" |
67+ # that are extensions we could show
68+ Where-Object Extension -in ' .html' , ' .jpg' , ' .jpeg' , ' .gif' , ' .apng' , ' .png'
69+ )
70+ if ($imageFiles.Count ) {
71+ $FilePath = ($imageFiles | Get-Random ).FullName
72+ }
73+ }
74+
75+
76+
77+ # Determine if the thing we are showing will be a ffmpeg source.
78+ $IsMediaSource =
79+ # If it's an http path, it's not
80+ if ($FilePath -like ' http*' ) {
81+ $false
82+ } elseif (
83+ # If it's an HTML-friendly path, it's not
84+ $FilePath -match ' .(?>html?|gif|jpe?g|a?png|svg)$'
85+ ) {
86+ $false
87+ } else {
88+ # Otherwise, let's give it a try.
89+ $true
90+ }
91+
92+ if (-not $RootPath -and $filePath -notlike ' http*' ) {
93+ $RootPath = " $ ( $FilePath | Split-Path ) "
94+ }
95+
96+ # If we provided a scene
97+ if ($Scene ) {
98+ # pass it down.
99+ $SourceParameter.Scene = $Scene
100+ }
101+
102+ # If we want to use -Force
103+ if ($Force ) {
104+ # pass it down
105+ $SourceParameter.Force = $Force
106+ }
107+
108+ if (-not $IsMediaSource ) {
109+ # Create a browser source
110+ $SourceParameter.Uri = $FilePath
111+ if ($RootPath -and $FilePath -notmatch ' \.html{0,1}$' ) {
112+ # Make a minimal frame in a .html file
113+ $relativePath = $FilePath.Substring ($RootPath.Length + 1 )
114+ $htmlFrame = " <html>
115+ <body style='width:100%;height:100%'>
116+ <img src='$relativePath ' style='width:100%;height:100%' />'
117+ <body>
118+ </html>"
119+
120+ $leafPath = $filePath | Split-Path - Leaf
121+ $htmlPath = Join-Path $RootPath " $ ( $leafPath ) .html"
122+
123+ $htmlFrame | Set-Content - Path $htmlPath
124+ # And set up the CSS for that frame.
125+ $css = "
126+ body {
127+ background-color: rgba(0, 0, 0, 0); margin: 0px auto; overflow: hidden;
128+ }
129+ img {
130+ width: 100%
131+ height: 100%;
132+ opacity: $opacity ;
133+ }
134+ "
135+ $SourceParameter.Uri = $htmlPath
136+ $SourceParameter.CSS = $css
137+ Add-OBSBrowserSource @SourceParameter
138+ } else {
139+ Add-OBSBrowserSource @SourceParameter
140+ }
141+
142+ } else {
143+ $SourceParameter.FilePath = $FilePath
144+ Add-OBSMediaSource @SourceParameter
145+ }
146+ }
147+ }
0 commit comments