Spaces:
Runtime error
Runtime error
File size: 6,775 Bytes
c7fbc2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import { useState, useRef, useEffect } from "react";
import { X, Image as ImageIcon, Check } from "lucide-react";
interface CoverFrameSelectorProps {
isOpen: boolean;
onClose: () => void;
videoFile: File | string | null;
onSave: (file: File) => void;
}
export default function CoverFrameSelector({ isOpen, onClose, videoFile, onSave }: CoverFrameSelectorProps) {
const videoRef = useRef<HTMLVideoElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
// Reset when opening with a new file
useEffect(() => {
let objectUrl: string | null = null;
if (isOpen && videoFile && videoRef.current) {
if (typeof videoFile === "string") {
videoRef.current.src = videoFile;
// If it's a remote URL, we must set crossOrigin to anonymous to allow canvas capture
videoRef.current.crossOrigin = "anonymous";
} else {
objectUrl = URL.createObjectURL(videoFile);
videoRef.current.src = objectUrl;
videoRef.current.removeAttribute("crossorigin");
}
videoRef.current.currentTime = 0;
}
return () => {
if (objectUrl) URL.revokeObjectURL(objectUrl);
};
}, [isOpen, videoFile]);
const handleTimeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const time = parseFloat(e.target.value);
setCurrentTime(time);
if (videoRef.current) {
videoRef.current.currentTime = time;
}
};
const handleLoadedMetadata = () => {
if (videoRef.current) {
setDuration(videoRef.current.duration);
}
};
const handleSaveFrame = () => {
if (!videoRef.current || !canvasRef.current) return;
const video = videoRef.current;
const canvas = canvasRef.current;
// Set canvas to match video dimensions
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Draw current frame
const ctx = canvas.getContext("2d");
ctx?.drawImage(video, 0, 0, canvas.width, canvas.height);
// Convert to Blob/File
canvas.toBlob((blob) => {
if (blob) {
const file = new File([blob], "cover_frame.jpg", { type: "image/jpeg" });
onSave(file);
onClose();
}
}, "image/jpeg", 0.95);
};
const handleCustomUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files[0]) {
onSave(e.target.files[0]);
onClose();
}
};
if (!isOpen || !videoFile) return null;
return (
<div className="fixed inset-0 z-[100] flex items-center justify-center bg-black/80 backdrop-blur-sm p-4">
<div className="bg-white dark:bg-zinc-900 w-full max-w-lg rounded-2xl shadow-2xl overflow-hidden animate-in fade-in zoom-in-95 duration-200">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-gray-100 dark:border-zinc-800">
<h3 className="font-bold text-gray-900 dark:text-white">Set Cover Frame</h3>
<button onClick={onClose} className="p-1 rounded-full hover:bg-gray-100 dark:hover:bg-zinc-800 transition-colors">
<X size={20} className="text-gray-500" />
</button>
</div>
{/* Content */}
<div className="p-6">
{/* Video Viewer */}
<div className="relative aspect-[9/16] bg-black rounded-lg overflow-hidden shadow-inner mb-6 mx-auto max-h-[60vh] w-auto">
<video
ref={videoRef}
className="w-full h-full object-contain"
onLoadedMetadata={handleLoadedMetadata}
playsInline
muted // Muted to avoid random audio blasts while seeking
/>
{/* Hidden Canvas for capture */}
<canvas ref={canvasRef} className="hidden" />
</div>
{/* Scrubber */}
<div className="mb-6">
<label className="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-2 block">
Select Frame ({currentTime.toFixed(1)}s / {duration.toFixed(1)}s)
</label>
<input
type="range"
min={0}
max={duration}
step={0.1}
value={currentTime}
onChange={handleTimeChange}
className="w-full accent-green-500 h-2 bg-gray-200 dark:bg-zinc-700 rounded-lg appearance-none cursor-pointer"
/>
</div>
{/* Actions */}
<div className="flex gap-3">
<div className="flex-1">
<input
type="file"
id="custom-cover-upload"
className="hidden"
accept="image/*"
onChange={handleCustomUpload}
/>
<label
htmlFor="custom-cover-upload"
className="flex items-center justify-center gap-2 w-full py-2.5 border border-gray-200 dark:border-zinc-700 rounded-xl font-semibold text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-zinc-800 transition-all cursor-pointer text-sm"
>
<ImageIcon size={18} />
Upload Image
</label>
</div>
<button
type="button"
onClick={handleSaveFrame}
className="flex-1 flex items-center justify-center gap-2 py-2.5 bg-green-500 hover:bg-green-600 text-white rounded-xl font-semibold shadow-sm transition-all text-sm"
>
<Check size={18} />
Use This Frame
</button>
</div>
</div>
</div>
</div>
);
}
|