Files
live/start.ps1
adminandClaude Opus 4.8 4ee945ca0d feat: encode on the GPU and restart individual renditions
Hardware encoding
- Detect the best usable encoder at startup by encoding a test frame with each
  candidate in turn (nvenc, qsv, amf, then libx264). Being compiled into ffmpeg
  is not enough: h264_amf fails without amfrt64.dll and h264_qsv fails without an
  Intel iGPU, so the probe is the only reliable answer.
- Translate the quality target into the chosen encoder's own vocabulary. libx264's
  -crf and "ultrafast" mean nothing to h264_nvenc, which takes -cq and p1..p7, so
  passing the configured values straight through would have been silently wrong.
- On the reference GTX 1060 a 1080p->720p rendition costs ~62% of a core on NVENC
  versus ~109% on libx264 ultrafast.

Per-rendition supervision
- Track encoder processes per rendition, not per stream. is_running answered
  "any alive", so when the source ("原画") encoder died while 1080p and 720p kept
  running, the supervisor saw a healthy stream and never restarted it — the
  source URL then 404'd permanently. It now requires every rendition to be alive
  and reports which ones are down.
- Restart only the dead renditions, so a single failure no longer interrupts
  viewers on the renditions that were fine.

Production serving
- Serve the built frontend from FastAPI with SPA fallback, so production is one
  process and one port with no Node runtime. Node becomes a build-time dependency.
- start.ps1/start.sh build the frontend on first run instead of failing to a 503.

Cleanup
- Drop the duplicated top-left LIVE badge; the control strip already carries it.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-09-27 18:56:21 +08:00

71 lines
2.7 KiB
PowerShell

param(
[switch]$InstallDeps,
[switch]$BuildFrontend,
[string]$HostAddress = "0.0.0.0",
[int]$BackendPort = 8080
)
$ErrorActionPreference = "Stop"
$Base = Split-Path -Parent $MyInvocation.MyCommand.Path
$BackendDir = Join-Path $Base "backend"
$RuntimeDir = Join-Path $Base "runtime"
$HlsDir = Join-Path $RuntimeDir "hls"
$ReplayDir = Join-Path $RuntimeDir "replay"
$LogDir = Join-Path $RuntimeDir "logs"
New-Item -ItemType Directory -Force -Path $RuntimeDir, $HlsDir, $ReplayDir, $LogDir | Out-Null
if (-not $env:LS_HLS_RUNTIME_DIR) { $env:LS_HLS_RUNTIME_DIR = $HlsDir }
if (-not $env:LS_REPLAY_MEDIA_DIR) { $env:LS_REPLAY_MEDIA_DIR = $ReplayDir }
function Resolve-Python {
$candidates = @(
(Join-Path $BackendDir ".venv\Scripts\python.exe"),
(Join-Path $BackendDir "venv\Scripts\python.exe"),
"python",
"py"
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) { return $candidate }
$cmd = Get-Command $candidate -ErrorAction SilentlyContinue
if ($cmd) { return $cmd.Source }
}
throw "Python not found. Install Python 3.11+ or create backend\.venv."
}
$Python = Resolve-Python
if ($InstallDeps) {
Write-Host "[StreamCore] Installing backend dependencies..."
Push-Location $BackendDir
& $Python -m pip install -r requirements.txt
Pop-Location
}
# The frontend is built once and then served by the backend, so Node is only
# needed here — production never runs npm. Done automatically on a first start so
# a fresh checkout does not come up as a 503 with no obvious cause.
$FrontendDir = Join-Path $Base "frontend"
$DistDir = Join-Path $FrontendDir "dist"
if ($BuildFrontend -or -not (Test-Path (Join-Path $DistDir "index.html"))) {
if (Get-Command "npm" -ErrorAction SilentlyContinue) {
Write-Host "[StreamCore] Building frontend..."
Push-Location $FrontendDir
if (-not (Test-Path "node_modules")) { npm install }
npm run build
Pop-Location
} else {
Write-Warning "npm not found and frontend\dist is missing; the site will return 503."
Write-Warning "Install Node.js and run: cd frontend; npm install; npm run build"
}
}
# MediaMTX, the transcoders and the HLS tree are managed by the backend itself:
# it starts the media origin at boot, supervises ffmpeg, and restarts either if it
# dies. This launcher only has to bring up the process that owns all of that.
Write-Host "[StreamCore] Starting backend on ${HostAddress}:${BackendPort} ..."
Write-Host "[StreamCore] Frontend, MediaMTX, transcoders and HLS are served by the backend."
$backendArgs = @("-m", "uvicorn", "main:app", "--host", $HostAddress, "--port", "$BackendPort")
& $Python @backendArgs