|
@@ -1,105 +1,134 @@
|
|
|
import { useState } from 'react'
|
|
import { useState } from 'react'
|
|
|
-import { Loader2, Play, RotateCcw, Scissors } from 'lucide-react'
|
|
|
|
|
-import { formatVideoTime, parseVideoTime, trimmedVideoLength } from '@shared/videoTrim'
|
|
|
|
|
-
|
|
|
|
|
-export interface VideoTrim {
|
|
|
|
|
- start?: number
|
|
|
|
|
- end?: number
|
|
|
|
|
-}
|
|
|
|
|
|
|
+import { Loader2, Play, Plus, RotateCcw, Scissors, X } from 'lucide-react'
|
|
|
|
|
+import {
|
|
|
|
|
+ formatVideoTime,
|
|
|
|
|
+ normalizeVideoSegments,
|
|
|
|
|
+ parseVideoTime,
|
|
|
|
|
+ trimmedVideoLength,
|
|
|
|
|
+ type VideoSegment
|
|
|
|
|
+} from '@shared/videoTrim'
|
|
|
|
|
|
|
|
interface Props {
|
|
interface Props {
|
|
|
- start?: number
|
|
|
|
|
- end?: number
|
|
|
|
|
|
|
+ segments: VideoSegment[]
|
|
|
/** Length of the whole video in seconds, null when it couldn't be measured (no ffprobe) */
|
|
/** Length of the whole video in seconds, null when it couldn't be measured (no ffprobe) */
|
|
|
duration: number | null
|
|
duration: number | null
|
|
|
live: boolean
|
|
live: boolean
|
|
|
- onCommit: (trim: VideoTrim) => Promise<void>
|
|
|
|
|
- /** seek is where playback should jump right after, to watch the loop point */
|
|
|
|
|
- onPreview: (trim: VideoTrim, seek?: number) => void
|
|
|
|
|
|
|
+ onCommit: (segments: VideoSegment[]) => Promise<void>
|
|
|
|
|
+ /** seek: where playback jumps afterwards */
|
|
|
|
|
+ onPreview: (segments: VideoSegment[], seek?: number) => void
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// a loop shorter than this is almost always a slip of the slider
|
|
|
|
|
const MIN_LENGTH = 0.5
|
|
const MIN_LENGTH = 0.5
|
|
|
-// how much before the end the loop point preview starts
|
|
|
|
|
|
|
+// preview starts this long before a cut
|
|
|
const SEAM_LEAD = 3
|
|
const SEAM_LEAD = 3
|
|
|
|
|
+const NEW_PART_LENGTH = 30
|
|
|
|
|
|
|
|
const timeInputClass =
|
|
const timeInputClass =
|
|
|
'w-16 rounded bg-white/5 px-1 py-0.5 text-right text-gray-300 outline-none focus:ring-1 focus:ring-indigo-500 disabled:opacity-50'
|
|
'w-16 rounded bg-white/5 px-1 py-0.5 text-right text-gray-300 outline-none focus:ring-1 focus:ring-indigo-500 disabled:opacity-50'
|
|
|
|
|
|
|
|
-export default function VideoTrimSection({ start, end, duration, live, onCommit, onPreview }: Props) {
|
|
|
|
|
- const [draft, setDraft] = useState<VideoTrim | null>(null)
|
|
|
|
|
- const [startText, setStartText] = useState<string | null>(null)
|
|
|
|
|
- const [endText, setEndText] = useState<string | null>(null)
|
|
|
|
|
|
|
+export default function VideoTrimSection({ segments, duration, live, onCommit, onPreview }: Props) {
|
|
|
|
|
+ const [draft, setDraft] = useState<VideoSegment[] | null>(null)
|
|
|
|
|
+ const [texts, setTexts] = useState<Record<string, string>>({})
|
|
|
|
|
+ const [selected, setSelected] = useState(0)
|
|
|
const [busy, setBusy] = useState(false)
|
|
const [busy, setBusy] = useState(false)
|
|
|
- const current = draft ?? { start, end }
|
|
|
|
|
- const startValue = current.start ?? 0
|
|
|
|
|
- const loopEnd = current.end ?? duration
|
|
|
|
|
- const trimmed = start !== undefined || end !== undefined
|
|
|
|
|
-
|
|
|
|
|
- // the very start and the very end are the same as no trim at all
|
|
|
|
|
- function normalize(trim: VideoTrim): VideoTrim {
|
|
|
|
|
- return {
|
|
|
|
|
- start: trim.start !== undefined && trim.start > 0 ? trim.start : undefined,
|
|
|
|
|
- end: trim.end !== undefined && (duration === null || trim.end < duration) ? trim.end : undefined
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const rows = draft ?? segments
|
|
|
|
|
+ const index = Math.min(selected, rows.length - 1)
|
|
|
|
|
+ const part = index >= 0 ? rows[index] : undefined
|
|
|
|
|
+ const trimmed = segments.length > 0
|
|
|
|
|
+
|
|
|
|
|
+ function endOf(segment: VideoSegment): number | null {
|
|
|
|
|
+ return segment.end ?? duration
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function seamSeek(trim: VideoTrim): number | undefined {
|
|
|
|
|
- const endOfLoop = trim.end ?? duration
|
|
|
|
|
- return endOfLoop === null ? undefined : Math.max(trim.start ?? 0, endOfLoop - SEAM_LEAD)
|
|
|
|
|
|
|
+ function seamSeek(segment: VideoSegment): number | undefined {
|
|
|
|
|
+ const end = endOf(segment)
|
|
|
|
|
+ return end === null ? undefined : Math.max(segment.start, end - SEAM_LEAD)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async function commit(next: VideoTrim) {
|
|
|
|
|
|
|
+ function clearText(key: string) {
|
|
|
|
|
+ setTexts((t) => {
|
|
|
|
|
+ const rest = { ...t }
|
|
|
|
|
+ delete rest[key]
|
|
|
|
|
+ return rest
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function withPart(i: number, changed: VideoSegment): VideoSegment[] {
|
|
|
|
|
+ return rows.map((s, j) => (j === i ? changed : s))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async function commit(next: VideoSegment[]) {
|
|
|
setBusy(true)
|
|
setBusy(true)
|
|
|
try {
|
|
try {
|
|
|
- await onCommit(normalize(next))
|
|
|
|
|
|
|
+ await onCommit(normalizeVideoSegments(next, duration))
|
|
|
} finally {
|
|
} finally {
|
|
|
setDraft(null)
|
|
setDraft(null)
|
|
|
- setStartText(null)
|
|
|
|
|
- setEndText(null)
|
|
|
|
|
|
|
+ setTexts({})
|
|
|
setBusy(false)
|
|
setBusy(false)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function slideStart(value: number) {
|
|
|
|
|
- const next = { ...current, start: loopEnd === null ? value : Math.min(value, loopEnd - MIN_LENGTH) }
|
|
|
|
|
|
|
+ function slide(side: 'start' | 'end', value: number) {
|
|
|
|
|
+ if (!part) return
|
|
|
|
|
+ const end = endOf(part)
|
|
|
|
|
+ const changed =
|
|
|
|
|
+ side === 'start'
|
|
|
|
|
+ ? { ...part, start: end === null ? value : Math.min(value, end - MIN_LENGTH) }
|
|
|
|
|
+ : { ...part, end: Math.max(value, part.start + MIN_LENGTH) }
|
|
|
|
|
+ const next = withPart(index, changed)
|
|
|
setDraft(next)
|
|
setDraft(next)
|
|
|
- onPreview(normalize(next))
|
|
|
|
|
|
|
+ onPreview(normalizeVideoSegments(next, duration), side === 'end' ? seamSeek(changed) : undefined)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function slideEnd(value: number) {
|
|
|
|
|
- const next = { ...current, end: Math.max(value, startValue + MIN_LENGTH) }
|
|
|
|
|
- setDraft(next)
|
|
|
|
|
- onPreview(normalize(next), seamSeek(next))
|
|
|
|
|
|
|
+ function commitText(i: number, side: 'start' | 'end', text: string) {
|
|
|
|
|
+ const key = `${i}-${side}`
|
|
|
|
|
+ const reset = () => clearText(key)
|
|
|
|
|
+ const value = text.trim() === '' ? (side === 'start' ? 0 : undefined) : parseVideoTime(text)
|
|
|
|
|
+ if (value === null) return reset()
|
|
|
|
|
+ const changed = { ...rows[i], [side]: value }
|
|
|
|
|
+ if (changed.start === rows[i].start && changed.end === rows[i].end) return reset()
|
|
|
|
|
+ const length = (changed.end ?? duration ?? Infinity) - changed.start
|
|
|
|
|
+ if (length < MIN_LENGTH || (duration !== null && changed.start >= duration)) return reset()
|
|
|
|
|
+ void commit(withPart(i, changed))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function commitText(side: 'start' | 'end', text: string) {
|
|
|
|
|
- const reset = () => (side === 'start' ? setStartText(null) : setEndText(null))
|
|
|
|
|
- const value = text.trim() === '' ? undefined : parseVideoTime(text)
|
|
|
|
|
- if (value === null) return reset()
|
|
|
|
|
- const next = { ...current, [side]: value }
|
|
|
|
|
- const length = (next.end ?? duration ?? Infinity) - (next.start ?? 0)
|
|
|
|
|
- if (length < MIN_LENGTH || (side === 'start' && duration !== null && (value ?? 0) >= duration)) return reset()
|
|
|
|
|
- if (value === current[side]) return reset()
|
|
|
|
|
- void commit(next)
|
|
|
|
|
|
|
+ function newPart(): VideoSegment | null {
|
|
|
|
|
+ const last = rows[rows.length - 1]
|
|
|
|
|
+ const start = last ? last.end : 0
|
|
|
|
|
+ if (start === undefined) return null
|
|
|
|
|
+ if (duration === null) return { start, end: start + NEW_PART_LENGTH }
|
|
|
|
|
+ if (start > duration - MIN_LENGTH) return null
|
|
|
|
|
+ return { start, end: Math.min(start + NEW_PART_LENGTH, duration) }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function addPart() {
|
|
|
|
|
+ const added = newPart()
|
|
|
|
|
+ if (!added) return
|
|
|
|
|
+ setSelected(rows.length)
|
|
|
|
|
+ void commit([...rows, added])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function timeInput(side: 'start' | 'end') {
|
|
|
|
|
- const text = side === 'start' ? startText : endText
|
|
|
|
|
- const setText = side === 'start' ? setStartText : setEndText
|
|
|
|
|
- const value = side === 'start' ? startValue : loopEnd
|
|
|
|
|
|
|
+ function removePart(i: number) {
|
|
|
|
|
+ setSelected(Math.max(0, i - 1))
|
|
|
|
|
+ void commit(rows.filter((_, j) => j !== i))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function timeInput(i: number, side: 'start' | 'end') {
|
|
|
|
|
+ const key = `${i}-${side}`
|
|
|
|
|
+ const value = side === 'start' ? rows[i].start : endOf(rows[i])
|
|
|
return (
|
|
return (
|
|
|
<input
|
|
<input
|
|
|
type="text"
|
|
type="text"
|
|
|
- value={text ?? (value === null ? '' : formatVideoTime(value))}
|
|
|
|
|
|
|
+ value={texts[key] ?? (value === null ? '' : formatVideoTime(value))}
|
|
|
placeholder={side === 'start' ? '0:00' : 'end'}
|
|
placeholder={side === 'start' ? '0:00' : 'end'}
|
|
|
disabled={busy}
|
|
disabled={busy}
|
|
|
- onChange={(e) => setText(e.target.value)}
|
|
|
|
|
|
|
+ onFocus={() => setSelected(i)}
|
|
|
|
|
+ onChange={(e) => setTexts((t) => ({ ...t, [key]: e.target.value }))}
|
|
|
onKeyDown={(e) => {
|
|
onKeyDown={(e) => {
|
|
|
if (e.key === 'Enter') e.currentTarget.blur()
|
|
if (e.key === 'Enter') e.currentTarget.blur()
|
|
|
- if (e.key === 'Escape') setText(null)
|
|
|
|
|
|
|
+ if (e.key === 'Escape') clearText(key)
|
|
|
}}
|
|
}}
|
|
|
- onBlur={(e) => text !== null && commitText(side, e.currentTarget.value)}
|
|
|
|
|
|
|
+ onBlur={(e) => texts[key] !== undefined && commitText(i, side, e.currentTarget.value)}
|
|
|
title="Seconds, m:ss or h:mm:ss - applies on Enter or clicking away"
|
|
title="Seconds, m:ss or h:mm:ss - applies on Enter or clicking away"
|
|
|
className={timeInputClass}
|
|
className={timeInputClass}
|
|
|
/>
|
|
/>
|
|
@@ -107,23 +136,25 @@ export default function VideoTrimSection({ start, end, duration, live, onCommit,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function slider(side: 'start' | 'end') {
|
|
function slider(side: 'start' | 'end') {
|
|
|
- if (duration === null) return null
|
|
|
|
|
- const value = side === 'start' ? startValue : (loopEnd ?? duration)
|
|
|
|
|
|
|
+ if (duration === null || !part) return null
|
|
|
return (
|
|
return (
|
|
|
- <input
|
|
|
|
|
- type="range"
|
|
|
|
|
- min={0}
|
|
|
|
|
- max={duration}
|
|
|
|
|
- step={0.1}
|
|
|
|
|
- value={value}
|
|
|
|
|
- disabled={busy}
|
|
|
|
|
- onChange={(e) => (side === 'start' ? slideStart : slideEnd)(Number(e.target.value))}
|
|
|
|
|
- onMouseUp={() => draft && commit(draft)}
|
|
|
|
|
- onTouchEnd={() => draft && commit(draft)}
|
|
|
|
|
- onKeyUp={() => draft && commit(draft)}
|
|
|
|
|
- title={side === 'start' ? 'Where the loop starts' : 'Where the loop ends and jumps back to the start'}
|
|
|
|
|
- className="w-full accent-indigo-500 disabled:opacity-50"
|
|
|
|
|
- />
|
|
|
|
|
|
|
+ <div className="flex items-center gap-1.5">
|
|
|
|
|
+ <span className="w-7 text-gray-500">{side === 'start' ? 'Start' : 'End'}</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="range"
|
|
|
|
|
+ min={0}
|
|
|
|
|
+ max={duration}
|
|
|
|
|
+ step={0.1}
|
|
|
|
|
+ value={side === 'start' ? part.start : (part.end ?? duration)}
|
|
|
|
|
+ disabled={busy}
|
|
|
|
|
+ onChange={(e) => slide(side, Number(e.target.value))}
|
|
|
|
|
+ onMouseUp={() => draft && commit(draft)}
|
|
|
|
|
+ onTouchEnd={() => draft && commit(draft)}
|
|
|
|
|
+ onKeyUp={() => draft && commit(draft)}
|
|
|
|
|
+ title={side === 'start' ? 'Where this part starts' : 'Where this part ends and the next one starts'}
|
|
|
|
|
+ className="w-full accent-indigo-500 disabled:opacity-50"
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -137,7 +168,7 @@ export default function VideoTrimSection({ start, end, duration, live, onCommit,
|
|
|
{trimmed && (
|
|
{trimmed && (
|
|
|
<button
|
|
<button
|
|
|
disabled={busy}
|
|
disabled={busy}
|
|
|
- onClick={() => commit({})}
|
|
|
|
|
|
|
+ onClick={() => commit([])}
|
|
|
title="Play the whole video again"
|
|
title="Play the whole video again"
|
|
|
className="text-gray-500 hover:text-gray-300 disabled:opacity-50"
|
|
className="text-gray-500 hover:text-gray-300 disabled:opacity-50"
|
|
|
>
|
|
>
|
|
@@ -145,34 +176,79 @@ export default function VideoTrimSection({ start, end, duration, live, onCommit,
|
|
|
</button>
|
|
</button>
|
|
|
)}
|
|
)}
|
|
|
</div>
|
|
</div>
|
|
|
- <p className="mb-1.5 text-[11px] text-gray-600">Only this part of the video plays, over and over.</p>
|
|
|
|
|
|
|
+ <p className="mb-1.5 text-[11px] text-gray-600">
|
|
|
|
|
+ Only these parts play, one after the other, over and over. Everything between them is skipped.
|
|
|
|
|
+ </p>
|
|
|
|
|
|
|
|
<div className="space-y-1.5 text-[11px] text-gray-400">
|
|
<div className="space-y-1.5 text-[11px] text-gray-400">
|
|
|
- <div className="flex items-center gap-1.5">
|
|
|
|
|
- <span className="flex-1">Start</span>
|
|
|
|
|
- {timeInput('start')}
|
|
|
|
|
- </div>
|
|
|
|
|
|
|
+ {duration !== null && (
|
|
|
|
|
+ <div className="relative h-2.5 overflow-hidden rounded bg-white/5">
|
|
|
|
|
+ {(rows.length ? rows : [{ start: 0 }]).map((s, i) => (
|
|
|
|
|
+ <button
|
|
|
|
|
+ key={i}
|
|
|
|
|
+ disabled={!rows.length}
|
|
|
|
|
+ onClick={() => setSelected(i)}
|
|
|
|
|
+ title={`${formatVideoTime(s.start)} - ${formatVideoTime(endOf(s) ?? duration)}`}
|
|
|
|
|
+ className={`absolute inset-y-0 ${rows.length && i === index ? 'bg-indigo-400' : 'bg-indigo-500/50'}`}
|
|
|
|
|
+ style={{
|
|
|
|
|
+ left: `${(Math.min(s.start, duration) / duration) * 100}%`,
|
|
|
|
|
+ width: `${((Math.min(endOf(s) ?? duration, duration) - Math.min(s.start, duration)) / duration) * 100}%`
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ {rows.length === 0 && <p className="text-gray-600">Plays the whole video.</p>}
|
|
|
|
|
+ {rows.map((s, i) => (
|
|
|
|
|
+ <div key={i} className={`flex items-center gap-1.5 rounded px-1 ${i === index ? 'bg-white/5' : ''}`}>
|
|
|
|
|
+ <button
|
|
|
|
|
+ onClick={() => setSelected(i)}
|
|
|
|
|
+ className={`flex-1 text-left ${i === index ? 'text-gray-200' : 'text-gray-500 hover:text-gray-300'}`}
|
|
|
|
|
+ >
|
|
|
|
|
+ Part {i + 1}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ {timeInput(i, 'start')}
|
|
|
|
|
+ <span className="text-gray-600">-</span>
|
|
|
|
|
+ {timeInput(i, 'end')}
|
|
|
|
|
+ <button
|
|
|
|
|
+ disabled={busy}
|
|
|
|
|
+ onClick={() => removePart(i)}
|
|
|
|
|
+ title="Remove this part"
|
|
|
|
|
+ className="text-gray-500 hover:text-red-300 disabled:opacity-50"
|
|
|
|
|
+ >
|
|
|
|
|
+ <X size={11} />
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ))}
|
|
|
|
|
+
|
|
|
{slider('start')}
|
|
{slider('start')}
|
|
|
- <div className="flex items-center gap-1.5">
|
|
|
|
|
- <span className="flex-1">End</span>
|
|
|
|
|
- {timeInput('end')}
|
|
|
|
|
- </div>
|
|
|
|
|
{slider('end')}
|
|
{slider('end')}
|
|
|
|
|
|
|
|
<div className="flex items-center gap-1.5 pt-0.5">
|
|
<div className="flex items-center gap-1.5 pt-0.5">
|
|
|
<span className="flex-1 text-gray-500">
|
|
<span className="flex-1 text-gray-500">
|
|
|
{duration !== null &&
|
|
{duration !== null &&
|
|
|
- `Loops ${formatVideoTime(trimmedVideoLength(duration, current.start, current.end))} of ${formatVideoTime(duration)}`}
|
|
|
|
|
|
|
+ trimmed &&
|
|
|
|
|
+ `Plays ${formatVideoTime(trimmedVideoLength(duration, rows))} of ${formatVideoTime(duration)}`}
|
|
|
</span>
|
|
</span>
|
|
|
- {live && trimmed && seamSeek({ start, end }) !== undefined && (
|
|
|
|
|
|
|
+ <button
|
|
|
|
|
+ disabled={busy || newPart() === null}
|
|
|
|
|
+ onClick={addPart}
|
|
|
|
|
+ title="Add another part after the last one"
|
|
|
|
|
+ className="flex items-center gap-1 rounded bg-white/10 px-1.5 py-0.5 text-[10px] text-gray-300 hover:bg-white/15 disabled:opacity-40"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Plus size={9} />
|
|
|
|
|
+ Add part
|
|
|
|
|
+ </button>
|
|
|
|
|
+ {live && trimmed && part && seamSeek(part) !== undefined && (
|
|
|
<button
|
|
<button
|
|
|
disabled={busy}
|
|
disabled={busy}
|
|
|
- onClick={() => onPreview({ start, end }, seamSeek({ start, end }))}
|
|
|
|
|
- title={`Jumps ${SEAM_LEAD} seconds before the end, to see how the loop point looks`}
|
|
|
|
|
|
|
+ onClick={() => onPreview(segments, seamSeek(part))}
|
|
|
|
|
+ title={`Jumps ${SEAM_LEAD} seconds before the end of part ${index + 1}, to see the cut into the next one`}
|
|
|
className="flex items-center gap-1 rounded bg-white/10 px-1.5 py-0.5 text-[10px] text-gray-300 hover:bg-white/15 disabled:opacity-40"
|
|
className="flex items-center gap-1 rounded bg-white/10 px-1.5 py-0.5 text-[10px] text-gray-300 hover:bg-white/15 disabled:opacity-40"
|
|
|
>
|
|
>
|
|
|
<Play size={9} />
|
|
<Play size={9} />
|
|
|
- Loop point
|
|
|
|
|
|
|
+ Cut
|
|
|
</button>
|
|
</button>
|
|
|
)}
|
|
)}
|
|
|
</div>
|
|
</div>
|