TimelapseEditorModal.tsx 20 KB

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