separate trim from review and save
This commit is contained in:
+112
-55
@@ -1,64 +1,121 @@
|
||||
<script lang="ts">
|
||||
import { convertFileSrc } from "./api"
|
||||
import { trimPreviewState } from "./trimPreviewState.svelte"
|
||||
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 {
|
||||
previewPath,
|
||||
previewLoading,
|
||||
previewCacheBust,
|
||||
}: {
|
||||
previewPath: string | null
|
||||
previewLoading: boolean
|
||||
previewCacheBust: number
|
||||
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>
|
||||
|
||||
{#if trimPreviewState.dragging && trimPreviewState.path}
|
||||
<div class="preview-box">
|
||||
<img src={convertFileSrc(trimPreviewState.path) + "?t=" + trimPreviewState.bust} alt="preview frame" class="preview-img"/>
|
||||
<div class="preview-loading">original</div>
|
||||
<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>
|
||||
{:else if previewPath}
|
||||
<div class="preview-box">
|
||||
<img src={convertFileSrc(previewPath) + "?t=" + previewCacheBust} alt="preview frame" class="preview-img"/>
|
||||
{#if previewLoading}<div class="preview-loading">rendering</div>{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="preview-box empty">
|
||||
<span class="preview-placeholder">preview</span>
|
||||
</div>
|
||||
{/if}
|
||||
</figure>
|
||||
|
||||
<style>
|
||||
.preview-box {
|
||||
position: relative;
|
||||
border-radius: var(--r-sm);
|
||||
overflow: hidden;
|
||||
background: var(--bg-2);
|
||||
border: 1px solid var(--border);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
aspect-ratio: 16 / 9;
|
||||
}
|
||||
.preview-img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
display: block;
|
||||
}
|
||||
.preview-placeholder {
|
||||
font-size: 11px;
|
||||
color: var(--text-4);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
}
|
||||
.preview-loading {
|
||||
position: absolute; inset: 0;
|
||||
background: rgba(0,0,0,0.5);
|
||||
display: grid; place-items: center;
|
||||
font-size: 11px;
|
||||
color: var(--text-3);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
}
|
||||
.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>
|
||||
|
||||
Reference in New Issue
Block a user