Files: re-encode a video the browser can't play, on the machine's own GPU, and play that

The viewer tries the file as it is first. When the browser gives up — Safari and an MKV,
HEVC without a hardware decoder — or on a tap, a picker: codec (H.264, HEVC, AV1, or the
original codec repackaged) and size (original, 1080p, 720p, 480p), each codec labelled
with the engine this machine would use. POST /api/files/play runs ffmpeg into an HLS
event playlist of fragmented-MP4 segments under <data dir>/transcode/<id>/; Safari plays
HLS itself and everything else goes through hls.js, loaded only then. The recipes are
tried best first: a card on the bus before the integrated GPU, decoding on the same card
before decoding on the CPU and uploading, the software encoders last; a recipe that
produces no first segment is dropped for the next. The seekable range grows as ffmpeg
gets ahead; status says how far. Streams are reused for the same request, closed when
the player goes, swept after ten idle minutes, and the folder is emptied at start. Root
mode runs ffmpeg as root through a helper that ends when the daemon lets go of it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Mitchell-Johnson
2026-10-11 17:42:25 -07:00
co-authored by Claude Fable 5.1
parent aa4ae57c22
commit f3c547adfa
18 changed files with 1351 additions and 32 deletions
+141
View File
@@ -0,0 +1,141 @@
// A video in the Files viewer. First the file as it is, handed to the browser's own
// decoder; when that fails — Safari and an MKV, HEVC without a hardware decoder — a
// picker: codec and size, and the machine re-encodes it to HLS on its own GPU (or CPU),
// which plays natively on Safari and through hls.js everywhere else. The same picker is
// one tap away even when the file plays, for a 36 Mbps capture over a slow link.
import { useCallback, useEffect, useRef, useState } from 'react'
import { api, apiPath, PlayCaps, PlayStatus, PlayStream } from './api'
import { Btn, Spinner } from './ui'
type Mode = 'native' | 'pick' | 'hls'
const CODEC_KEY = 'hamlab.play.codec'
const QUALITY_KEY = 'hamlab.play.quality'
const remember = (k: string, v: string) => { try { localStorage.setItem(k, v) } catch { /* private mode */ } }
const recall = (k: string, d: string) => { try { return localStorage.getItem(k) || d } catch { return d } }
const fmtMbps = (bps?: number) => (bps ? `${(bps / 1e6).toFixed(bps >= 10e6 ? 0 : 1)} Mbps` : '')
const describe = (s: PlayStream) => {
const v = s.source.video
const src = v ? `${v.codec}${v.profile ? ' ' + v.profile : ''} ${v.width}×${v.height}${v.fps ? ` ${Math.round(v.fps)} fps` : ''}${s.source.bit_rate ? ` ${fmtMbps(s.source.bit_rate)}` : ''} in ${s.source.container.split(',')[0]}` : s.source.container
const out = s.codec === 'copy' ? 'repackaged' : `${s.codec === 'h264' ? 'H.264' : s.codec.toUpperCase()} ${s.width}×${s.height} ${(s.kbps / 1000).toFixed(s.kbps >= 10000 ? 0 : 1)} Mbps`
return `${src} → ${out} on ${s.engine}`
}
export default function Player({ path, sudo, name }: { path: string; sudo: boolean; name: string }) {
const [mode, setMode] = useState<Mode>('native')
const [why, setWhy] = useState('') // what brought the picker up
const [caps, setCaps] = useState<PlayCaps | null>(null)
const [codec, setCodec] = useState(() => recall(CODEC_KEY, 'h264'))
const [quality, setQuality] = useState(() => recall(QUALITY_KEY, '1080p'))
const [stream, setStream] = useState<{ s: PlayStream; url: string } | null>(null)
const [status, setStatus] = useState<PlayStatus | null>(null)
const [err, setErr] = useState('')
const video = useRef<HTMLVideoElement>(null)
const streamRef = useRef(stream)
streamRef.current = stream
useEffect(() => { setMode('native'); setWhy(''); setStream(null); setStatus(null); setErr('') }, [path, sudo])
useEffect(() => { remember(CODEC_KEY, codec) }, [codec])
useEffect(() => { remember(QUALITY_KEY, quality) }, [quality])
useEffect(() => { if (mode === 'pick' && !caps) api.playCaps(sudo).then(setCaps).catch((e) => setCaps({ codecs: [], qualities: [], error: e instanceof Error ? e.message : 'failed' })) }, [mode, caps, sudo])
// a stream is the machine's work: let go of it when this player does
const stop = useCallback(() => { const s = streamRef.current; if (s) { api.playStop(s.s.id).catch(() => { /* gone already */ }); setStream(null); setStatus(null) } }, [])
useEffect(() => () => stop(), [stop])
const start = async () => {
setErr('')
try {
stop()
const r = await api.playStart(path, codec, quality, sudo)
setStream({ s: r.stream, url: r.url }); setMode('hls')
} catch (e) { setErr(e instanceof Error ? e.message : 'failed') }
}
// feed the stream to the element: Safari plays HLS itself, the rest through hls.js
useEffect(() => {
const v = video.current
if (mode !== 'hls' || !stream || !v) return
const url = apiPath(stream.url)
let hls: { destroy: () => void } | null = null
let gone = false
;(async () => {
if (v.canPlayType('application/vnd.apple.mpegurl')) {
v.src = url
} else {
const { default: Hls } = await import('hls.js')
if (gone) return
if (!Hls.isSupported()) { setErr('this browser has neither HLS nor MediaSource — nothing can play here'); return }
const h = new Hls({ maxBufferLength: 60, backBufferLength: 30 })
hls = h
h.on(Hls.Events.ERROR, (_e, d) => { if (d.fatal) setErr(`playback failed: ${d.details}`) })
h.loadSource(url)
h.attachMedia(v)
}
v.play().catch(() => { /* needs a tap on some phones */ })
})()
return () => { gone = true; hls?.destroy() }
}, [mode, stream])
// follow the re-encode while it runs
useEffect(() => {
if (mode !== 'hls' || !stream) return
let stopPolling = false
const tick = async () => { try { const st = await api.playStatus(stream.s.id); if (!stopPolling) { setStatus(st); if (st.running) setTimeout(tick, 2000) } } catch { /* ended */ } }
tick()
return () => { stopPolling = true }
}, [mode, stream])
const pick = (reason: string) => { setWhy(reason); setMode('pick') }
const codecs = caps?.codecs ?? []
return (
<div className="player">
{mode === 'native' && (
<>
<div className="play-bar"><span>as it is</span><span className="spacer" /><button className="btn" onClick={() => pick('')}>Re-encode…</button></div>
<div className="preview"><video controls playsInline preload="metadata" src={api.fileReadURL(path, sudo)} onError={() => pick(`This browser can't play ${name} as it is — the container or codec isn't one it decodes.`)} /></div>
</>
)}
{mode === 'pick' && (
<div className="play-pick notice">
<div>{why || `Re-encode ${name} on this machine and play that instead.`}</div>
{caps?.error && <div className="error" style={{ marginTop: 8 }}>{caps.error}</div>}
{!caps && !err && <div className="muted small" style={{ marginTop: 8 }}><Spinner /> asking the machine what it can encode…</div>}
{caps && !caps.error && (
<div className="row">
<label>codec
<select value={codec} onChange={(e) => setCodec(e.target.value)}>
{codecs.map((c) => <option key={c.id} value={c.id} disabled={!c.engine}>{c.label}{c.engine ? ` — ${c.engine}` : ' — not on this machine'}</option>)}
</select>
</label>
<label>size
<select value={quality} onChange={(e) => setQuality(e.target.value)} disabled={codec === 'copy'}>
{caps.qualities.map((q) => <option key={q.id} value={q.id}>{q.label}</option>)}
</select>
</label>
<Btn className="btn on" disabled={!codecs.find((c) => c.id === codec)?.engine} onClick={start}>Play</Btn>
<a className="btn" href={api.fileDownloadURL(path, sudo)} download={name}>Download instead</a>
</div>
)}
{err && <div className="error" style={{ marginTop: 8 }}>{err}</div>}
</div>
)}
{mode === 'hls' && stream && (
<>
<div className="play-bar">
<span title={describe(stream.s)}>{describe(stream.s)}</span>
{status && status.running && <span>· {status.speed ? `${status.speed.toFixed(1)}×` : ''} {stream.s.source.duration ? `${Math.min(100, Math.round(status.progress / stream.s.source.duration * 100))}% through` : `${Math.round(status.progress)} s done`} — seek within what's done</span>}
{status?.done && <span>· all of it is ready</span>}
{status?.error && <span className="error">· stopped early: {status.error}</span>}
<span className="spacer" />
<button className="btn" onClick={() => { stop(); setMode('pick'); setWhy('') }}>Change…</button>
</div>
<div className="preview"><video ref={video} controls autoPlay playsInline /></div>
{err && <div className="error" style={{ margin: 12 }}>{err}</div>}
</>
)}
</div>
)
}