feat(dashboard): say a video is too short the moment it is picked
Uploads under 45 s (MIN_SOURCE_SECONDS) were only rejected by the server after the whole file went up. The browser now reads the file's duration on select, explains why it cannot be clipped, keeps Generate disabled, and the drop zone states the minimum. A file the browser cannot read is left to the server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
681bfb5719
commit
d585b93ea2
1 changed files with 49 additions and 4 deletions
|
|
@ -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 }) {
|
|||
<X size={16} />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
) : null}
|
||||
{file && fileTooShort ? (
|
||||
<p className="text-danger text-sm mt-3" role="alert">
|
||||
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.
|
||||
</p>
|
||||
) : null}
|
||||
{!file && (
|
||||
<label className="cursor-pointer block">
|
||||
<input
|
||||
type="file"
|
||||
|
|
@ -202,7 +247,7 @@ export default function MediaInput({ onProcess, isProcessing }) {
|
|||
/>
|
||||
<Upload className="mx-auto mb-3 text-muted" size={18} />
|
||||
<p className="text-ink2 lowercase">Click to upload or drag and drop</p>
|
||||
<p className="readout mt-2">MP4, MOV up to 500MB</p>
|
||||
<p className="readout mt-2">MP4, MOV up to 500MB · at least {MIN_SOURCE_SECONDS}s long</p>
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
|
|
@ -353,7 +398,7 @@ export default function MediaInput({ onProcess, isProcessing }) {
|
|||
<button
|
||||
type="submit"
|
||||
data-tutorial="generate"
|
||||
disabled={isProcessing || !acknowledged || (mode === 'url' && !url) || (mode === 'file' && !file)}
|
||||
disabled={isProcessing || !acknowledged || (mode === 'url' && !url) || (mode === 'file' && (!file || fileTooShort))}
|
||||
className="w-full btn-primary mt-4"
|
||||
>
|
||||
{isProcessing ? (
|
||||
|
|
|
|||
Loading…
Reference in a new issue