diff --git a/README.md b/README.md index f243225..b5ff32f 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ A browser-first AI video editor for image/video timelines, AI voiceover generati - Move sticker segments on the timeline and render sticker overlays in preview/export. - Use multi-track controls: visibility, lock, delete, duplicate, split, zoom, snap, and reorder. - Apply filters, visual effects, stickers, and transition presets. +- Detect the primary subject in an image or scan a complete video into a timestamped YOLOS tiny subject track in a browser worker. +- Generate transparent portrait cutouts with MODNet; videos are pre-analyzed into a full-duration temporal mask track used by preview and export. +- Use normalized subject geometry for smart caption avoidance and subject-aware cropping across aspect ratios. - Export MP4 when browser support is available, with WebM fallback. - Show export progress during browser-side rendering. - Persist language selection in `localStorage`. @@ -48,6 +51,7 @@ A browser-first AI video editor for image/video timelines, AI voiceover generati - Added timeline-native sticker overlays with generated PNG sticker assets, automatic sticker-track creation, movable sticker clips, and a cleaner scrollbar-free sticker picker. - Added installable PWA metadata, app icons, service-worker app shell/model caching, and cached sticker assets for faster repeat sessions. - Added browser-side automatic caption groundwork using Whisper ONNX workers, plus timeline snapping/alignment improvements for captions, source audio, and generated voiceover. +- Added lazy-loaded YOLOS tiny and MODNet vision workers with subject overlays, portrait matting, caption avoidance, and preview/export-consistent smart crop geometry. ## AI Features @@ -61,6 +65,9 @@ Current AI capabilities are designed to run in the browser as much as possible: - Script-to-caption timeline alignment based on generated audio duration. - Browser voice recording for manually captured narration. - Local waveform decoding for voiceover, source audio, and background music. +- YOLOS tiny q8 ONNX subject detection and MODNet q8 ONNX portrait matting with revision-pinned, service-worker-cached model assets. + +MODNet is portrait-oriented, while YOLOS tiny covers common COCO categories. Images support the full matting path. Videos are pre-analyzed across their full duration before export, then resolve timestamped YOLOS geometry and MODNet masks for preview, caption avoidance, smart crop, and every rendered export time instead of freezing the first-frame result. Long videos automatically use a wider temporal sampling interval to keep WASM inference and memory bounded. ## Multilingual UI @@ -94,7 +101,7 @@ Planned or desirable AI capabilities: - Automatic highlight extraction from long videos. - AI background music recommendation and beat matching. - Noise reduction, loudness normalization, and vocal enhancement. -- AI image/video enhancement, background removal, and object segmentation. +- General-purpose product/animal segmentation and offline per-frame video background removal. - Cloud-assisted model loading for larger models while keeping an offline/local-first mode. - Batch export and template-based video generation. @@ -130,7 +137,10 @@ src/ timeline.js timelineScale.js ttsText.js + vision.js + visualGeometry.js workers/ + vision.worker.js i18n.js main.jsx styles.css diff --git a/README.zh-CN.md b/README.zh-CN.md index 629bb00..ee729bd 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -36,6 +36,9 @@ - 支持独立背景音乐轨。 - 支持多轨道控制:显示/隐藏、锁定、删除、复制、切分、缩放、磁吸和重排。 - 支持滤镜、视觉效果、贴纸和转场预设。 +- 使用 YOLOS tiny 在浏览器 Worker 中识别图片主体,或扫描整段视频生成带时间戳的主体轨迹。 +- 使用 MODNet 为图片生成透明人像抠图;视频会先生成覆盖全片的时序遮罩,再用于播放与导出。 +- 基于归一化主体框自动做字幕避让和跨画幅智能裁切。 - 浏览器支持时导出 MP4,不支持时回退到 WebM。 - 本地渲染导出时显示导出进度。 - 首次语言选择会保存到 `localStorage`。 @@ -51,6 +54,9 @@ - 根据生成音频时长对齐文案字幕时间线。 - 浏览器录音,可手动录制旁白并写入配音轨。 - 本地解析配音、视频原声、背景音乐波形。 +- YOLOS tiny q8 ONNX 主体检测与 MODNet q8 ONNX 人像抠图,模型按需加载并由 Service Worker 缓存。 + +MODNet 主要面向人像抠图,YOLOS tiny 覆盖 COCO 常见类别。图片支持完整抠图;视频采用导出前全片预分析,按时间解析 YOLOS 主体轨迹与 MODNet 遮罩,因此预览、字幕避让、智能裁切和导出不再静态复用首帧。为控制浏览器内存与 WASM 推理耗时,长视频会自动降低时序采样密度并在相邻主体帧之间插值。 ## 多语言界面 @@ -85,7 +91,7 @@ - 长视频自动高光提取。 - AI 背景音乐推荐和节拍匹配。 - 降噪、响度标准化和人声增强。 -- 图片/视频增强、背景移除和主体分割。 +- 更通用的商品/动物分割与视频逐帧背景移除。 - 在保留离线/本地优先模式的同时,支持云端辅助加载更大模型。 - 批量导出和基于模板的视频生成。 @@ -116,6 +122,10 @@ src/ lib/ media.js timeline.js + vision.js + visualGeometry.js + workers/ + vision.worker.js i18n.js main.jsx styles.css diff --git a/package-lock.json b/package-lock.json index e265b33..2b1b5f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@huggingface/transformers": "^3.8.1", "@phosphor-icons/react": "^2.1.10", "@vitejs/plugin-react": "5.0.4", + "fflate": "^0.8.3", "kokoro-js": "^1.2.1", "react": "19.2.0", "react-dom": "19.2.0", @@ -795,6 +796,26 @@ "sharp": "^0.34.1" } }, + "node_modules/@huggingface/transformers/node_modules/onnxruntime-common": { + "version": "1.22.0-dev.20250409-89f8206ba4", + "resolved": "https://registry.npmmirror.com/onnxruntime-common/-/onnxruntime-common-1.22.0-dev.20250409-89f8206ba4.tgz", + "integrity": "sha512-vDJMkfCfb0b1A836rgHj+ORuZf4B4+cc2bASQtpeoJLueuFc5DuYwjIZUBrSvx/fO5IrLjLz+oTrB3pcGlhovQ==", + "license": "MIT" + }, + "node_modules/@huggingface/transformers/node_modules/onnxruntime-web": { + "version": "1.22.0-dev.20250409-89f8206ba4", + "resolved": "https://registry.npmmirror.com/onnxruntime-web/-/onnxruntime-web-1.22.0-dev.20250409-89f8206ba4.tgz", + "integrity": "sha512-0uS76OPgH0hWCPrFKlL8kYVV7ckM7t/36HfbgoFw6Nd0CZVVbQC4PkrR8mBX8LtNUFZO25IQBqV2Hx2ho3FlbQ==", + "license": "MIT", + "dependencies": { + "flatbuffers": "^25.1.24", + "guid-typescript": "^1.0.9", + "long": "^5.2.3", + "onnxruntime-common": "1.22.0-dev.20250409-89f8206ba4", + "platform": "^1.3.6", + "protobufjs": "^7.2.4" + } + }, "node_modules/@img/colour": { "version": "1.1.0", "resolved": "https://registry.npmmirror.com/@img/colour/-/colour-1.1.0.tgz", @@ -2056,6 +2077,12 @@ } } }, + "node_modules/fflate": { + "version": "0.8.3", + "resolved": "https://registry.npmmirror.com/fflate/-/fflate-0.8.3.tgz", + "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", + "license": "MIT" + }, "node_modules/flatbuffers": { "version": "25.9.23", "resolved": "https://registry.npmmirror.com/flatbuffers/-/flatbuffers-25.9.23.tgz", @@ -2319,26 +2346,6 @@ "tar": "^7.0.1" } }, - "node_modules/onnxruntime-web": { - "version": "1.22.0-dev.20250409-89f8206ba4", - "resolved": "https://registry.npmmirror.com/onnxruntime-web/-/onnxruntime-web-1.22.0-dev.20250409-89f8206ba4.tgz", - "integrity": "sha512-0uS76OPgH0hWCPrFKlL8kYVV7ckM7t/36HfbgoFw6Nd0CZVVbQC4PkrR8mBX8LtNUFZO25IQBqV2Hx2ho3FlbQ==", - "license": "MIT", - "dependencies": { - "flatbuffers": "^25.1.24", - "guid-typescript": "^1.0.9", - "long": "^5.2.3", - "onnxruntime-common": "1.22.0-dev.20250409-89f8206ba4", - "platform": "^1.3.6", - "protobufjs": "^7.2.4" - } - }, - "node_modules/onnxruntime-web/node_modules/onnxruntime-common": { - "version": "1.22.0-dev.20250409-89f8206ba4", - "resolved": "https://registry.npmmirror.com/onnxruntime-common/-/onnxruntime-common-1.22.0-dev.20250409-89f8206ba4.tgz", - "integrity": "sha512-vDJMkfCfb0b1A836rgHj+ORuZf4B4+cc2bASQtpeoJLueuFc5DuYwjIZUBrSvx/fO5IrLjLz+oTrB3pcGlhovQ==", - "license": "MIT" - }, "node_modules/phonemizer": { "version": "1.2.1", "resolved": "https://registry.npmmirror.com/phonemizer/-/phonemizer-1.2.1.tgz", diff --git a/package.json b/package.json index 9a184b8..13e05ff 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@huggingface/transformers": "^3.8.1", "@phosphor-icons/react": "^2.1.10", "@vitejs/plugin-react": "5.0.4", + "fflate": "^0.8.3", "kokoro-js": "^1.2.1", "react": "19.2.0", "react-dom": "19.2.0", diff --git a/public/assets/sample-motion.mp4 b/public/assets/sample-motion.mp4 new file mode 100644 index 0000000..f2f2cac Binary files /dev/null and b/public/assets/sample-motion.mp4 differ diff --git a/public/model-cache-sw.js b/public/model-cache-sw.js index 78e6cd6..252d800 100644 --- a/public/model-cache-sw.js +++ b/public/model-cache-sw.js @@ -1,5 +1,5 @@ -const MODEL_CACHE_NAME = "timeline-studio-model-cache-v2"; -const APP_CACHE_NAME = "timeline-studio-app-shell-v2"; +const MODEL_CACHE_NAME = "timeline-studio-model-cache-v3"; +const APP_CACHE_NAME = "timeline-studio-app-shell-v3"; const APP_SHELL_URLS = [ "/", "/manifest.webmanifest", @@ -10,8 +10,11 @@ const APP_SHELL_URLS = [ ]; const CACHEABLE_EXTENSIONS = [ ".bin", + ".css", + ".js", ".json", ".model", + ".mp4", ".onnx", ".png", ".safetensors", diff --git a/src/App.jsx b/src/App.jsx index f7d5834..bfcae70 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -26,6 +26,7 @@ import { Timeline } from "./components/Timeline.jsx"; import { Topbar } from "./components/Topbar.jsx"; import { createTranslator, getStoredLanguage, saveLanguagePreference, translateOptionName } from "./i18n.js"; import { transcribeAudioToCaptionSegments } from "./lib/asr.js"; +import { getCaptionTextLayout } from "./lib/captionLayout.js"; import { createCaptionSegments, createStickerSegment, @@ -60,6 +61,7 @@ import { getSupportedRecordingFormat, transcodeWebmToMp4, } from "./lib/media.js"; +import { createProjectArchive, readProjectArchive, readProjectFileAsText } from "./lib/projectArchive.js"; import { clearPiperCacheIfStorageTight, isPiperSymbolError, @@ -67,8 +69,33 @@ import { prepareTextForVoice, TtsInputError, } from "./lib/ttsText.js"; +import { + analyzeVideoVisualTrack, + analyzeVisualSubject, + captureVisualFrame, + disposeVisionWorker, + getVisionKey, + resolveVisionAnalysisAtTime, +} from "./lib/vision.js"; +import { + getCaptionAvoidancePlacement, + getSmartCropRect, + mapNormalizedBoxToFrame, +} from "./lib/visualGeometry.js"; const PLAYBACK_UI_FRAME_MS = 50; +const DEFAULT_VISION_OPTIONS = Object.freeze({ + showDetections: true, + removeBackground: false, + avoidCaptions: true, + smartCrop: true, +}); +const EMPTY_VISION_OPTIONS = Object.freeze({ + showDetections: false, + removeBackground: false, + avoidCaptions: false, + smartCrop: false, +}); function getNearestRatioIdForSize(width, height) { const mediaWidth = Number(width); @@ -88,6 +115,42 @@ function getNearestRatioIdForSize(width, height) { ).id; } +function getObjectPositionForCrop(cropRect) { + const normalized = cropRect?.normalized; + if (!normalized) { + return "50% 50%"; + } + + const horizontalRange = Math.max(0, 1 - normalized.width); + const verticalRange = Math.max(0, 1 - normalized.height); + const x = horizontalRange > 0.0001 ? (normalized.xMin / horizontalRange) * 100 : 50; + const y = verticalRange > 0.0001 ? (normalized.yMin / verticalRange) * 100 : 50; + return `${Math.max(0, Math.min(100, x))}% ${Math.max(0, Math.min(100, y))}%`; +} + +function isSameVisionDetection(detection, subject) { + if (!detection?.box || !subject?.box || detection.label !== subject.label) { + return false; + } + + return [ + ["xMin", "xmin"], + ["yMin", "ymin"], + ["xMax", "xmax"], + ["yMax", "ymax"], + ].every(([camelKey, lowerKey]) => + Math.abs( + (detection.box[camelKey] ?? detection.box[lowerKey] ?? 0) - + (subject.box[camelKey] ?? subject.box[lowerKey] ?? 0), + ) < 0.004, + ); +} + +function revokeVisionObjectUrls(value) { + const urls = Array.isArray(value) ? value : value ? [value] : []; + urls.forEach((url) => URL.revokeObjectURL(url)); +} + function getTimelineTrackLocalTime(time, start = 0, duration = 0) { return Math.max(0, Math.min(duration || 0, time - start)); } @@ -149,6 +212,7 @@ export function App() { const [showRatioMenu, setShowRatioMenu] = useState(false); const [fitMode, setFitMode] = useState("contain"); const [showSettings, setShowSettings] = useState(false); + const [showFileMenu, setShowFileMenu] = useState(false); const [compactRail, setCompactRail] = useState(false); const [selectedTrack, setSelectedTrack] = useState("image"); const [timelineZoom, setTimelineZoom] = useState(1); @@ -171,6 +235,7 @@ export function App() { const [captionPosition, setCaptionPosition] = useState("bottom"); const [captionPlacement, setCaptionPlacement] = useState({ x: 50, y: 78 }); const [captionSize, setCaptionSize] = useState(12); + const [captionStyle, setCaptionStyle] = useState({ backgroundColor: "#05080d", backgroundOpacity: 0.62, textColor: "#f5fbff", borderColor: "#35f0dd", borderWidth: 0, radius: 7, paddingX: 22, paddingY: 12, shadowOpacity: 0.45, effect: "normal" }); const [captionsEnabled, setCaptionsEnabled] = useState(true); const [captionSegments, setCaptionSegments] = useState(() => createCaptionSegments(DEFAULT_SCRIPT)); const [selectedSegmentId, setSelectedSegmentId] = useState(""); @@ -196,8 +261,18 @@ export function App() { const [toast, setToast] = useState(""); const [lastSaved, setLastSaved] = useState(formatSavedTime()); const [previewFrameSize, setPreviewFrameSize] = useState({ width: 0, height: 0 }); + const [previewVideoMediaTime, setPreviewVideoMediaTime] = useState(0); + const [visionRecords, setVisionRecords] = useState({}); + const [visionJob, setVisionJob] = useState({ + running: false, + key: "", + progress: 0, + phase: "", + }); + const [avatarPanelOpen, setAvatarPanelOpen] = useState(false); const fileInputRef = useRef(null); + const projectFileInputRef = useRef(null); const previewShellRef = useRef(null); const previewCanvasRef = useRef(null); const audioRef = useRef(null); @@ -229,6 +304,9 @@ export function App() { const timelineClipDragRef = useRef(null); const suppressTimelineClipClickRef = useRef(""); const autoRatioSourceKeyRef = useRef(""); + const visionObjectUrlsRef = useRef(new Map()); + const visionAbortControllerRef = useRef(null); + const visionJobGenerationRef = useRef(0); const activeLanguage = uiLanguage || "zh"; const t = useMemo(() => createTranslator(activeLanguage), [activeLanguage]); const trOption = (name, option) => { @@ -245,6 +323,10 @@ export function App() { [selectedVoiceId], ); + function openAvatarPanel() { + setAvatarPanelOpen(true); + } + const ratio = useMemo( () => RATIO_OPTIONS.find((option) => option.id === ratioId) ?? RATIO_OPTIONS[0], [ratioId], @@ -306,7 +388,13 @@ export function App() { ], ); const timelineDuration = useMemo( - () => (estimatedDuration > 0 ? MAX_TIMELINE_DURATION_SECONDS : 0), + () => + estimatedDuration > 0 + ? Math.min( + MAX_TIMELINE_DURATION_SECONDS, + Math.max(10, Math.ceil((estimatedDuration + 1) / 10) * 10), + ) + : 0, [estimatedDuration], ); timelineDurationRef.current = timelineDuration; @@ -345,12 +433,59 @@ export function App() { currentVisualSegmentIndex >= 0 ? visualSegments[currentVisualSegmentIndex] ?? null : null; const currentVisualRange = currentVisualSegmentIndex >= 0 ? visualTimeline[currentVisualSegmentIndex] ?? null : null; - const previewVisualSegment = currentVisualSegment ?? visualSegments[0] ?? null; + const previewVisualSegmentIndex = + currentVisualSegmentIndex >= 0 + ? currentVisualSegmentIndex + : visualSegments.length + ? currentTime >= imageDuration + ? visualSegments.length - 1 + : 0 + : -1; + const previewVisualSegment = + previewVisualSegmentIndex >= 0 ? visualSegments[previewVisualSegmentIndex] ?? null : null; + const previewVisualRange = + previewVisualSegmentIndex >= 0 ? visualTimeline[previewVisualSegmentIndex] ?? null : null; const previewVisualSrc = previewVisualSegment?.src || imageSrc; const previewVisualType = previewVisualSegment?.type || visualType; - const previewVisualLocalTime = currentVisualRange - ? Math.max(0, currentTime - currentVisualRange.start) + const activePreviewFilter = useMemo( + () => VISUAL_STYLE_OPTIONS.find((filter) => filter.id === (previewVisualSegment?.filterId || selectedFilterId)) ?? FILTER_OPTIONS[0], + [previewVisualSegment?.filterId, selectedFilterId], + ); + const previewVisualLocalTime = previewVisualRange + ? Math.max(0, currentTime - previewVisualRange.start) : currentTime; + const previewVisualSourceTime = + previewVisualType === "video" + ? Math.max(0, Number(previewVisualSegment?.sourceStart) || 0) + previewVisualLocalTime + : previewVisualLocalTime; + const previewVisionKey = getVisionKey( + previewVisualSegment ?? + (previewVisualSrc + ? { + id: "visual-fallback", + src: previewVisualSrc, + type: previewVisualType, + width: previewVisualSegment?.width ?? 0, + height: previewVisualSegment?.height ?? 0, + } + : null), + ); + const previewVisionRecord = previewVisionKey ? visionRecords[previewVisionKey] ?? null : null; + const previewVisionBaseAnalysis = previewVisionRecord?.analysis ?? null; + const previewVisionAnalysis = useMemo( + () => + resolveVisionAnalysisAtTime( + previewVisionBaseAnalysis, + previewVisualType === "video" ? previewVideoMediaTime : previewVisualSourceTime, + ), + [ + previewVideoMediaTime, + previewVisionBaseAnalysis, + previewVisualSourceTime, + previewVisualType, + ], + ); + const previewVisionOptions = previewVisionRecord?.options ?? EMPTY_VISION_OPTIONS; const selectedVisualSegmentIndex = Math.max( 0, visualSegments.findIndex((segment) => segment.id === selectedVisualSegmentId), @@ -364,6 +499,125 @@ export function App() { (trackVisibility.music && musicBlob && musicUrl), ); const canPreview = hasPlayableVisualTimeline || hasPlayableAudioTimeline; + const previewVisionFrameSize = useMemo( + () => ({ + width: previewFrameSize.width || ratio.width, + height: previewFrameSize.height || ratio.height, + }), + [previewFrameSize.height, previewFrameSize.width, ratio.height, ratio.width], + ); + const previewSmartCropRect = useMemo(() => { + if ( + !previewVisionOptions.smartCrop || + !previewVisionAnalysis?.subject?.box || + !previewVisionAnalysis?.sourceSize + ) { + return null; + } + + return getSmartCropRect( + previewVisionAnalysis.sourceSize, + previewVisionFrameSize, + previewVisionAnalysis.subject.box, + { padding: 0.14 }, + ); + }, [ + previewVisionAnalysis, + previewVisionFrameSize, + previewVisionOptions.smartCrop, + ]); + const previewVisionOverlayBoxes = useMemo(() => { + if (!previewVisionOptions.showDetections || !previewVisionAnalysis?.sourceSize) { + return []; + } + + return (previewVisionAnalysis.detections ?? []) + .map((detection) => { + const mapped = mapNormalizedBoxToFrame( + detection.box, + previewVisionAnalysis.sourceSize, + previewVisionFrameSize, + { + fitMode, + smartCrop: previewSmartCropRect || false, + outputSize: previewVisionFrameSize, + }, + ); + if (!mapped?.normalized || mapped.width < 1 || mapped.height < 1) { + return null; + } + + return { + ...mapped.normalized, + label: detection.label, + score: detection.score, + isSubject: isSameVisionDetection(detection, previewVisionAnalysis.subject), + }; + }) + .filter(Boolean); + }, [ + fitMode, + previewSmartCropRect, + previewVisionAnalysis, + previewVisionFrameSize, + previewVisionOptions.showDetections, + ]); + const previewCaptionLayout = useMemo( + () => + getCaptionTextLayout({ + text: currentCaption, + captionSize, + captionStyle, + referenceFrame: previewVisionFrameSize, + renderFrame: previewVisionFrameSize, + }), + [captionSize, captionStyle, currentCaption, previewVisionFrameSize], + ); + const effectiveCaptionPlacement = useMemo(() => { + if ( + !previewVisionOptions.avoidCaptions || + !previewVisionAnalysis?.subject?.box || + !previewVisionAnalysis?.sourceSize + ) { + return captionPlacement; + } + + return getCaptionAvoidancePlacement(previewVisionAnalysis.subject.box, { + sourceSize: previewVisionAnalysis.sourceSize, + frameSize: previewVisionFrameSize, + fitMode, + smartCrop: previewSmartCropRect || false, + basePlacement: captionPlacement, + previousPlacement: captionPlacement, + captionSize: { + width: previewCaptionLayout.width / Math.max(1, previewVisionFrameSize.width), + height: previewCaptionLayout.height, + }, + safeMargin: 0.045, + }); + }, [ + captionPlacement, + fitMode, + previewCaptionLayout, + previewSmartCropRect, + previewVisionAnalysis, + previewVisionFrameSize, + previewVisionOptions.avoidCaptions, + ]); + const previewVisualRenderSrc = + previewVisionOptions.removeBackground && + previewVisualType === "image" && + previewVisionAnalysis?.cutoutUrl + ? previewVisionAnalysis.cutoutUrl + : previewVisualSrc; + const previewVisionMaskUrl = + previewVisionOptions.removeBackground && previewVisualType === "video" + ? previewVisionAnalysis?.cutoutUrl ?? "" + : ""; + const previewVisualObjectFit = previewSmartCropRect ? "cover" : fitMode; + const previewVisualObjectPosition = previewSmartCropRect + ? getObjectPositionForCrop(previewSmartCropRect) + : "50% 50%"; const filteredVoices = useMemo(() => { return VOICES.filter((voice) => { @@ -385,6 +639,17 @@ export function App() { width: 1920, height: 1080, }, + { + id: "sample-motion", + type: "video", + src: "/assets/sample-motion.mp4", + name: "sample-motion.mp4", + meta: "640 x 360 · 00:02.50", + duration: 2.5, + width: 640, + height: 360, + trackFrames: [], + }, ], [], ); @@ -526,17 +791,36 @@ export function App() { return () => window.clearInterval(timer); }, [exporting]); + useEffect(() => { + setPreviewVideoMediaTime( + previewVisualType === "video" + ? Math.max(0, Number(previewVisualSegment?.sourceStart) || 0) + : 0, + ); + }, [previewVisualSegment?.id, previewVisualSrc, previewVisualType]); + useEffect(() => { const video = previewVideoRef.current; if (!video || previewVisualType !== "video") { return; } - const boundedTime = Math.min(Math.max(0, previewVisualLocalTime), video.duration || previewVisualLocalTime); + const maximumTime = Math.max(0, (Number(video.duration) || previewVisualSourceTime) - 0.001); + const boundedTime = Math.min(Math.max(0, previewVisualSourceTime), maximumTime); if (Number.isFinite(boundedTime) && Math.abs(video.currentTime - boundedTime) > 0.2) { video.currentTime = boundedTime; + setPreviewVideoMediaTime(boundedTime); } - }, [previewVisualLocalTime, previewVisualSrc, previewVisualType]); + if (isPlaying && trackVisibility.image && video.paused) { + video.play().catch(() => {}); + } + }, [ + isPlaying, + previewVisualSourceTime, + previewVisualSrc, + previewVisualType, + trackVisibility.image, + ]); useEffect(() => { const video = previewVideoRef.current; @@ -709,6 +993,7 @@ export function App() { selectedStickerId, captionSegments, visualSegments, + visionRecords, timelineZoom, ]); @@ -725,6 +1010,10 @@ export function App() { } imageUrlRefs.current.forEach((url) => URL.revokeObjectURL(url)); imageUrlRefs.current.clear(); + visionAbortControllerRef.current?.abort(); + visionObjectUrlsRef.current.forEach((urls) => revokeVisionObjectUrls(urls)); + visionObjectUrlsRef.current.clear(); + disposeVisionWorker(); voiceRecorderStreamRef.current?.getTracks().forEach((track) => track.stop()); window.clearInterval(voiceRecorderTimerRef.current); window.clearTimeout(toastTimerRef.current); @@ -737,8 +1026,432 @@ export function App() { toastTimerRef.current = window.setTimeout(() => setToast(""), 2600); } + function clearAllVisionState() { + visionJobGenerationRef.current += 1; + visionAbortControllerRef.current?.abort(); + visionAbortControllerRef.current = null; + visionObjectUrlsRef.current.forEach((urls) => revokeVisionObjectUrls(urls)); + visionObjectUrlsRef.current.clear(); + setVisionRecords({}); + setVisionJob({ running: false, key: "", progress: 0, phase: "" }); + } + + function getProjectSnapshot() { + const serializableVisualSegments = visualSegments.map(({ blob, trackFrames, src, cutoutVisual, ...segment }) => segment); + return { + script, selectedVoiceId, speed, volume, ratioId, fitMode, captionPosition, + captionPlacement, captionSize, captionStyle, captionsEnabled, captionSegments, visualSegments: serializableVisualSegments, + stickerSegments, selectedFilterId, selectedTransitionId, selectedStickerId, trackVisibility, timelineZoom, + audioDuration, musicName, musicDuration, musicVolume, sourceAudioName, sourceAudioDuration, sourceAudioStart, sourceAudioVolume, + }; + } + + async function handleExportProject() { + setShowFileMenu(false); + try { + notify("正在打包工程与媒体素材…"); + const archive = await createProjectArchive({ + project: getProjectSnapshot(), + visualSegments, + audio: audioBlob ? { blob: audioBlob, name: "ai-voiceover" } : null, + sourceAudio: sourceAudioBlob ? { blob: sourceAudioBlob, name: sourceAudioName || "source-audio" } : null, + music: musicBlob ? { blob: musicBlob, name: musicName || "background-music" } : null, + }); + downloadBlob(archive, "AI-配音项目.timeline"); + notify("工程包已导出(含媒体素材)"); + } catch (error) { + notify(error instanceof Error ? `工程导出失败:${error.message}` : "工程导出失败"); + } + } + + function handleNewProject() { + if (!window.confirm("新建工程将清空当前时间线,是否继续?")) return; + setScript(DEFAULT_SCRIPT); + setCaptionSegments(createCaptionSegments(DEFAULT_SCRIPT)); + setSelectedSegmentId(""); + clearImageTrack(""); + clearAudioTrack(""); + clearSourceAudioTrack(""); + clearMusicTrack(""); + setStickerSegments([]); + setSelectedStickerSegmentId(""); + clearAllVisionState(); + setCurrentTime(0); + setShowFileMenu(false); + notify("已新建空白工程"); + } + + async function handleImportProject(file) { + if (!file) { + projectFileInputRef.current?.click(); + return; + } + try { + let archive; + try { + archive = await readProjectArchive(file); + } catch (archiveError) { + // Projects exported before the portable archive format were JSON-only. + // Keep them importable so upgrading the editor never strands a project. + const legacy = JSON.parse(await readProjectFileAsText(file)); + if (legacy?.format !== "timeline-studio-project" || !legacy.project) throw archiveError; + archive = { + payload: { ...legacy, media: { visuals: [] } }, + visualMedia: new Map(), + audio: null, + sourceAudio: null, + music: null, + legacy: true, + }; + } + const { payload, visualMedia, audio: importedAudio, sourceAudio: importedSourceAudio, music: importedMusic } = archive; + const data = payload.project; + setScript(typeof data.script === "string" ? data.script : DEFAULT_SCRIPT); + const nextCaptions = Array.isArray(data.captionSegments) ? data.captionSegments : createCaptionSegments(data.script || DEFAULT_SCRIPT); + setCaptionSegments(nextCaptions); + setSelectedSegmentId(nextCaptions[0]?.id ?? ""); + setSelectedVoiceId(data.selectedVoiceId || VOICES[0].id); + setSpeed(Number(data.speed) || 1); setVolume(Number(data.volume) || 1); + setRatioId(RATIO_OPTIONS.some((option) => option.id === data.ratioId) ? data.ratioId : "16:9"); + setFitMode(data.fitMode || "contain"); setCaptionPosition(data.captionPosition || "bottom"); + setCaptionPlacement(data.captionPlacement || { x: 50, y: 78 }); setCaptionSize(Number(data.captionSize) || 12); + setCaptionStyle(data.captionStyle || captionStyle); + setCaptionsEnabled(data.captionsEnabled !== false); setTrackVisibility(data.trackVisibility || trackVisibility); + setTimelineZoom(Number(data.timelineZoom) || 1); setSelectedFilterId(data.selectedFilterId || "none"); + setSelectedTransitionId(data.selectedTransitionId || "none"); setSelectedStickerId(data.selectedStickerId || "none"); + setStickerSegments(Array.isArray(data.stickerSegments) ? data.stickerSegments : []); + const importedVisuals = Array.isArray(data.visualSegments) ? data.visualSegments + .map((segment) => { + const media = visualMedia.get(segment.id); + if (media?.blob) return { ...segment, src: URL.createObjectURL(media.blob), blob: media.blob }; + // JSON-era projects could only retain public/static asset URLs. + return segment?.src ? segment : null; + }) + .filter(Boolean) : []; + importedVisuals.filter((segment) => segment.src?.startsWith("blob:")).forEach((segment) => imageUrlRefs.current.add(segment.src)); + setVisualSegments(importedVisuals); setImageDuration(getVisualSegmentsTotal(importedVisuals)); + setImageClipCount(getImageThumbnailCount(getVisualSegmentsTotal(importedVisuals))); + setCurrentVisualAsset(importedVisuals[0] || null); + if (importedAudio) { + const decoded = await decodeWaveform(importedAudio); + replaceAudio(importedAudio, Number(data.audioDuration) || decoded.duration, decoded.peaks, "已恢复工程配音"); + } else clearAudioTrack(""); + if (importedSourceAudio) { + const decoded = await decodeWaveform(importedSourceAudio); + replaceSourceAudio(importedSourceAudio, Number(data.sourceAudioDuration) || decoded.duration, decoded.peaks, data.sourceAudioName || "source-audio", "", Number(data.sourceAudioStart) || 0); + } else clearSourceAudioTrack(""); + if (importedMusic) { + const decoded = await decodeWaveform(importedMusic); + replaceMusic(importedMusic, Number(data.musicDuration) || decoded.duration, decoded.peaks, data.musicName || "background-music", ""); + } else clearMusicTrack(""); + setMusicVolume(Number(data.musicVolume) || 0.35); setSourceAudioVolume(Number(data.sourceAudioVolume) || 1); + setCurrentTime(0); clearAllVisionState(); setShowFileMenu(false); + notify(archive.legacy + ? "旧版工程已导入;请重新添加未嵌入的本地媒体,然后导出为 .timeline 工程包" + : "工程包已导入,媒体素材已恢复"); + } catch (error) { + const detail = error instanceof Error && error.message ? `:${error.message}` : ""; + notify(`无法读取工程文件${detail}`); + } + if (projectFileInputRef.current) projectFileInputRef.current.value = ""; + } + + async function analyzeCurrentVisual() { + if (!previewVisualSrc || !previewVisionKey) { + notify("请先把图片或视频放到图片轨"); + return; + } + + if (visionJob.running && visionJob.key === previewVisionKey) { + visionJobGenerationRef.current += 1; + visionAbortControllerRef.current?.abort(); + visionAbortControllerRef.current = null; + setVisionJob({ running: false, key: previewVisionKey, progress: 0, phase: "分析已取消" }); + notify("已取消当前视觉分析"); + return; + } + + visionAbortControllerRef.current?.abort(); + const controller = new AbortController(); + visionAbortControllerRef.current = controller; + const jobGeneration = visionJobGenerationRef.current + 1; + visionJobGenerationRef.current = jobGeneration; + const analysisKey = previewVisionKey; + const analysisType = previewVisualType; + setVisionJob({ + running: true, + key: analysisKey, + progress: 1, + phase: analysisType === "video" ? "准备分析整段视频" : "截取当前视觉画面", + }); + + try { + const handleVisionProgress = ({ progress: nextProgress, phase }) => { + setVisionJob((job) => + job.key === analysisKey + ? { + ...job, + progress: Math.max(job.progress, nextProgress), + phase: phase || job.phase, + } + : job, + ); + }; + const source = previewVisualSegment?.blob || previewVisualSrc; + let analysis; + let nextObjectUrls = []; + + if (analysisType === "video") { + const videoDuration = Math.max( + 0.05, + Number(previewVideoRef.current?.duration) || + Number(previewVisualSegment?.duration) || + 0.05, + ); + const result = await analyzeVideoVisualTrack({ + src: source, + duration: videoDuration, + includeMatting: true, + fps: 2, + maxSamples: 180, + maxDimension: 512, + threshold: 0.32, + preferredLabels: ["person", "cat", "dog", "car", "bottle", "chair"], + signal: controller.signal, + onProgress: handleVisionProgress, + }); + const samples = result.samples.map((sample) => { + const { cutoutBlob, ...sampleData } = sample; + const cutoutUrl = cutoutBlob ? URL.createObjectURL(cutoutBlob) : ""; + if (cutoutUrl) { + nextObjectUrls.push(cutoutUrl); + } + return { ...sampleData, cutoutUrl }; + }); + analysis = { + ...result, + samples, + analyzedAt: Date.now(), + visualType: analysisType, + }; + } else { + const frameBlob = await captureVisualFrame({ + src: source, + type: analysisType, + maxDimension: 1024, + outputType: "image/png", + quality: 0.92, + signal: controller.signal, + }); + const result = await analyzeVisualSubject({ + blob: frameBlob, + includeMatting: true, + threshold: 0.32, + preferredLabels: ["person", "cat", "dog", "car", "bottle", "chair"], + signal: controller.signal, + onProgress: handleVisionProgress, + }); + const cutoutUrl = result.cutoutBlob ? URL.createObjectURL(result.cutoutBlob) : ""; + if (cutoutUrl) { + nextObjectUrls = [cutoutUrl]; + } + analysis = { + ...result, + cutoutUrl, + analyzedAt: Date.now(), + visualType: analysisType, + }; + } + + if (controller.signal.aborted || jobGeneration !== visionJobGenerationRef.current) { + return; + } + + const previousUrls = visionObjectUrlsRef.current.get(analysisKey); + revokeVisionObjectUrls(previousUrls); + visionObjectUrlsRef.current.set(analysisKey, nextObjectUrls); + + setVisionRecords((records) => { + const previous = records[analysisKey]; + return { + ...records, + [analysisKey]: { + analysis, + options: previous?.options ?? { + ...DEFAULT_VISION_OPTIONS, + removeBackground: false, + }, + }, + }; + }); + setVisionJob({ + running: false, + key: analysisKey, + progress: 100, + phase: "视觉主体分析完成", + }); + notify( + analysisType === "image" + ? "YOLOS tiny 主体识别与 MODNet 抠图已就绪" + : `全视频分析完成:YOLOS + MODNet 已覆盖 ${analysis.samples.length} 个时序帧`, + ); + } catch (error) { + if (error?.name === "AbortError") { + return; + } + console.error(error); + setVisionJob({ + running: false, + key: analysisKey, + progress: 0, + phase: "视觉分析失败", + }); + notify(error?.message || "视觉主体分析失败,请重试"); + } finally { + if (visionAbortControllerRef.current === controller) { + visionAbortControllerRef.current = null; + setVisionJob((job) => (job.running && job.key === analysisKey ? { ...job, running: false } : job)); + } + } + } + + function toggleVisionOption(optionId) { + if (!previewVisionKey || !previewVisionRecord) { + return; + } + + const hasMatting = + Boolean(previewVisionAnalysis?.cutoutUrl) || + Boolean(previewVisionBaseAnalysis?.samples?.some((sample) => sample.cutoutUrl)); + if (optionId === "removeBackground" && !hasMatting) { + notify("请先完成当前图片或整段视频的 MODNet 分析"); + return; + } + + const nextEnabled = !previewVisionOptions[optionId]; + setVisionRecords((records) => { + const record = records[previewVisionKey]; + if (!record) { + return records; + } + return { + ...records, + [previewVisionKey]: { + ...record, + options: { + ...record.options, + [optionId]: nextEnabled, + }, + }, + }; + }); + + const optionLabels = { + showDetections: "主体识别框", + removeBackground: "MODNet 抠图", + avoidCaptions: "字幕智能避让", + smartCrop: "主体智能裁切", + }; + notify(`${optionLabels[optionId] ?? "智能画面"}已${nextEnabled ? "开启" : "关闭"}`); + } + + function setFitModeFromUser(nextModeOrUpdater) { + setFitMode(nextModeOrUpdater); + if (!previewVisionKey || !previewVisionRecord?.options?.smartCrop) { + return; + } + setVisionRecords((records) => { + const record = records[previewVisionKey]; + return record + ? { + ...records, + [previewVisionKey]: { + ...record, + options: { ...record.options, smartCrop: false }, + }, + } + : records; + }); + } + + function clearVisionAnalysis() { + if (!previewVisionKey) { + return; + } + + if (visionJob.running && visionJob.key === previewVisionKey) { + visionJobGenerationRef.current += 1; + visionAbortControllerRef.current?.abort(); + visionAbortControllerRef.current = null; + } + const cutoutUrls = visionObjectUrlsRef.current.get(previewVisionKey); + if (cutoutUrls) { + revokeVisionObjectUrls(cutoutUrls); + visionObjectUrlsRef.current.delete(previewVisionKey); + } + setVisionRecords((records) => { + const nextRecords = { ...records }; + delete nextRecords[previewVisionKey]; + return nextRecords; + }); + setVisionJob({ running: false, key: "", progress: 0, phase: "" }); + notify("当前素材的视觉分析已清除"); + } + + function downloadVisionCutout() { + if (previewVisualType === "video") { + notify("视频 MODNet 遮罩会随时间变化,并随视频一起预览和导出"); + return; + } + const cutoutBlob = previewVisionAnalysis?.cutoutBlob; + if (!cutoutBlob) { + notify("当前素材还没有透明抠图"); + return; + } + const baseName = (previewVisualSegment?.name || imageName || "subject") + .replace(/\.[^.]+$/, "") + .replace(/[^\w\u3400-\u9fff-]+/g, "-"); + downloadBlob(cutoutBlob, `${baseName || "subject"}-modnet.png`); + notify("透明 PNG 已下载"); + } + + function removeVisionRecordsForAsset(asset) { + if (!asset?.id && !asset?.src) { + return; + } + + const belongsToAsset = (key) => + Boolean( + (asset.id && key.startsWith(`${asset.id}::`)) || + (asset.src && key.includes(`::${asset.src}`)), + ); + if (visionJob.running && belongsToAsset(visionJob.key)) { + visionJobGenerationRef.current += 1; + visionAbortControllerRef.current?.abort(); + visionAbortControllerRef.current = null; + setVisionJob({ running: false, key: "", progress: 0, phase: "" }); + } + + setVisionRecords((records) => { + const nextRecords = { ...records }; + Object.keys(records).forEach((key) => { + if (!belongsToAsset(key)) { + return; + } + const cutoutUrls = visionObjectUrlsRef.current.get(key); + if (cutoutUrls) { + revokeVisionObjectUrls(cutoutUrls); + visionObjectUrlsRef.current.delete(key); + } + delete nextRecords[key]; + }); + return nextRecords; + }); + } + function selectTool(toolId) { setActiveTool(toolId); + if (toolId !== "smart") setAvatarPanelOpen(false); if (toolId === "audio") { setSelectedTrack("audio"); setVoiceTab("synthesis"); @@ -819,6 +1532,21 @@ export function App() { }; setCaptionPosition(position); setCaptionPlacement(placementMap[position] ?? placementMap.bottom); + if (previewVisionKey && previewVisionRecord?.options?.avoidCaptions) { + setVisionRecords((records) => { + const record = records[previewVisionKey]; + return record + ? { + ...records, + [previewVisionKey]: { + ...record, + options: { ...record.options, avoidCaptions: false }, + }, + } + : records; + }); + notify("已切回手动字幕位置,智能避让已关闭"); + } } function startCaptionDrag(event) { @@ -833,6 +1561,23 @@ export function App() { event.preventDefault(); event.stopPropagation(); + const disabledSmartAvoidance = Boolean( + previewVisionKey && previewVisionRecord?.options?.avoidCaptions, + ); + if (disabledSmartAvoidance) { + setVisionRecords((records) => { + const record = records[previewVisionKey]; + return record + ? { + ...records, + [previewVisionKey]: { + ...record, + options: { ...record.options, avoidCaptions: false }, + }, + } + : records; + }); + } setSelectedTrack("caption"); if (currentCaptionSegment) { setSelectedSegmentId(currentCaptionSegment.id); @@ -857,7 +1602,7 @@ export function App() { const handlePointerUp = () => { window.removeEventListener("pointermove", handlePointerMove); window.removeEventListener("pointerup", handlePointerUp); - notify("字幕位置已调整"); + notify(disabledSmartAvoidance ? "字幕位置已手动调整,智能避让已关闭" : "字幕位置已调整"); }; window.addEventListener("pointermove", handlePointerMove); @@ -1259,6 +2004,7 @@ export function App() { blob: previewVisualSegment?.blob || null, width: previewVisualSegment?.width || 0, height: previewVisualSegment?.height || 0, + sourceStart: Math.max(0, Number(previewVisualSegment?.sourceStart) || 0), trackFrames: previewVisualSegment?.trackFrames || [], }; } @@ -2020,6 +2766,11 @@ export function App() { } setSelectedLibraryAssetId(asset.id); + if (event.detail >= 2) { + const targetTrack = asset.type === "audio" ? "music" : "image"; + void applyAssetToTrack(asset, targetTrack); + return; + } notify("素材已选中,请拖到对应轨道使用"); } @@ -2141,6 +2892,29 @@ export function App() { void applyAssetToTrack(asset, targetTrack, { percent }); } + function handleVisualStyleDrop(event) { + const payload = event.dataTransfer?.getData("application/x-timeline-visual-style") || ""; + const [kind, styleId] = payload.split(":"); + if (!styleId || (kind !== "effect" && kind !== "transition")) { + handleTrackAssetDrop(event, "image"); + return; + } + const clip = event.target.closest?.("[data-timeline-segment-id]"); + const segmentId = clip?.dataset.timelineSegmentId; + if (!segmentId) { + notify("请将效果或转场拖到具体的画面片段上"); + return; + } + event.preventDefault(); + event.stopPropagation(); + setVisualSegments((segments) => segments.map((segment) => segment.id === segmentId ? { ...segment, [kind === "effect" ? "filterId" : "transitionId"]: styleId } : segment)); + setSelectedVisualSegmentId(segmentId); + setSelectedTrack("image"); + if (kind === "effect") setSelectedFilterId(styleId); + else setSelectedTransitionId(styleId); + notify(kind === "effect" ? "效果已应用到该画面片段" : "转场已绑定到该片段的结尾"); + } + async function selectAsset(asset) { if (asset.type === "audio") { if (!asset.blob) { @@ -2163,6 +2937,7 @@ export function App() { } function deleteUserAsset(asset) { + removeVisionRecordsForAsset(asset); const hasOtherAssetUsingUrl = userAssets.some( (item) => item.id !== asset.id && item.src === asset.src, ); @@ -2755,7 +3530,18 @@ export function App() { const video = await exportBrowserVideo({ imageSrc, visualType, - visualSegments: renderedVisualSegments, + visualSegments: renderedVisualSegments.map((segment) => { + const record = visionRecords[getVisionKey(segment)]; + return record + ? { + ...segment, + vision: { + ...record.analysis, + options: record.options, + }, + } + : segment; + }), audioBlob: trackVisibility.audio ? audioBlob : null, voiceVolume: volume, sourceAudioBlob: trackVisibility.source ? sourceAudioBlob : null, @@ -2781,6 +3567,14 @@ export function App() { captionPosition, captionPlacement, captionSize, + captionStyle, + captionReferenceSize: + previewFrameSize.width > 0 && previewFrameSize.height > 0 + ? previewFrameSize + : { + width: (360 * ratio.width) / ratio.height, + height: 360, + }, sticker: stickerSegments.length ? null : selectedSticker, stickerSegments: trackVisibility.sticker ? stickerSegments : [], transitionId: selectedTransitionId, @@ -3082,6 +3876,10 @@ export function App() { ...source, id: makeId("visual"), duration: secondDuration, + sourceStart: + source.type === "video" + ? Math.max(0, Number(source.sourceStart) || 0) + firstDuration + : Math.max(0, Number(source.sourceStart) || 0), }; const nextSegments = [...sourceSegments]; nextSegments.splice(segmentIndex, 1, firstSegment, secondSegment); @@ -3564,6 +4362,12 @@ export function App() { setCaptionsEnabled={setCaptionsEnabled} trackVisibility={trackVisibility} toggleTrackVisibility={toggleTrackVisibility} + showFileMenu={showFileMenu} + setShowFileMenu={setShowFileMenu} + handleNewProject={handleNewProject} + handleExportProject={handleExportProject} + handleImportProject={handleImportProject} + projectFileInputRef={projectFileInputRef} />
@@ -3603,6 +4407,7 @@ export function App() { ) : (
@@ -3756,7 +4596,7 @@ export function App() { handleDuplicateTrack={handleDuplicateTrack} handleCutTrack={handleCutTrack} fitMode={fitMode} - setFitMode={setFitMode} + setFitMode={setFitModeFromUser} canPreview={canPreview} handlePlayToggle={handlePlayToggle} isPlaying={isPlaying} @@ -3785,6 +4625,7 @@ export function App() { handleTrackAssetDragOver={handleTrackAssetDragOver} handleTrackAssetDragLeave={handleTrackAssetDragLeave} handleTrackAssetDrop={handleTrackAssetDrop} + handleVisualStyleDrop={handleVisualStyleDrop} activeTimelineClipDrag={activeTimelineClipDrag} showStickerTrack={showStickerTrack} stickerSegments={stickerSegments} diff --git a/src/components/CaptionOverlay.jsx b/src/components/CaptionOverlay.jsx new file mode 100644 index 0000000..6ee1cdf --- /dev/null +++ b/src/components/CaptionOverlay.jsx @@ -0,0 +1,68 @@ +import { useEffect, useMemo, useRef } from "react"; + +import { drawCaptionLayout, getCaptionTextLayout } from "../lib/captionLayout.js"; + +const FALLBACK_PREVIEW_FRAME = { width: 640, height: 360 }; + +export function CaptionOverlay({ + text, + captionSize, + captionStyle, + placement, + frameSize, + onPointerDown, + onDoubleClick, +}) { + const canvasRef = useRef(null); + const renderFrame = + frameSize?.width > 0 && frameSize?.height > 0 ? frameSize : FALLBACK_PREVIEW_FRAME; + const layout = useMemo( + () => + getCaptionTextLayout({ + text, + captionSize, + captionStyle, + referenceFrame: renderFrame, + renderFrame, + }), + [captionSize, captionStyle, renderFrame.height, renderFrame.width, text], + ); + + useEffect(() => { + const canvas = canvasRef.current; + if (!canvas || !layout.width || !layout.height) { + return; + } + + const pixelRatio = Math.max(1, Math.min(2, window.devicePixelRatio || 1)); + canvas.width = Math.max(1, Math.ceil(layout.width * pixelRatio)); + canvas.height = Math.max(1, Math.ceil(layout.height * pixelRatio)); + canvas.style.width = `${layout.width}px`; + canvas.style.height = `${layout.height}px`; + const context = canvas.getContext("2d"); + if (!context) { + return; + } + context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); + drawCaptionLayout(context, layout); + }, [layout]); + + return ( + + ); +} diff --git a/src/components/PreviewStage.jsx b/src/components/PreviewStage.jsx index 39f6619..97e8f06 100644 --- a/src/components/PreviewStage.jsx +++ b/src/components/PreviewStage.jsx @@ -1,3 +1,4 @@ +import { useEffect } from "react"; import { CaretDown, CloudArrowUp, @@ -9,6 +10,7 @@ import { } from "@phosphor-icons/react"; import { formatTime } from "../lib/timeline.js"; +import { CaptionOverlay } from "./CaptionOverlay.jsx"; import { IconButton } from "./ui.jsx"; export function PreviewStage({ @@ -16,18 +18,30 @@ export function PreviewStage({ previewShellRef, previewCanvasRef, previewVideoRef, + onPreviewVideoTimeUpdate, previewVisualSrc, + previewVisualRenderSrc, + previewVisionMaskUrl = "", previewVisualType, previewRatio, previewFrameStyle, + previewFrameSize, trackVisibility, fileInputRef, selectedFilter, fitMode, + visualObjectFit, + visualObjectPosition, + visionOverlayBoxes = [], + showVisionOverlays = false, + backgroundRemoved = false, + smartCropActive = false, + captionAvoidanceActive = false, setFitMode, captionsEnabled, currentCaption, captionSize, + captionStyle, captionPlacement, startCaptionDrag, setActiveTool, @@ -42,6 +56,35 @@ export function PreviewStage({ }) { const hasStickerOverlay = Boolean(selectedSticker?.src || selectedSticker?.text); const hasPreviewContent = Boolean(previewVisualSrc || hasStickerOverlay); + const renderedVisualSrc = previewVisualRenderSrc || previewVisualSrc; + const activeObjectFit = visualObjectFit || fitMode; + const activeObjectPosition = visualObjectPosition || "50% 50%"; + + useEffect(() => { + const video = previewVideoRef.current; + if ( + previewVisualType !== "video" || + !video || + typeof video.requestVideoFrameCallback !== "function" + ) { + return undefined; + } + + let callbackId = 0; + const handleVideoFrame = (_now, metadata) => { + onPreviewVideoTimeUpdate?.( + Number.isFinite(metadata?.mediaTime) ? metadata.mediaTime : video.currentTime, + ); + callbackId = video.requestVideoFrameCallback(handleVideoFrame); + }; + callbackId = video.requestVideoFrameCallback(handleVideoFrame); + return () => video.cancelVideoFrameCallback?.(callbackId); + }, [ + onPreviewVideoTimeUpdate, + previewVideoRef, + previewVisualSrc, + previewVisualType, + ]); return (
@@ -61,17 +104,20 @@ export function PreviewStage({ ) : (
- {previewVisualSrc && trackVisibility.image && previewVisualType === "image" ? ( + {renderedVisualSrc && trackVisibility.image && previewVisualType === "image" ? ( {t("currentMediaAlt")} ) : null} @@ -84,26 +130,67 @@ export function PreviewStage({ muted playsInline preload="metadata" + onTimeUpdate={(event) => + onPreviewVideoTimeUpdate?.(event.currentTarget.currentTime) + } + onSeeked={(event) => + onPreviewVideoTimeUpdate?.(event.currentTarget.currentTime) + } style={{ filter: selectedFilter.css, - objectFit: fitMode, + objectFit: activeObjectFit, + objectPosition: activeObjectPosition, + WebkitMaskImage: previewVisionMaskUrl + ? `url("${previewVisionMaskUrl}")` + : undefined, + maskImage: previewVisionMaskUrl + ? `url("${previewVisionMaskUrl}")` + : undefined, + WebkitMaskSize: previewVisionMaskUrl ? activeObjectFit : undefined, + maskSize: previewVisionMaskUrl ? activeObjectFit : undefined, + WebkitMaskPosition: previewVisionMaskUrl ? activeObjectPosition : undefined, + maskPosition: previewVisionMaskUrl ? activeObjectPosition : undefined, + WebkitMaskRepeat: previewVisionMaskUrl ? "no-repeat" : undefined, + maskRepeat: previewVisionMaskUrl ? "no-repeat" : undefined, }} /> ) : null} + {showVisionOverlays + ? visionOverlayBoxes.map((detection, index) => ( +
+ + {detection.label || "subject"} + {Number.isFinite(detection.score) ? ` ${Math.round(detection.score * 100)}%` : ""} + +
+ )) + : null} + {smartCropActive || captionAvoidanceActive || backgroundRemoved ? ( + + ) : null} {captionsEnabled && trackVisibility.caption && currentCaption ? ( - + /> ) : null} {selectedSticker.src ? ( diff --git a/src/components/Timeline.jsx b/src/components/Timeline.jsx index 285cf15..bf81557 100644 --- a/src/components/Timeline.jsx +++ b/src/components/Timeline.jsx @@ -114,6 +114,7 @@ export function Timeline({ handleTrackAssetDragOver, handleTrackAssetDragLeave, handleTrackAssetDrop, + handleVisualStyleDrop, activeTimelineClipDrag, showStickerTrack, stickerSegments, @@ -619,9 +620,12 @@ export function Timeline({ activeTimelineClipDrag?.track === "image" ? "is-reordering" : "" }`} onClick={() => setSelectedTrack("image")} - onDragOver={(event) => handleTrackAssetDragOver(event, "image")} onDragLeave={(event) => handleTrackAssetDragLeave(event, "image")} - onDrop={(event) => handleTrackAssetDrop(event, "image")} + onDragOver={(event) => { + if (event.dataTransfer?.types.includes("application/x-timeline-visual-style")) event.preventDefault(); + else handleTrackAssetDragOver(event, "image"); + }} + onDrop={(event) => handleVisualStyleDrop(event)} data-asset-drop-track="image" data-timeline-reorder-track="image" > diff --git a/src/components/Topbar.jsx b/src/components/Topbar.jsx index ffe121b..0c4a310 100644 --- a/src/components/Topbar.jsx +++ b/src/components/Topbar.jsx @@ -3,6 +3,8 @@ import { ArrowCounterClockwise, CaretDown, FileArrowDown, + FileArrowUp, + FilePlus, GearSix, Pause, Play, @@ -40,6 +42,12 @@ export function Topbar({ setCaptionsEnabled, trackVisibility, toggleTrackVisibility, + showFileMenu, + setShowFileMenu, + handleNewProject, + handleExportProject, + handleImportProject, + projectFileInputRef, }) { return (
@@ -48,7 +56,40 @@ export function Topbar({
-
{t("projectTitle")}
+
+
{t("projectTitle")}
+
+ + {showFileMenu ? ( + setShowFileMenu(false)}> +
+
+ 工程 + Timeline Studio +
+ +
+ + +
+ + ) : null} + event.target.files?.[0] && handleImportProject(event.target.files[0])} /> +
+
{t("autosave")} · {lastSaved} diff --git a/src/components/VoicePanel.jsx b/src/components/VoicePanel.jsx index 13de8fa..2565a32 100644 --- a/src/components/VoicePanel.jsx +++ b/src/components/VoicePanel.jsx @@ -1,9 +1,12 @@ import { + CheckCircle, ClosedCaptioning, Eye, EyeSlash, ListBullets, + PersonSimpleRun, Trash, + Waveform, } from "@phosphor-icons/react"; import { formatTime, getSegmentStartTime } from "../lib/timeline.js"; @@ -133,6 +136,26 @@ function CaptionContextPanel({ ); } +function AvatarContextPanel({ t, hasVisual, visualType, audioBlob, audioDuration, captionSegments, selectedVoice }) { + const hasPortrait = hasVisual && visualType === "image"; + return ( +
+
+ +
{t("avatarKicker")}{t("avatarLipSyncTitle")}{t("avatarLipSyncDescription")}
+
+
+
{t("avatarPortrait")}{hasPortrait ? t("avatarCurrentPortrait") : t("avatarNeedsPortrait")}
+
{t("avatarAudio")}{audioBlob ? `${selectedVoice?.name ?? "AI"} · ${audioDuration.toFixed(1)}s` : t("avatarNeedsAudio")}
+
{t("avatarLipSyncSource")}{captionSegments.length ? `${captionSegments.length} ${t("captionSegmentsUnit")} · ${t("avatarCaptionSync")}` : t("avatarNeedsCaptions")}
+
+
{t("avatarSyncMode")}{t("avatarSyncModeCaption")}
+

{t("avatarGenerationNote")}

+
{t("avatarServiceNeeded")}{t("avatarServiceHint")}
+
+ ); +} + export function VoicePanel({ t, activeTool, @@ -190,19 +213,26 @@ export function VoicePanel({ generateCaptionsFromSourceAudio, isGeneratingCaptions, automaticCaptionProgress, + avatarPanelOpen, + hasVisual, + visualType, + audioDuration, }) { const isCaptionContext = activeTool === "caption"; - const title = isCaptionContext ? t("caption") : t("aiVoice"); + const isAvatarContext = activeTool === "smart" && avatarPanelOpen; + const title = isAvatarContext ? t("avatarTitle") : isCaptionContext ? t("caption") : t("aiVoice"); const panelStatusText = isCaptionContext ? captionSegments.length ? `${captionSegments.length} ${t("captionSegmentsUnit", "条字幕")}` : t("noCaptionSegments") + : isAvatarContext + ? t("avatarServiceNeeded") : statusText === "模型待命" ? t("modelReady") : statusText; return ( -