capture, encode, export wiring

This commit is contained in:
2026-05-11 23:54:21 +03:00
parent c64ec2045e
commit 2c041be5a9
5 changed files with 938 additions and 68 deletions
+341 -61
View File
@@ -1,56 +1,221 @@
<script lang="ts">
import { api, type Config } from "./api"
import { onMount } from "svelte"
import {
api,
formatBytes,
type Config,
type Format,
type RecordedClip,
type ExportResult,
} from "./api"
let recording: boolean = $state(false)
type Phase = "idle" | "recording" | "editing" | "exporting" | "done"
let phase: Phase = $state("idle")
let config: Config | null = $state(null)
let clip: RecordedClip | null = $state(null)
let elapsed: number = $state(0)
let elapsedTimer: number | null = null
let format: Format = $state("gif")
let keepAudio: boolean = $state(false)
let fps: number = $state(30)
let targetMB: number = $state(8)
let exportResult: ExportResult | null = $state(null)
let errMsg: string | null = $state(null)
onMount(async () => {
config = await api.getConfig()
format = config.export.default_format
fps = config.capture.default_fps
keepAudio = config.capture.capture_audio
})
function toggle() {
recording = !recording
function tick() {
elapsedTimer = window.setInterval(() => {
elapsed += 0.1
}, 100)
}
function clearTick() {
if (elapsedTimer !== null) {
clearInterval(elapsedTimer)
elapsedTimer = null
}
}
async function startRec() {
errMsg = null
elapsed = 0
try {
await api.startCapture()
phase = "recording"
tick()
} catch (e) {
errMsg = String(e)
}
}
async function stopRec() {
clearTick()
try {
const c = await api.stopCapture()
clip = c
phase = "editing"
const sensible = Math.max(1, Math.round(c.duration_secs * 0.5))
targetMB = Math.min(50, Math.max(minMB(c.duration_secs), sensible))
} catch (e) {
errMsg = String(e)
phase = "idle"
}
}
function minMB(durationSecs: number): number {
const minBytes = Math.max(50_000, durationSecs * 50_000)
return Math.max(0.1, minBytes / 1_048_576)
}
function maxMB(durationSecs: number): number {
return Math.min(200, Math.max(10, durationSecs * 5))
}
async function doExport() {
if (!clip) return
errMsg = null
phase = "exporting"
try {
const result = await api.export({
format,
target_bytes: Math.round(targetMB * 1_048_576),
fps,
keep_audio: keepAudio && format !== "gif",
})
exportResult = result
phase = "done"
} catch (e) {
errMsg = String(e)
phase = "editing"
}
}
function startOver() {
clip = null
exportResult = null
errMsg = null
elapsed = 0
phase = "idle"
}
</script>
<main>
<header>
<h1>ByteSize</h1>
<p class="sub">ready</p>
<p class="sub">
{#if phase === "recording"}recording...
{:else if phase === "editing"}clip ready
{:else if phase === "exporting"}encoding...
{:else if phase === "done"}exported
{:else}ready{/if}
</p>
</header>
<section class="controls">
<button onclick={toggle} class:active={recording}>
{recording ? "stop" : "start"}
</button>
</section>
{#if config}
<section class="cfg">
<h2>current settings</h2>
{#if phase === "idle"}
<section class="controls">
<button class="primary big" onclick={startRec}>start recording</button>
</section>
<p class="hint">
Captures your primary monitor. Region capture and the draggable overlay
come next.
</p>
{:else if phase === "recording"}
<section class="recording">
<div class="elapsed">{elapsed.toFixed(1)}s</div>
<button class="stop big" onclick={stopRec}>stop</button>
</section>
{:else if phase === "editing" && clip}
<section class="clip">
<h2>clip</h2>
<dl>
<dt>format</dt>
<dd>{config.export.default_format}</dd>
<dt>fps</dt>
<dd>{config.capture.default_fps}</dd>
<dt>cursor</dt>
<dd>{config.capture.capture_cursor ? "on" : "off"}</dd>
<dt>audio</dt>
<dd>{config.capture.capture_audio ? "on" : "off"}</dd>
<dt>hotkeys</dt>
<dd>
start: {config.hotkeys.start ?? "-"} |
stop: {config.hotkeys.stop ?? "-"} |
pause: {config.hotkeys.pause ?? "-"}
</dd>
<dt>duration</dt><dd>{clip.duration_secs.toFixed(2)}s</dd>
<dt>dimensions</dt><dd>{clip.width} x {clip.height}</dd>
</dl>
</section>
<section class="export">
<h2>export</h2>
<div class="row">
<label class:active={format === "gif"}>
<input type="radio" bind:group={format} value="gif" />
<span>GIF</span>
</label>
<label class:active={format === "webm"}>
<input type="radio" bind:group={format} value="webm" />
<span>WebM</span>
</label>
<label class:active={format === "mp4"}>
<input type="radio" bind:group={format} value="mp4" />
<span>MP4</span>
</label>
</div>
<div class="slider-row">
<label>
target size
<input
type="range"
min={minMB(clip.duration_secs)}
max={maxMB(clip.duration_secs)}
step="0.1"
bind:value={targetMB}
/>
</label>
<div class="readout">{targetMB.toFixed(1)} MB</div>
</div>
<div class="grid-row">
<label>
fps
<select bind:value={fps}>
<option value={15}>15</option>
<option value={24}>24</option>
<option value={30}>30</option>
<option value={60}>60</option>
</select>
</label>
{#if format !== "gif"}
<label class="checkbox">
<input type="checkbox" bind:checked={keepAudio} />
<span>keep audio</span>
</label>
{/if}
</div>
<div class="row">
<button class="primary" onclick={doExport}>export</button>
<button onclick={startOver}>discard and start over</button>
</div>
</section>
{:else if phase === "exporting"}
<section class="exporting">
<p>ffmpeg is working...</p>
<p class="hint">two-pass encode for target size. usually a few seconds.</p>
</section>
{:else if phase === "done" && exportResult}
<section class="done">
<h2>exported</h2>
<p class="path">{exportResult.output_path}</p>
<p>final size: {formatBytes(exportResult.size_bytes)}</p>
<div class="row">
<button class="primary" onclick={startOver}>record another</button>
</div>
</section>
{/if}
<p class="hint">
capture and encode aren't wired up yet. that's next.
</p>
{#if errMsg}
<p class="err">{errMsg}</p>
{/if}
</main>
<style>
@@ -69,34 +234,10 @@
font-size: 0.875rem;
margin: 0 0 1.5rem;
}
.controls {
margin: 1rem 0 2rem;
section {
margin: 1.25rem 0;
}
button {
font: inherit;
padding: 0.6rem 1.2rem;
border: 1px solid #444;
background: transparent;
color: inherit;
cursor: pointer;
border-radius: 4px;
font-size: 1rem;
}
button:hover {
border-color: #888;
}
button.active {
background: #c33;
border-color: #c33;
color: #fff;
}
.cfg {
border: 1px solid #2a2a2a;
border-radius: 6px;
padding: 1rem 1.25rem;
background: #1e1e1e;
}
.cfg h2 {
h2 {
margin: 0 0 0.75rem;
font-size: 0.85rem;
color: #888;
@@ -106,8 +247,8 @@
}
dl {
display: grid;
grid-template-columns: 100px 1fr;
gap: 0.5rem 1rem;
grid-template-columns: 110px 1fr;
gap: 0.4rem 1rem;
margin: 0;
font-size: 0.9rem;
}
@@ -117,9 +258,148 @@
dd {
margin: 0;
}
.controls {
display: flex;
justify-content: center;
margin-top: 2rem;
}
.recording {
display: grid;
place-items: center;
gap: 1rem;
margin-top: 1rem;
}
.elapsed {
font-size: 2.5rem;
font-variant-numeric: tabular-nums;
color: #4a8;
}
button {
font: inherit;
padding: 0.5rem 1rem;
border: 1px solid #444;
background: transparent;
color: inherit;
cursor: pointer;
border-radius: 4px;
}
button:hover {
border-color: #888;
}
button.primary {
background: #4a8;
border-color: #4a8;
color: #111;
}
button.primary:hover {
background: #5b9;
border-color: #5b9;
}
button.big {
padding: 0.75rem 1.75rem;
font-size: 1rem;
}
button.stop {
background: #c33;
border-color: #c33;
color: #fff;
padding: 0.75rem 2rem;
font-size: 1rem;
}
.clip {
border: 1px solid #2a2a2a;
border-radius: 6px;
padding: 1rem 1.25rem;
background: #1e1e1e;
}
.row {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
margin: 0.5rem 0;
}
.row label {
flex: 1;
border: 1px solid #2a2a2a;
padding: 0.5rem 0.75rem;
border-radius: 4px;
cursor: pointer;
text-align: center;
background: #1e1e1e;
}
.row label.active {
border-color: #4a8;
background: #1a2a24;
}
.row label input[type="radio"] {
display: none;
}
.slider-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 1rem;
align-items: center;
margin: 1rem 0;
}
.slider-row label {
display: grid;
gap: 0.4rem;
color: #aaa;
font-size: 0.85rem;
}
.readout {
font-variant-numeric: tabular-nums;
font-size: 1rem;
color: #e8e8e8;
min-width: 80px;
text-align: right;
}
input[type="range"] {
width: 100%;
}
.grid-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin: 1rem 0;
}
.grid-row label {
display: grid;
gap: 0.4rem;
color: #aaa;
font-size: 0.85rem;
}
.checkbox {
flex-direction: row !important;
align-items: center !important;
gap: 0.5rem !important;
}
select {
font: inherit;
padding: 0.4rem 0.5rem;
border: 1px solid #444;
background: #222;
color: inherit;
border-radius: 4px;
}
.path {
font-family: ui-monospace, monospace;
font-size: 0.85rem;
color: #ccc;
word-break: break-all;
background: #1e1e1e;
padding: 0.5rem 0.75rem;
border-radius: 4px;
}
.err {
color: #ec9a9a;
background: #2a1818;
padding: 0.6rem 0.75rem;
border-radius: 4px;
font-size: 0.85rem;
}
.hint {
color: #888;
font-size: 0.85rem;
margin-top: 2rem;
}
</style>