TimelapseEditorModal.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. import { useState, useRef, useEffect, useCallback } from 'react';
  2. import { useQuery, useMutation } from '@tanstack/react-query';
  3. import {
  4. X,
  5. Save,
  6. Film,
  7. Play,
  8. Pause,
  9. Scissors,
  10. Gauge,
  11. Music,
  12. Upload,
  13. Trash2,
  14. Volume2,
  15. VolumeX,
  16. Loader2,
  17. } from 'lucide-react';
  18. import { Button } from './Button';
  19. import { api } from '../api/client';
  20. import { useToast } from '../contexts/ToastContext';
  21. interface TimelapseEditorModalProps {
  22. archiveId: number;
  23. timelapseSrc: string;
  24. onClose: () => void;
  25. onSave?: () => void;
  26. }
  27. const SPEED_OPTIONS = [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4];
  28. function formatTime(seconds: number): string {
  29. const mins = Math.floor(seconds / 60);
  30. const secs = Math.floor(seconds % 60);
  31. return `${mins}:${secs.toString().padStart(2, '0')}`;
  32. }
  33. export function TimelapseEditorModal({
  34. archiveId,
  35. timelapseSrc,
  36. onClose,
  37. onSave,
  38. }: TimelapseEditorModalProps) {
  39. const { showToast } = useToast();
  40. const videoRef = useRef<HTMLVideoElement>(null);
  41. const audioRef = useRef<HTMLAudioElement>(null);
  42. // Video state
  43. const [isPlaying, setIsPlaying] = useState(false);
  44. const [currentTime, setCurrentTime] = useState(0);
  45. const [duration, setDuration] = useState(0);
  46. // Editor state
  47. const [trimStart, setTrimStart] = useState(0);
  48. const [trimEnd, setTrimEnd] = useState(0);
  49. const [speed, setSpeed] = useState(1);
  50. const [audioFile, setAudioFile] = useState<File | null>(null);
  51. const [audioUrl, setAudioUrl] = useState<string | null>(null);
  52. const [audioVolume, setAudioVolume] = useState(0.8);
  53. const [audioMuted, setAudioMuted] = useState(false);
  54. // Fetch video info
  55. const { data: videoInfo, isLoading: isLoadingInfo } = useQuery({
  56. queryKey: ['timelapse-info', archiveId],
  57. queryFn: () => api.getTimelapseInfo(archiveId),
  58. });
  59. // Fetch thumbnails
  60. const { data: thumbnailData } = useQuery({
  61. queryKey: ['timelapse-thumbnails', archiveId],
  62. queryFn: () => api.getTimelapseThumbnails(archiveId, 15),
  63. });
  64. // Process mutation
  65. const processMutation = useMutation({
  66. mutationFn: () =>
  67. api.processTimelapse(
  68. archiveId,
  69. {
  70. trimStart,
  71. trimEnd,
  72. speed,
  73. saveMode: 'replace',
  74. },
  75. audioFile || undefined
  76. ),
  77. onSuccess: (data) => {
  78. showToast(data.message, 'success');
  79. onSave?.();
  80. onClose();
  81. },
  82. onError: (error: Error) => {
  83. showToast(error.message || 'Processing failed', 'error');
  84. },
  85. });
  86. // Initialize trimEnd when duration is available
  87. useEffect(() => {
  88. if (videoInfo?.duration && trimEnd === 0) {
  89. setTrimEnd(videoInfo.duration);
  90. }
  91. }, [videoInfo?.duration, trimEnd]);
  92. // Close on Escape
  93. useEffect(() => {
  94. const handleKeyDown = (e: KeyboardEvent) => {
  95. if (e.key === 'Escape') {
  96. onClose();
  97. }
  98. };
  99. window.addEventListener('keydown', handleKeyDown);
  100. return () => window.removeEventListener('keydown', handleKeyDown);
  101. }, [onClose]);
  102. // Video event handlers
  103. useEffect(() => {
  104. const video = videoRef.current;
  105. if (!video) return;
  106. const handleTimeUpdate = () => {
  107. const time = video.currentTime;
  108. setCurrentTime(time);
  109. // Loop within trim region
  110. if (time >= trimEnd) {
  111. video.currentTime = trimStart;
  112. }
  113. };
  114. const handleDurationChange = () => {
  115. setDuration(video.duration);
  116. if (trimEnd === 0) {
  117. setTrimEnd(video.duration);
  118. }
  119. };
  120. const handlePlay = () => setIsPlaying(true);
  121. const handlePause = () => setIsPlaying(false);
  122. video.addEventListener('timeupdate', handleTimeUpdate);
  123. video.addEventListener('durationchange', handleDurationChange);
  124. video.addEventListener('play', handlePlay);
  125. video.addEventListener('pause', handlePause);
  126. return () => {
  127. video.removeEventListener('timeupdate', handleTimeUpdate);
  128. video.removeEventListener('durationchange', handleDurationChange);
  129. video.removeEventListener('play', handlePlay);
  130. video.removeEventListener('pause', handlePause);
  131. };
  132. }, [trimStart, trimEnd]);
  133. // Sync audio with video
  134. useEffect(() => {
  135. const audio = audioRef.current;
  136. const video = videoRef.current;
  137. if (!audio || !video || !audioUrl) return;
  138. audio.currentTime = video.currentTime;
  139. audio.playbackRate = video.playbackRate;
  140. if (isPlaying && !audioMuted) {
  141. audio.play().catch(() => {});
  142. } else {
  143. audio.pause();
  144. }
  145. }, [isPlaying, audioUrl, audioMuted]);
  146. // Update audio volume
  147. useEffect(() => {
  148. if (audioRef.current) {
  149. audioRef.current.volume = audioMuted ? 0 : audioVolume;
  150. }
  151. }, [audioVolume, audioMuted]);
  152. // Update playback rate
  153. useEffect(() => {
  154. if (videoRef.current) {
  155. videoRef.current.playbackRate = speed;
  156. }
  157. if (audioRef.current) {
  158. audioRef.current.playbackRate = speed;
  159. }
  160. }, [speed]);
  161. const togglePlay = useCallback(() => {
  162. const video = videoRef.current;
  163. if (!video) return;
  164. if (isPlaying) {
  165. video.pause();
  166. } else {
  167. // Start from trim start if before it
  168. if (video.currentTime < trimStart) {
  169. video.currentTime = trimStart;
  170. }
  171. video.play();
  172. }
  173. }, [isPlaying, trimStart]);
  174. const handleSeek = (time: number) => {
  175. const video = videoRef.current;
  176. if (!video) return;
  177. video.currentTime = Math.max(trimStart, Math.min(trimEnd, time));
  178. };
  179. const handleAudioUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
  180. const file = e.target.files?.[0];
  181. if (!file) return;
  182. // Cleanup previous URL
  183. if (audioUrl) {
  184. URL.revokeObjectURL(audioUrl);
  185. }
  186. setAudioFile(file);
  187. setAudioUrl(URL.createObjectURL(file));
  188. };
  189. const removeAudio = () => {
  190. if (audioUrl) {
  191. URL.revokeObjectURL(audioUrl);
  192. }
  193. setAudioFile(null);
  194. setAudioUrl(null);
  195. };
  196. // Cleanup on unmount
  197. useEffect(() => {
  198. return () => {
  199. if (audioUrl) {
  200. URL.revokeObjectURL(audioUrl);
  201. }
  202. };
  203. }, [audioUrl]);
  204. const trimmedDuration = trimEnd - trimStart;
  205. const outputDuration = trimmedDuration / speed;
  206. if (isLoadingInfo) {
  207. return (
  208. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/80">
  209. <div className="flex items-center gap-3 text-white">
  210. <Loader2 className="w-6 h-6 animate-spin" />
  211. Loading video info...
  212. </div>
  213. </div>
  214. );
  215. }
  216. return (
  217. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/80">
  218. <div className="relative bg-bambu-dark-secondary rounded-xl max-w-5xl w-full mx-4 max-h-[90vh] overflow-hidden flex flex-col">
  219. {/* Header */}
  220. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0">
  221. <h3 className="text-lg font-semibold text-white flex items-center gap-2">
  222. <Film className="w-5 h-5 text-bambu-green" />
  223. Edit Timelapse
  224. </h3>
  225. <div className="flex items-center gap-2">
  226. <Button
  227. variant="primary"
  228. size="sm"
  229. onClick={() => processMutation.mutate()}
  230. disabled={processMutation.isPending}
  231. >
  232. {processMutation.isPending ? (
  233. <>
  234. <Loader2 className="w-4 h-4 animate-spin" />
  235. Processing...
  236. </>
  237. ) : (
  238. <>
  239. <Save className="w-4 h-4" />
  240. Save
  241. </>
  242. )}
  243. </Button>
  244. <button
  245. onClick={onClose}
  246. className="p-1 hover:bg-bambu-dark-tertiary rounded transition-colors"
  247. >
  248. <X className="w-5 h-5 text-bambu-gray" />
  249. </button>
  250. </div>
  251. </div>
  252. {/* Content */}
  253. <div className="flex-1 overflow-y-auto p-4 space-y-4">
  254. {/* Video Preview */}
  255. <div className="relative">
  256. <video
  257. ref={videoRef}
  258. src={timelapseSrc}
  259. className="w-full rounded-lg bg-black"
  260. onClick={togglePlay}
  261. muted={!!audioUrl}
  262. />
  263. {/* Play overlay */}
  264. {!isPlaying && (
  265. <button
  266. onClick={togglePlay}
  267. className="absolute inset-0 flex items-center justify-center bg-black/30 hover:bg-black/40 transition-colors"
  268. >
  269. <div className="p-4 bg-bambu-green rounded-full">
  270. <Play className="w-8 h-8 text-white" />
  271. </div>
  272. </button>
  273. )}
  274. {/* Hidden audio element for music overlay preview */}
  275. {audioUrl && (
  276. <audio ref={audioRef} src={audioUrl} loop />
  277. )}
  278. </div>
  279. {/* Timeline with Thumbnails */}
  280. <div className="space-y-2">
  281. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  282. <Scissors className="w-4 h-4" />
  283. <span>Trim</span>
  284. <span className="ml-auto">
  285. {formatTime(trimStart)} - {formatTime(trimEnd)} ({formatTime(trimmedDuration)})
  286. </span>
  287. </div>
  288. {/* Thumbnail strip */}
  289. <div className="relative h-16 bg-bambu-dark rounded-lg overflow-hidden">
  290. {/* Thumbnails background */}
  291. <div className="absolute inset-0 flex">
  292. {thumbnailData?.thumbnails.map((thumb, i) => (
  293. <div
  294. key={i}
  295. className="flex-1 bg-cover bg-center"
  296. style={{
  297. backgroundImage: `url(data:image/jpeg;base64,${thumb})`,
  298. }}
  299. />
  300. ))}
  301. </div>
  302. {/* Trim overlay - grayed out areas */}
  303. <div
  304. className="absolute inset-y-0 left-0 bg-black/60"
  305. style={{ width: `${(trimStart / duration) * 100}%` }}
  306. />
  307. <div
  308. className="absolute inset-y-0 right-0 bg-black/60"
  309. style={{ width: `${((duration - trimEnd) / duration) * 100}%` }}
  310. />
  311. {/* Selected region border */}
  312. <div
  313. className="absolute inset-y-0 border-2 border-bambu-green"
  314. style={{
  315. left: `${(trimStart / duration) * 100}%`,
  316. right: `${((duration - trimEnd) / duration) * 100}%`,
  317. }}
  318. />
  319. {/* Current time indicator */}
  320. <div
  321. className="absolute top-0 bottom-0 w-0.5 bg-white shadow-lg"
  322. style={{ left: `${(currentTime / duration) * 100}%` }}
  323. />
  324. {/* Trim handles */}
  325. <input
  326. type="range"
  327. min={0}
  328. max={duration}
  329. step={0.1}
  330. value={trimStart}
  331. onChange={(e) => {
  332. const val = parseFloat(e.target.value);
  333. if (val < trimEnd - 1) {
  334. setTrimStart(val);
  335. if (videoRef.current && videoRef.current.currentTime < val) {
  336. videoRef.current.currentTime = val;
  337. }
  338. }
  339. }}
  340. className="absolute inset-0 w-full opacity-0 cursor-ew-resize"
  341. style={{ clipPath: 'inset(0 50% 0 0)' }}
  342. />
  343. <input
  344. type="range"
  345. min={0}
  346. max={duration}
  347. step={0.1}
  348. value={trimEnd}
  349. onChange={(e) => {
  350. const val = parseFloat(e.target.value);
  351. if (val > trimStart + 1) {
  352. setTrimEnd(val);
  353. }
  354. }}
  355. className="absolute inset-0 w-full opacity-0 cursor-ew-resize"
  356. style={{ clipPath: 'inset(0 0 0 50%)' }}
  357. />
  358. </div>
  359. {/* Playback scrubber */}
  360. <input
  361. type="range"
  362. min={0}
  363. max={duration}
  364. step={0.1}
  365. value={currentTime}
  366. onChange={(e) => handleSeek(parseFloat(e.target.value))}
  367. className="w-full h-1 bg-bambu-dark-tertiary rounded-lg appearance-none cursor-pointer
  368. [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3
  369. [&::-webkit-slider-thumb]:bg-bambu-green [&::-webkit-slider-thumb]:rounded-full
  370. [&::-webkit-slider-thumb]:cursor-pointer"
  371. />
  372. {/* Play controls */}
  373. <div className="flex items-center justify-center gap-2">
  374. <button
  375. onClick={togglePlay}
  376. className="p-2 bg-bambu-green hover:bg-bambu-green-dark rounded-lg transition-colors"
  377. >
  378. {isPlaying ? (
  379. <Pause className="w-5 h-5 text-white" />
  380. ) : (
  381. <Play className="w-5 h-5 text-white" />
  382. )}
  383. </button>
  384. </div>
  385. </div>
  386. {/* Speed Control */}
  387. <div className="space-y-2">
  388. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  389. <Gauge className="w-4 h-4" />
  390. <span>Speed</span>
  391. <span className="ml-auto">{speed}x (output: {formatTime(outputDuration)})</span>
  392. </div>
  393. <div className="flex gap-1">
  394. {SPEED_OPTIONS.map((s) => (
  395. <button
  396. key={s}
  397. onClick={() => setSpeed(s)}
  398. className={`flex-1 px-2 py-2 text-sm rounded transition-colors ${
  399. speed === s
  400. ? 'bg-bambu-green text-white'
  401. : 'bg-bambu-dark text-bambu-gray hover:bg-bambu-dark-tertiary'
  402. }`}
  403. >
  404. {s}x
  405. </button>
  406. ))}
  407. </div>
  408. </div>
  409. {/* Audio Upload */}
  410. <div className="space-y-2">
  411. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  412. <Music className="w-4 h-4" />
  413. <span>Music Overlay</span>
  414. </div>
  415. {audioFile ? (
  416. <div className="flex items-center gap-3 p-3 bg-bambu-dark rounded-lg">
  417. <Music className="w-5 h-5 text-bambu-green" />
  418. <div className="flex-1 min-w-0">
  419. <p className="text-sm text-white truncate">{audioFile.name}</p>
  420. <p className="text-xs text-bambu-gray">
  421. {(audioFile.size / 1024 / 1024).toFixed(1)} MB
  422. </p>
  423. </div>
  424. {/* Volume control */}
  425. <button
  426. onClick={() => setAudioMuted(!audioMuted)}
  427. className="p-2 hover:bg-bambu-dark-tertiary rounded transition-colors"
  428. >
  429. {audioMuted ? (
  430. <VolumeX className="w-4 h-4 text-bambu-gray" />
  431. ) : (
  432. <Volume2 className="w-4 h-4 text-bambu-green" />
  433. )}
  434. </button>
  435. <input
  436. type="range"
  437. min={0}
  438. max={1}
  439. step={0.1}
  440. value={audioVolume}
  441. onChange={(e) => setAudioVolume(parseFloat(e.target.value))}
  442. className="w-20 h-1 bg-bambu-dark-tertiary rounded-lg appearance-none cursor-pointer
  443. [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3
  444. [&::-webkit-slider-thumb]:bg-bambu-green [&::-webkit-slider-thumb]:rounded-full"
  445. />
  446. <button
  447. onClick={removeAudio}
  448. className="p-2 hover:bg-red-500/20 rounded transition-colors"
  449. >
  450. <Trash2 className="w-4 h-4 text-red-400" />
  451. </button>
  452. </div>
  453. ) : (
  454. <label className="flex flex-col items-center justify-center gap-2 p-6 border-2 border-dashed border-bambu-dark-tertiary rounded-lg cursor-pointer hover:border-bambu-green/50 transition-colors">
  455. <Upload className="w-8 h-8 text-bambu-gray" />
  456. <span className="text-sm text-bambu-gray">
  457. Drop audio file or click to upload
  458. </span>
  459. <span className="text-xs text-bambu-gray/60">
  460. MP3, WAV, M4A, AAC, OGG
  461. </span>
  462. <input
  463. type="file"
  464. accept=".mp3,.wav,.m4a,.aac,.ogg,audio/*"
  465. onChange={handleAudioUpload}
  466. className="hidden"
  467. />
  468. </label>
  469. )}
  470. </div>
  471. {/* Summary */}
  472. <div className="p-3 bg-bambu-dark rounded-lg text-sm space-y-1">
  473. <p className="text-bambu-gray">
  474. <span className="text-white">Original:</span> {formatTime(duration)} @ {videoInfo?.width}x{videoInfo?.height}
  475. </p>
  476. <p className="text-bambu-gray">
  477. <span className="text-white">Output:</span> {formatTime(outputDuration)} @ {speed}x speed
  478. {audioFile && ` + music overlay`}
  479. </p>
  480. </div>
  481. </div>
  482. {/* Processing overlay */}
  483. {processMutation.isPending && (
  484. <div className="absolute inset-0 bg-black/80 flex flex-col items-center justify-center gap-4">
  485. <Loader2 className="w-12 h-12 text-bambu-green animate-spin" />
  486. <p className="text-white text-lg">Processing timelapse...</p>
  487. <p className="text-bambu-gray text-sm">This may take a few moments</p>
  488. </div>
  489. )}
  490. </div>
  491. </div>
  492. );
  493. }