BlowfishCMS
A small content management system for OpenBSD
version 0.45.6 · ISC · 2026-12-03 · releases
101 lines · 6.0 KB · download this file
# live-upload: takes OBS's stream on this computer and sends it, piece by
# piece, to your site's Live add-on. Run it in PowerShell, leave it running,
# and use Start Streaming in OBS (Stream settings: Service Custom, Server
# rtmp://127.0.0.1:1935/live, any stream key):
#
# powershell -ExecutionPolicy Bypass -File live-upload.ps1 -Site https://example.org
#
# It needs FFmpeg, installed once with: winget install Gyan.FFmpeg
# It asks for the stream key (made on the Live add-on's Stream screen) and
# keeps it only while it runs. Each time OBS stops streaming, the stream comes
# off the site and live-upload waits for the next. Ctrl+C stops it.
param(
[Parameter(Mandatory = $true)][string]$Site,
[int]$Port = 1935
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue
if (-not $ffmpeg) { Write-Host 'live-upload needs FFmpeg. Install it once with: winget install Gyan.FFmpeg then open a new PowerShell window.'; exit 1 }
$In = $Site.TrimEnd('/') + '/x/live/in/'
$secure = Read-Host -Prompt 'Stream key' -AsSecureString
$Key = [Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)).Trim()
$Headers = @{ 'X-Stream-Key' = $Key }
$Work = Join-Path ([IO.Path]::GetTempPath()) 'live-upload'
function Send([string]$name, $file) {
try {
Invoke-WebRequest -Uri ($In + $name) -Method Put -InFile $file -Headers $Headers -ContentType 'application/octet-stream' -UseBasicParsing | Out-Null
return $true
} catch {
# The site's own words when it answered (PowerShell 5 and 7 alike), else what went wrong.
$msg = if ($_.ErrorDetails -and $_.ErrorDetails.Message) { $_.ErrorDetails.Message.Trim() } else { $_.Exception.Message }
Write-Host " $name wasn't sent: $msg"
if ($msg -match 'stream key') { throw 'Stopped: check the stream key on the Live add-on''s Stream screen.' }
return $false
}
}
function Off {
try { Invoke-WebRequest -Uri ($In + 'live.m3u8') -Method Delete -Headers $Headers -UseBasicParsing | Out-Null; Write-Host 'The stream is off the site.' } catch { }
}
$proc = $null; $live = $false
try {
while ($true) {
# FFmpeg waits for OBS, cuts its stream into 2-second pieces as it comes, and ends when OBS stops.
if (Test-Path -LiteralPath $Work) { Remove-Item -LiteralPath $Work -Recurse -Force }
New-Item -ItemType Directory -Path $Work | Out-Null
$ffArgs = @('-hide_banner', '-loglevel', 'error', '-listen', '1', '-i', "rtmp://127.0.0.1:$Port/live/obs",
'-c', 'copy', '-f', 'hls', '-hls_time', '2', '-hls_list_size', '6',
'-hls_flags', 'delete_segments+independent_segments+temp_file', '-hls_segment_type', 'fmp4',
'-hls_fmp4_init_filename', 'init.mp4', '-hls_segment_filename', 'seg%d.m4s', 'live.m3u8')
# FFmpeg's own messages (it ends with an error each time OBS stops) go to a log, shown only if it never streamed.
$log = Join-Path ([IO.Path]::GetTempPath()) 'live-upload-ffmpeg.log'
$proc = Start-Process -FilePath $ffmpeg.Source -ArgumentList $ffArgs -WorkingDirectory $Work -NoNewWindow -PassThru -RedirectStandardError $log
Write-Host "Waiting for OBS: Start Streaming (to rtmp://127.0.0.1:$Port/live). Ctrl+C stops."
$sent = @{}; $lastList = ''; $fails = 0
while (-not $proc.HasExited) {
try {
$list = Join-Path $Work 'live.m3u8'
if (Test-Path -LiteralPath $list) {
$text = [IO.File]::ReadAllText($list)
$names = @()
foreach ($m in [regex]::Matches($text, 'URI="([^"]+)"')) { $names += $m.Groups[1].Value }
foreach ($line in ($text -split "`r?`n")) { if ($line -and -not $line.StartsWith('#')) { $names += $line.Trim() } }
$ok = $true; $fail = $false
foreach ($n in $names) {
$f = Get-Item -LiteralPath (Join-Path $Work $n) -ErrorAction SilentlyContinue
if (-not $f) { $ok = $false; continue }
$id = "$($f.Length):$($f.LastWriteTimeUtc.Ticks)"
# The first refusal ends the round: the rest wait for the next try.
if ($sent[$n] -ne $id) { if (Send $n $f.FullName) { $sent[$n] = $id } else { $ok = $false; $fail = $true; break } }
}
if ($ok -and $text -ne $lastList) {
if (Send 'live.m3u8' $list) { if (-not $live) { Write-Host 'Live.' }; $live = $true; $lastList = $text } else { $fail = $true }
}
# When the site refuses or can't be reached, try less and less often (up to every 30 seconds), never in a tight loop.
if (-not $fail) { $fails = 0 } else {
$fails++
if ($fails -eq 5) { Write-Host 'The site keeps refusing pieces: trying less often. See the messages above.' }
Start-Sleep -Seconds ([Math]::Min(30, [Math]::Pow(2, [Math]::Min($fails, 5))))
}
}
} catch {
# A wrong key stops it; anything else (a piece FFmpeg just removed, a dropped connection) is tried again.
if ($_.Exception.Message -like 'Stopped:*') { throw }
Write-Host " $($_.Exception.Message)"
}
Start-Sleep -Milliseconds 500
}
if ($live) { Off; $live = $false }
if ($proc.ExitCode -ne 0 -and -not $lastList) {
Write-Host "FFmpeg stopped: $((Get-Content -LiteralPath $log -Tail 2 -ErrorAction SilentlyContinue) -join ' ')"
Write-Host "Is something else using port $Port? Another live-upload, or a streaming program?"
Start-Sleep -Seconds 3
}
}
} finally {
if ($proc -and -not $proc.HasExited) { Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue }
if ($live) { Off }
}