diff --git a/dashboard/src/components/MediaInput.jsx b/dashboard/src/components/MediaInput.jsx index 079e3d0..6b67e2d 100644 --- a/dashboard/src/components/MediaInput.jsx +++ b/dashboard/src/components/MediaInput.jsx @@ -1,5 +1,6 @@ import React, { useState, useEffect, useRef } from 'react'; import { Link2, Upload, FileVideo, X, Info, Loader2, ChevronDown } from 'lucide-react'; +import { track } from '../lib/analytics'; import { getApiUrl } from '../config'; const SUPPORTED_PLATFORMS = [ @@ -7,12 +8,35 @@ const SUPPORTED_PLATFORMS = [ 'Facebook', 'Instagram', 'Dailymotion', 'Reddit', 'Streamable', ]; +// Mirrors the server's MIN_SOURCE_SECONDS: shorter sources are rejected with a +// 400 after the whole file was uploaded. Checking the duration in the browser +// says so the moment the file is picked (a 27 s upload used to end in a bare +// "That run failed"). +const MIN_SOURCE_SECONDS = 45; + +// Duration of a local video file in seconds, or null when the browser cannot +// read it (unsupported codec): then the server stays the judge. +const readVideoDuration = (file) => new Promise((resolve) => { + try { + const url = URL.createObjectURL(file); + const v = document.createElement('video'); + v.preload = 'metadata'; + const done = (d) => { URL.revokeObjectURL(url); resolve(d); }; + v.onloadedmetadata = () => done(Number.isFinite(v.duration) ? v.duration : null); + v.onerror = () => done(null); + setTimeout(() => done(null), 8000); + v.src = url; + } catch { resolve(null); } +}); + export default function MediaInput({ onProcess, isProcessing }) { const [youtubeUrlEnabled, setYoutubeUrlEnabled] = useState(true); // File upload is the primary path; the link is secondary. const [mode, setMode] = useState('file'); // 'file' | 'url' const [url, setUrl] = useState(''); const [file, setFile] = useState(null); + const [fileSeconds, setFileSeconds] = useState(null); + const fileTooShort = fileSeconds != null && fileSeconds < MIN_SOURCE_SECONDS; const [acknowledged, setAcknowledged] = useState(false); const [outputFormat, setOutputFormat] = useState('vertical'); // vertical | horizontal | square const [showInfo, setShowInfo] = useState(false); @@ -48,6 +72,20 @@ export default function MediaInput({ onProcess, isProcessing }) { return () => document.removeEventListener('mousedown', onClick); }, [showInfo]); + useEffect(() => { + let cancelled = false; + setFileSeconds(null); + if (!file) return undefined; + readVideoDuration(file).then((d) => { + if (cancelled) return; + setFileSeconds(d); + if (d != null && d < MIN_SOURCE_SECONDS) { + track('SourceTooShort', { props: { seconds: String(Math.round(d)) } }); + } + }); + return () => { cancelled = true; }; + }, [file]); + useEffect(() => { fetch(getApiUrl('/api/config')) .then((r) => r.ok ? r.json() : null) @@ -93,7 +131,7 @@ export default function MediaInput({ onProcess, isProcessing }) { } catch { /* ignore */ } if (mode === 'url' && url) { onProcess({ type: 'url', payload: url, acknowledged: true, outputFormat, ...advanced }); - } else if (mode === 'file' && file) { + } else if (mode === 'file' && file && !fileTooShort) { onProcess({ type: 'file', payload: file, acknowledged: true, outputFormat, ...advanced }); } }; @@ -192,7 +230,14 @@ export default function MediaInput({ onProcess, isProcessing }) { - ) : ( + ) : null} + {file && fileTooShort ? ( +

+ This video is {Math.round(fileSeconds)}s long. Clip generation needs at least {MIN_SOURCE_SECONDS}s + of footage to cut from: it already is a short. Pick a longer video. +

+ ) : null} + {!file && ( )} @@ -353,7 +398,7 @@ export default function MediaInput({ onProcess, isProcessing }) {