TimelapseEditorModal.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. className="w-full rounded-lg bg-black"
  303. onClick={togglePlay}
  304. muted={!!audioUrl}
  305. />
  306. {/* Play overlay */}
  307. {!isPlaying && (
  308. <button
  309. onClick={togglePlay}
  310. className="absolute inset-0 flex items-center justify-center bg-black/30 hover:bg-black/40 transition-colors"
  311. >
  312. <div className="p-4 bg-bambu-green rounded-full">
  313. <Play className="w-8 h-8 text-white" />
  314. </div>
  315. </button>
  316. )}
  317. {/* Hidden audio element for music overlay preview */}
  318. {audioUrl && (
  319. <audio ref={audioRef} src={audioUrl} loop />
  320. )}
  321. </div>
  322. {/* Timeline with Thumbnails */}
  323. <div className="space-y-2">
  324. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  325. <Scissors className="w-4 h-4" />
  326. <span>Trim</span>
  327. <span className="ml-auto">
  328. {formatMediaTime(trimStart)} - {formatMediaTime(trimEnd)} ({formatMediaTime(trimmedDuration)})
  329. </span>
  330. </div>
  331. {/* Thumbnail strip */}
  332. <div className="relative h-16 bg-bambu-dark rounded-lg overflow-hidden">
  333. {/* Thumbnails background */}
  334. <div className="absolute inset-0 flex">
  335. {thumbnailData?.thumbnails.map((thumb, i) => (
  336. <div
  337. key={i}
  338. className="flex-1 bg-cover bg-center"
  339. style={{
  340. backgroundImage: `url(data:image/jpeg;base64,${thumb})`,
  341. }}
  342. />
  343. ))}
  344. </div>
  345. {/* Trim overlay - grayed out areas */}
  346. <div
  347. className="absolute inset-y-0 left-0 bg-black/60"
  348. style={{ width: `${(trimStart / duration) * 100}%` }}
  349. />
  350. <div
  351. className="absolute inset-y-0 right-0 bg-black/60"
  352. style={{ width: `${((duration - trimEnd) / duration) * 100}%` }}
  353. />
  354. {/* Selected region border */}
  355. <div
  356. className="absolute inset-y-0 border-2 border-bambu-green"
  357. style={{
  358. left: `${(trimStart / duration) * 100}%`,
  359. right: `${((duration - trimEnd) / duration) * 100}%`,
  360. }}
  361. />
  362. {/* Current time indicator */}
  363. <div
  364. className="absolute top-0 bottom-0 w-0.5 bg-white shadow-lg"
  365. style={{ left: `${(currentTime / duration) * 100}%` }}
  366. />
  367. {/* Trim handles */}
  368. <input
  369. type="range"
  370. min={0}
  371. max={duration}
  372. step={0.1}
  373. value={trimStart}
  374. onChange={(e) => {
  375. const val = parseFloat(e.target.value);
  376. if (val < trimEnd - 1) {
  377. setTrimStart(val);
  378. if (videoRef.current && videoRef.current.currentTime < val) {
  379. videoRef.current.currentTime = val;
  380. }
  381. }
  382. }}
  383. className="absolute inset-0 w-full opacity-0 cursor-ew-resize"
  384. style={{ clipPath: 'inset(0 50% 0 0)' }}
  385. />
  386. <input
  387. type="range"
  388. min={0}
  389. max={duration}
  390. step={0.1}
  391. value={trimEnd}
  392. onChange={(e) => {
  393. const val = parseFloat(e.target.value);
  394. if (val > trimStart + 1) {
  395. setTrimEnd(val);
  396. }
  397. }}
  398. className="absolute inset-0 w-full opacity-0 cursor-ew-resize"
  399. style={{ clipPath: 'inset(0 0 0 50%)' }}
  400. />
  401. </div>
  402. {/* Playback scrubber */}
  403. <input
  404. type="range"
  405. min={0}
  406. max={duration}
  407. step={0.1}
  408. value={currentTime}
  409. onChange={(e) => handleSeek(parseFloat(e.target.value))}
  410. className="w-full h-1 bg-bambu-dark-tertiary rounded-lg appearance-none cursor-pointer
  411. [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3
  412. [&::-webkit-slider-thumb]:bg-bambu-green [&::-webkit-slider-thumb]:rounded-full
  413. [&::-webkit-slider-thumb]:cursor-pointer"
  414. />
  415. {/* Play controls */}
  416. <div className="flex items-center justify-center gap-2">
  417. <button
  418. onClick={togglePlay}
  419. className="p-2 bg-bambu-green hover:bg-bambu-green-dark rounded-lg transition-colors"
  420. >
  421. {isPlaying ? (
  422. <Pause className="w-5 h-5 text-white" />
  423. ) : (
  424. <Play className="w-5 h-5 text-white" />
  425. )}
  426. </button>
  427. </div>
  428. </div>
  429. {/* Speed Control */}
  430. <div className="space-y-2">
  431. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  432. <Gauge className="w-4 h-4" />
  433. <span>Speed</span>
  434. <span className="ml-auto">{speed}x (output: {formatMediaTime(outputDuration)})</span>
  435. </div>
  436. <div className="flex gap-1">
  437. {SPEED_OPTIONS.map((s) => (
  438. <button
  439. key={s}
  440. onClick={() => setSpeed(s)}
  441. className={`flex-1 px-2 py-2 text-sm rounded transition-colors ${
  442. speed === s
  443. ? 'bg-bambu-green text-white'
  444. : 'bg-bambu-dark text-bambu-gray hover:bg-bambu-dark-tertiary'
  445. }`}
  446. >
  447. {s}x
  448. </button>
  449. ))}
  450. </div>
  451. </div>
  452. {/* Audio Upload */}
  453. <div className="space-y-2">
  454. <div className="flex items-center gap-2 text-sm text-bambu-gray">
  455. <Music className="w-4 h-4" />
  456. <span>Music Overlay</span>
  457. </div>
  458. {audioFile ? (
  459. <div className="flex items-center gap-3 p-3 bg-bambu-dark rounded-lg">
  460. <Music className="w-5 h-5 text-bambu-green" />
  461. <div className="flex-1 min-w-0">
  462. <p className="text-sm text-white truncate">{audioFile.name}</p>
  463. <p className="text-xs text-bambu-gray">
  464. {(audioFile.size / 1024 / 1024).toFixed(1)} MB
  465. </p>
  466. </div>
  467. {/* Volume control */}
  468. <button
  469. onClick={() => setAudioMuted(!audioMuted)}
  470. className="p-2 hover:bg-bambu-dark-tertiary rounded transition-colors"
  471. >
  472. {audioMuted ? (
  473. <VolumeX className="w-4 h-4 text-bambu-gray" />
  474. ) : (
  475. <Volume2 className="w-4 h-4 text-bambu-green" />
  476. )}
  477. </button>
  478. <input
  479. type="range"
  480. min={0}
  481. max={1}
  482. step={0.1}
  483. value={audioVolume}
  484. onChange={(e) => setAudioVolume(parseFloat(e.target.value))}
  485. className="w-20 h-1 bg-bambu-dark-tertiary rounded-lg appearance-none cursor-pointer
  486. [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3
  487. [&::-webkit-slider-thumb]:bg-bambu-green [&::-webkit-slider-thumb]:rounded-full"
  488. />
  489. <button
  490. onClick={removeAudio}
  491. className="p-2 hover:bg-red-100 dark:hover:bg-red-500/20 rounded transition-colors"
  492. >
  493. <Trash2 className="w-4 h-4 text-red-600 dark:text-red-400" />
  494. </button>
  495. </div>
  496. ) : (
  497. <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">
  498. <Upload className="w-8 h-8 text-bambu-gray" />
  499. <span className="text-sm text-bambu-gray">
  500. Drop audio file or click to upload
  501. </span>
  502. <span className="text-xs text-bambu-gray/60">
  503. MP3, WAV, M4A, AAC, OGG
  504. </span>
  505. <input
  506. type="file"
  507. accept=".mp3,.wav,.m4a,.aac,.ogg,audio/*"
  508. onChange={handleAudioUpload}
  509. className="hidden"
  510. />
  511. </label>
  512. )}
  513. </div>
  514. {/* Summary */}
  515. <div className="p-3 bg-bambu-dark rounded-lg text-sm space-y-1">
  516. <p className="text-bambu-gray">
  517. <span className="text-white">Original:</span> {formatMediaTime(duration)} @ {videoInfo?.width}x{videoInfo?.height}
  518. </p>
  519. <p className="text-bambu-gray">
  520. <span className="text-white">Output:</span> {formatMediaTime(outputDuration)} @ {speed}x speed
  521. {audioFile && ` + music overlay`}
  522. </p>
  523. </div>
  524. </div>
  525. {/* Processing overlay */}
  526. {processMutation.isPending && (
  527. <div className="absolute inset-0 bg-black/80 flex flex-col items-center justify-center gap-4">
  528. <Loader2 className="w-12 h-12 text-bambu-green animate-spin" />
  529. <p className="text-white text-lg">Processing timelapse...</p>
  530. <p className="text-bambu-gray text-sm">This may take a few moments</p>
  531. </div>
  532. )}
  533. </div>
  534. </div>
  535. );
  536. }