122 lines
5.6 KiB
Svelte
122 lines
5.6 KiB
Svelte
<script lang="ts">
|
|
import { IconPlayerPauseFilled, IconPlayerPlayFilled, IconPlayerSkipBackFilled, IconPlayerSkipForwardFilled, IconVolume, IconVolumeOff } from "./ui/icons"
|
|
import { untrack } from "svelte"
|
|
import { convertFileSrc, type CaptureRect } from "./api"
|
|
import { appear, bound, timecode } from "./ui/motion"
|
|
|
|
let { path, label, dimensions, seek = null, playbackRangeOnly = false, frameStep = .04,
|
|
playhead = $bindable(0), trimStart, trimEnd, crop, onOpenFile }: {
|
|
path: string; label: string; dimensions: string; seek?: { seconds: number } | null
|
|
playbackRangeOnly?: boolean; frameStep?: number
|
|
playhead?: number; trimStart: number; trimEnd: number
|
|
crop?: CaptureRect & { imageWidth: number; imageHeight: number }
|
|
onOpenFile?: () => void
|
|
} = $props()
|
|
|
|
let player: HTMLVideoElement | undefined = $state()
|
|
let playing = $state(false)
|
|
let muted = $state(false)
|
|
let loadError = $state(false)
|
|
let pending: { seconds: number } | null = null
|
|
|
|
$effect(() => {
|
|
const request = seek
|
|
if (!request) return
|
|
untrack(() => {
|
|
pending = request
|
|
if (player?.readyState) applySeek(player)
|
|
})
|
|
})
|
|
|
|
function applySeek(video: HTMLVideoElement) {
|
|
if (!pending || !Number.isFinite(video.duration)) return
|
|
video.pause()
|
|
video.currentTime = bound(pending.seconds, 0, Math.max(0, video.duration - .001))
|
|
pending = null
|
|
}
|
|
|
|
function updateTime() {
|
|
if (!player || player.seeking || pending) return
|
|
playhead = player.currentTime
|
|
if (!player.paused && playhead >= trimEnd) {
|
|
player.pause()
|
|
player.currentTime = Math.min(trimEnd, player.duration)
|
|
}
|
|
}
|
|
|
|
function metadata() {
|
|
if (!player) return
|
|
loadError = false
|
|
if (pending) applySeek(player)
|
|
else player.currentTime = bound(playhead, 0, Math.max(0, player.duration - .001))
|
|
}
|
|
|
|
async function play() {
|
|
if (!player || !player.readyState) return
|
|
if (!player.paused) return player.pause()
|
|
if (pending) applySeek(player)
|
|
if (player.currentTime < trimStart || player.currentTime >= trimEnd - .001) player.currentTime = trimStart
|
|
try { await player.play() } catch { loadError = true }
|
|
}
|
|
|
|
function step(delta: number) {
|
|
if (!player || !player.readyState) return
|
|
player.pause()
|
|
const current = pending?.seconds ?? player.currentTime
|
|
pending = null
|
|
player.currentTime = bound(current + delta, playbackRangeOnly ? trimStart : 0,
|
|
playbackRangeOnly ? Math.min(player.duration, trimEnd) : player.duration)
|
|
playhead = player.currentTime
|
|
}
|
|
</script>
|
|
|
|
<figure class="viewer">
|
|
<figcaption class="row between wrap">
|
|
<strong>{label}</strong><span class="mono note">{dimensions}</span>
|
|
</figcaption>
|
|
{#key path}
|
|
<div class="media" use:appear>
|
|
<!-- svelte-ignore a11y_media_has_caption (A local user recording has no caption track.) -->
|
|
<video src={convertFileSrc(path)} bind:this={player} preload="auto" playsinline aria-label={label}
|
|
{muted} onplay={() => playing = true} onpause={() => playing = false} onended={() => playing = false}
|
|
ontimeupdate={updateTime} onloadedmetadata={metadata} onerror={() => { loadError = true; playing = false }}></video>
|
|
{#if crop}
|
|
<span class="crop-guide" aria-hidden="true"
|
|
style:inset={crop.y / crop.imageHeight * 100 + "% " + (crop.imageWidth - crop.x - crop.width) / crop.imageWidth * 100 + "% " + (crop.imageHeight - crop.y - crop.height) / crop.imageHeight * 100 + "% " + crop.x / crop.imageWidth * 100 + "%"}></span>
|
|
{/if}
|
|
</div>
|
|
{/key}
|
|
{#if loadError}
|
|
<div class="notice problem row between wrap" role="alert">
|
|
<p>The clip could not be played here.</p>
|
|
{#if onOpenFile}<button type="button" class="button" onclick={onOpenFile}>Open rendered file</button>{/if}
|
|
</div>
|
|
{/if}
|
|
<div class="transport">
|
|
<button type="button" class="button ghost icon-only mute" aria-label={muted ? "Unmute preview" : "Mute preview"} onclick={() => muted = !muted}>
|
|
{#if muted}<IconVolumeOff size={18} />{:else}<IconVolume size={18} />{/if}
|
|
</button>
|
|
<div class="row">
|
|
<button type="button" class="button ghost icon-only" aria-label="Previous frame" onclick={() => step(-frameStep)}><IconPlayerSkipBackFilled size={16} /></button>
|
|
<button type="button" class="button icon-only" aria-label={playing ? "Pause preview" : "Play preview"} onclick={play}>
|
|
{#if playing}<IconPlayerPauseFilled size={16} />{:else}<IconPlayerPlayFilled size={16} />{/if}
|
|
</button>
|
|
<button type="button" class="button ghost icon-only" aria-label="Next frame" onclick={() => step(frameStep)}><IconPlayerSkipForwardFilled size={16} /></button>
|
|
</div>
|
|
<span class="mono note"><strong>{timecode(playhead)}</strong> / {timecode(trimEnd)}</span>
|
|
</div>
|
|
</figure>
|
|
|
|
<style>
|
|
.viewer { padding:8px; display:grid; gap:8px; border:var(--edge) solid var(--rule); border-radius:var(--radius); background:var(--input); }
|
|
figcaption { padding:0 4px; }
|
|
.media { margin:auto; position:relative; width:min(100%,560px); border-radius:var(--control-radius); overflow:hidden; }
|
|
video { display:block; width:100%; max-height:48vh; object-fit:contain; background:#101417; }
|
|
.crop-guide { position:absolute; pointer-events:none; border:var(--edge) dashed var(--signal); outline:3px solid #172125; }
|
|
.transport { display:grid; grid-template-columns:1fr auto 1fr; align-items:center; gap:4px; }
|
|
.transport .button { min-height:34px; }
|
|
.transport > span { justify-self:end; white-space:nowrap; text-align:right; }
|
|
.transport strong { color:var(--ink); }
|
|
@media(max-width:460px) { .transport { grid-template-columns:auto 1fr; } .mute { display:none; } }
|
|
</style>
|