import { useEffect, useMemo, useRef, useState } from "react"; import { LanguageIntro } from "./components/panels.jsx"; import { PreviewStage } from "./components/PreviewStage.jsx"; import { VoicePanel } from "./components/VoicePanel.jsx"; import { Timeline } from "./components/Timeline.jsx"; import { Topbar } from "./components/Topbar.jsx"; import { AssetDragPreview, ExportProgressOverlay } from "./components/EditorOverlays.jsx"; import { EditorSidebar } from "./components/EditorSidebar.jsx"; import { useExportElapsed } from "./hooks/useExportElapsed.js"; import { usePreviewFrameSize } from "./hooks/usePreviewFrameSize.js"; import { useEditorCatalog } from "./hooks/useEditorCatalog.js"; import { useToast } from "./hooks/useToast.js"; import { useProjectFiles } from "./hooks/useProjectFiles.js"; import { useAutosaveTimestamp } from "./hooks/useAutosaveTimestamp.js"; import { useVisionAnalysis } from "./hooks/useVisionAnalysis.js"; import { useFileUpload } from "./hooks/useFileUpload.js"; import { useMediaSync } from "./hooks/useMediaSync.js"; import { useVideoExport } from "./hooks/useVideoExport.js"; import { useVoiceRecorder } from "./hooks/useVoiceRecorder.js"; import { useVoiceGeneration } from "./hooks/useVoiceGeneration.js"; import { useAutoCaptions } from "./hooks/useAutoCaptions.js"; import { useSourceAudioExtraction } from "./hooks/useSourceAudioExtraction.js"; import { useVocalSeparation } from "./hooks/useVocalSeparation.js"; import { useAvatarGeneration } from "./hooks/useAvatarGeneration.js"; import { useCaptionState } from "./hooks/useCaptionState.js"; import { useAudioTrackState } from "./hooks/useAudioTrackState.js"; import { useVisualTrackState } from "./hooks/useVisualTrackState.js"; import { useEditorUiState } from "./hooks/useEditorUiState.js"; import { useTimelineModel } from "./hooks/useTimelineModel.js"; import { usePreviewModel } from "./hooks/usePreviewModel.js"; import { useEditorRefs } from "./hooks/useEditorRefs.js"; import { useEditorLifecycle } from "./hooks/useEditorLifecycle.js"; import { useEditorHistory } from "./hooks/useEditorHistory.js"; import { createVisionControls } from "./lib/visionControls.js"; import { createAssetDragControls } from "./lib/assetDragControls.js"; import { createAssetLibraryActions } from "./lib/assetLibraryActions.js"; import { createPlaybackControls } from "./lib/playbackControls.js"; import { createTimelineReorderControls } from "./lib/timelineReorderControls.js"; import { createTimelineMoveControls } from "./lib/timelineMoveControls.js"; import { createImageResizeControl } from "./lib/imageResizeControl.js"; import { createTimelineClipboardActions } from "./lib/timelineClipboardActions.js"; import { createTimelineCutActions } from "./lib/timelineCutActions.js"; import { createTimelineSegmentCountActions } from "./lib/timelineSegmentCountActions.js"; import { createTimelineDurationActions } from "./lib/timelineDurationActions.js"; import { createAudioClipActions } from "./lib/audioClipActions.js"; import { createCaptionEditingActions } from "./lib/captionEditingActions.js"; import { createAudioTrackActions } from "./lib/audioTrackActions.js"; import { createVisualTimelineActions } from "./lib/visualTimelineActions.js"; import { createStickerTimelineActions } from "./lib/stickerTimelineActions.js"; import { createAssetDropActions } from "./lib/assetDropActions.js"; import { createEditorCommandActions } from "./lib/editorCommandActions.js"; import { createTimelineViewModel } from "./lib/timelineViewModel.js"; import { createTranslator, getStoredLanguage, translateOptionName } from "./i18n.js"; import { decodeWaveform, downloadBlob } from "./lib/media.js"; import { getImageThumbnailCount, getVisualSegmentsTotal } from "./lib/timeline.js"; import { removeVisualPropertyKeyframe, updateVisualSegmentPlaybackRate, upsertVisualKeyframe, upsertVisualPropertyKeyframe } from "./lib/visualEffects.js"; import { getLinkedSourceAudioEnd, getLinkedSourceAudioSegments } from "./lib/sourceAudioSync.js"; import { getTimelineInitialContentZoom } from "./lib/timelineScale.js"; function getExportDimensions(ratio, longEdge) { const sourceLongEdge = Math.max(ratio.width, ratio.height); const scale = longEdge / sourceLongEdge; const even = (value) => Math.max(2, Math.round(value / 2) * 2); return { width: even(ratio.width * scale), height: even(ratio.height * scale) }; } function getExportBitrate(resolution, quality, frameRate) { const base = { 720: 5, 1080: 10, 1440: 18, 2160: 38 }[resolution] || 10; const qualityScale = { standard: 0.65, high: 1, ultra: 1.45 }[quality] || 1; return Math.round(base * qualityScale * (frameRate / 30) * 1_000_000); } export function App() { const [uiLanguage, setUiLanguage] = useState(() => getStoredLanguage()); const [showExportMenu, setShowExportMenu] = useState(false); const [exportSettings, setExportSettings] = useState({ resolution: "1080", frameRate: 30, codec: "h264", quality: "high" }); const [captionVoiceFocusRequest, setCaptionVoiceFocusRequest] = useState(0); const timelineImportRestoreRef = useRef(false); const [introClosing, setIntroClosing] = useState(false); const { captionPlacement, captionPosition, captionSegments, captionSize, captionStyle, captionsEnabled, script, selectedSegmentId, setCaptionPlacement, setCaptionPosition, setCaptionSegments, setCaptionSize, setCaptionStyle, setCaptionsEnabled, setScript, setSelectedSegmentId, } = useCaptionState(); const { audioSegments, favoriteVoiceIds, historyItems, musicBlob, musicDuration, musicName, musicStart, musicPeaks, musicUrl, musicVolume, recordedVoices, recordingElapsed, recordingState, selectedAudioSegmentId, selectedVoiceId, setAudioSegments, setFavoriteVoiceIds, setHistoryItems, setMusicBlob, setMusicDuration, setMusicName, setMusicPeaks, setMusicStart, setMusicUrl, setMusicVolume, setRecordedVoices, setRecordingElapsed, setRecordingState, setSelectedAudioSegmentId, setSelectedVoiceId, setSourceAudioBlob, setSourceAudioAssetId, setSourceAudioDuration, setSourceAudioLinked, setSourceAudioName, setSourceAudioPeaks, setSourceAudioStart, setSourceAudioUrl, setSourceAudioVolume, setSpeed, setTimelineHorizon, setVolume, sourceAudioAssetId, sourceAudioBlob, sourceAudioDuration, sourceAudioLinked, sourceAudioName, sourceAudioPeaks, sourceAudioStart, sourceAudioUrl, sourceAudioVolume, speed, timelineHorizon, volume, } = useAudioTrackState(); const { fitMode, imageClipCount, imageDuration, imageMeta, imageName, imageSrc, selectedFilterId, selectedStickerId, selectedStickerSegmentId, selectedTransitionId, selectedVisualSegmentId, setFitMode, setImageClipCount, setImageDuration, setImageMeta, setImageName, setImageSrc, setSelectedFilterId, setSelectedStickerId, setSelectedStickerSegmentId, setSelectedTransitionId, setSelectedVisualSegmentId, setStickerSegments, setVisualSegments, setVisualType, stickerSegments, visualSegments, visualType, } = useVisualTrackState(); const { activeTool, assetDragPreview, assetDropPosition, assetDropPulseTrack, assetDropTargetTrack, compactRail, currentTime, draggedAssetId, exporting, exportPhase, exportProgress, isDragging, isPlaying, mediaTab, progress, ratioId, selectedLibraryAssetId, selectedTrack, setActiveTool, setAssetDragPreview, setAssetDropPosition, setAssetDropPulseTrack, setAssetDropTargetTrack, setCompactRail, setCurrentTime, setDraggedAssetId, setExporting, setExportPhase, setExportProgress, setIsDragging, setIsPlaying, setMediaTab, setProgress, setRatioId, setSelectedLibraryAssetId, setSelectedTrack, setShowFileMenu, setShowRatioMenu, setShowSettings, setShowVoiceFilter, setSnapGuide, setStatus, setStatusText, setTimelineClipDrag, setTimelineZoom, setTrackLocks, setTrackVisibility, setVoiceFilter, setVoiceTab, showFileMenu, showRatioMenu, showSettings, showVoiceFilter, snapGuide, status, statusText, timelineClipDrag, timelineZoom, trackLocks, trackVisibility, voiceFilter, voiceTab, } = useEditorUiState(); const [userAssets, setUserAssets] = useState([]); const { notify, toast } = useToast(); 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 [smartMode, setSmartMode] = useState("auto-edit"); const [avatarJob, setAvatarJob] = useState({ running: false, progress: 0, phase: "" }); const lastSaved = useAutosaveTimestamp([ script, imageSrc, visualType, imageDuration, captionPlacement, selectedVoiceId, speed, volume, musicName, musicDuration, musicStart, musicVolume, sourceAudioName, sourceAudioDuration, sourceAudioStart, sourceAudioVolume, ratioId, fitMode, selectedFilterId, selectedStickerId, captionSegments, visualSegments, visionRecords, timelineZoom, ]); const { assetDropPulseTimerRef, audioRef, audioSegmentRefs, audioUrlRef, autoRatioSourceKeyRef, avatarMotionCacheRef, avatarMotionWorkerRef, avatarRenderWorkerRef, avatarTestAudioImportedRef, avatarTestImportedRef, currentTimeRef, draggedAssetIdRef, exportStartRef, fileInputRef, imageUrlRefs, musicRef, musicUrlRef, pointerAssetDragRef, previewCanvasRef, previewShellRef, previewVideoRef, projectFileInputRef, sourceAudioRef, sourceAudioUrlRef, suppressAssetClickRef, suppressTimelineClipClickRef, timelineClipDragRef, timelineDurationRef, trackScrollRef, visionAbortControllerRef, visionJobGenerationRef, visionObjectUrlsRef, visualPlaybackFrameRef, visualPlaybackLastUpdateRef, visualPlaybackStartedAtRef, visualPlaybackStartTimeRef, voiceRecorderChunksRef, voiceRecorderRef, voiceRecorderStartedAtRef, voiceRecorderStreamRef, voiceRecorderTimerRef, } = useEditorRefs(); const { redo, undo } = useEditorHistory({ audioSegments, captionPlacement, captionPosition, captionSegments, captionSize, captionStyle, captionsEnabled, currentTime, fitMode, imageClipCount, imageDuration, imageMeta, imageName, imageSrc, imageUrlRefs, musicBlob, musicDuration, musicName, musicStart, musicPeaks, musicUrl, musicUrlRef, musicVolume, notify, selectedAudioSegmentId, selectedFilterId, selectedSegmentId, selectedStickerId, selectedStickerSegmentId, selectedTrack, selectedTransitionId, selectedVisualSegmentId, script, setAudioSegments, setCaptionPlacement, setCaptionPosition, setCaptionSegments, setCaptionSize, setCaptionStyle, setCaptionsEnabled, setCurrentTime, setFitMode, setImageClipCount, setImageDuration, setImageMeta, setImageName, setImageSrc, setIsPlaying, setMusicBlob, setMusicDuration, setMusicName, setMusicPeaks, setMusicStart, setMusicUrl, setMusicVolume, setScript, setSelectedAudioSegmentId, setSelectedFilterId, setSelectedSegmentId, setSelectedStickerId, setSelectedStickerSegmentId, setSelectedTrack, setSelectedTransitionId, setSelectedVisualSegmentId, setSourceAudioBlob, setSourceAudioAssetId, setSourceAudioDuration, setSourceAudioLinked, setSourceAudioName, setSourceAudioPeaks, setSourceAudioStart, setSourceAudioUrl, setSourceAudioVolume, setStickerSegments, setTimelineHorizon, setTrackLocks, setTrackVisibility, setUserAssets, setVisualSegments, setVisualType, sourceAudioAssetId, sourceAudioBlob, sourceAudioDuration, sourceAudioLinked, sourceAudioName, sourceAudioPeaks, sourceAudioStart, sourceAudioUrl, sourceAudioUrlRef, sourceAudioVolume, stickerSegments, timelineHorizon, trackLocks, trackVisibility, userAssets, visualSegments, visualType, }); const activeLanguage = uiLanguage || "zh"; const t = useMemo(() => createTranslator(activeLanguage), [activeLanguage]); const trOption = (name, option) => { if (option?.kind === "stickerCategory") { return activeLanguage !== "zh" && option.nameEn ? option.nameEn : name; } return activeLanguage !== "zh" && option?.nameEn ? option.nameEn : translateOptionName(activeLanguage, name); }; const shouldShowLanguageIntro = !uiLanguage; const linkedSourceAudioSegments = useMemo( () => sourceAudioLinked && sourceAudioBlob ? getLinkedSourceAudioSegments(visualSegments, sourceAudioAssetId, sourceAudioDuration) : [], [sourceAudioAssetId, sourceAudioBlob, sourceAudioDuration, sourceAudioLinked, visualSegments], ); const sourceAudioTimelineEnd = sourceAudioLinked && linkedSourceAudioSegments.length ? getLinkedSourceAudioEnd(linkedSourceAudioSegments) : sourceAudioStart + sourceAudioDuration; const { activePreviewFilter, audioBlob, audioDuration, audioUrl, canPreview, captionDuration, captionTargetDuration, captionTimeline, currentCaption, currentCaptionSegment, currentSegmentIndex, currentStickerSegment, currentStickerSegmentIndex, currentVisualRange, currentVisualSegment, currentVisualSegmentIndex, estimatedDuration, focusedSegmentIndex, getStickerDragAsset, peaks, previewSticker, previewStickers, previewTransition, previewVisionBaseAnalysis, previewVisionKey, previewVisionRecord, previewVisualLocalTime, previewVisualRange, previewVisualSegment, previewVisualSegmentIndex, previewVisualSourceTime, previewVisualSrc, previewVisualType, ratio, segments, selectedAudioSegment, selectedCaptionSegment, selectedFilter, selectedSegmentIndex, selectedSticker, selectedStickerSegmentIndex, selectedVisualSegmentIndex, selectedVoice, stickerDuration, timelineDuration, visualTimeline, voiceTrackDuration, } = useTimelineModel({ audioSegments, captionSegments, currentTime, imageDuration, imageSrc, musicBlob, musicDuration, musicUrl, ratioId, script, selectedAudioSegmentId, selectedFilterId, selectedSegmentId, selectedStickerId, selectedStickerSegmentId, selectedVisualSegmentId, selectedVoiceId, sourceAudioBlob, sourceAudioDuration, sourceAudioTimelineEnd, sourceAudioStart, sourceAudioUrl, stickerSegments, timelineDurationRef, timelineHorizon, trackVisibility, visionRecords, visualSegments, visualType, }); const previousTimelineContentDurationRef = useRef(estimatedDuration); useEffect(() => { const previousDuration = previousTimelineContentDurationRef.current; const becameNonEmpty = previousDuration <= 0 && estimatedDuration > 0; const becameEmpty = previousDuration > 0 && estimatedDuration <= 0; if (becameNonEmpty) { if (timelineImportRestoreRef.current) timelineImportRestoreRef.current = false; else setTimelineZoom(getTimelineInitialContentZoom(estimatedDuration)); if (trackScrollRef.current) trackScrollRef.current.scrollLeft = 0; } else if (becameEmpty) { timelineImportRestoreRef.current = false; setTimelineHorizon(10); setTimelineZoom(getTimelineInitialContentZoom(0)); setCurrentTime(0); if (trackScrollRef.current) trackScrollRef.current.scrollLeft = 0; } previousTimelineContentDurationRef.current = estimatedDuration; }, [estimatedDuration, setCurrentTime, setTimelineHorizon, setTimelineZoom, trackScrollRef]); const previewFrameSize = usePreviewFrameSize(previewShellRef, ratio, compactRail); const selectedVisualSegment = visualSegments[selectedVisualSegmentIndex] ?? previewVisualSegment ?? null; const selectedVisualRange = visualTimeline[selectedVisualSegmentIndex] ?? previewVisualRange; const [visualAnimationPreview, setVisualAnimationPreview] = useState(null); const visualLocalTime = Math.max(0, Math.min( selectedVisualSegment?.duration ?? 0, currentTime - (selectedVisualRange?.start ?? 0), )); const updateSelectedVisualEffects = (change) => { if (!selectedVisualSegment?.id || trackLocks.image) return notify("请先选择未锁定的 Visuals 片段"); setVisualSegments((items) => { const nextItems = items.map((item) => { if (item.id !== selectedVisualSegment.id) return item; if (Number.isFinite(change.playbackRate) && item.type === "video") return updateVisualSegmentPlaybackRate(item, change.playbackRate); if (change.keyframe) return { ...item, keyframes: upsertVisualKeyframe(item.keyframes, change.keyframe.time, change.keyframe) }; if (change.propertyKeyframe) return { ...item, keyframes: upsertVisualPropertyKeyframe(item.keyframes, change.propertyKeyframe.time, change.propertyKeyframe.key, change.propertyKeyframe.value) }; if (change.removePropertyKeyframe) return { ...item, keyframes: removeVisualPropertyKeyframe(item.keyframes, change.removePropertyKeyframe.time, change.removePropertyKeyframe.key) }; if (Number.isFinite(change.removeKeyframeAt)) return { ...item, keyframes: (item.keyframes ?? []).filter((frame) => Math.abs(frame.time - change.removeKeyframeAt) > 0.04) }; if (change.mask) return { ...item, mask: change.mask }; if (change.animation) return { ...item, animation: change.animation }; if (typeof change.enhancementEnabled === "boolean" && item.enhancement) { if (item.enhancement.mode === "remaster-drunet-full") { const source = change.enhancementEnabled ? item.enhancement.processed : item.enhancement.original; if (!source?.src) return item; return { ...item, src: source.src, blob: source.blob, width: source.width, height: source.height, sourceStart: source.sourceStart, sourceDuration: source.sourceDuration, trackFrames: source.trackFrames ?? [], enhancement: { ...item.enhancement, enabled: change.enhancementEnabled }, }; } if (item.enhancement.previewUrl) return { ...item, enhancement: { ...item.enhancement, enabled: change.enhancementEnabled } }; } return item; }); if (Number.isFinite(change.playbackRate)) { const nextSegment = nextItems.find((item) => item.id === selectedVisualSegment.id); const previousRate = Math.max(0.25, Math.min(4, Number(selectedVisualSegment.playbackRate) || 1)); const nextRate = Math.max(0.25, Math.min(4, Number(nextSegment?.playbackRate) || 1)); const nextDuration = getVisualSegmentsTotal(nextItems); setImageDuration(nextDuration); setImageClipCount(getImageThumbnailCount(nextDuration)); setCurrentTime((time) => Math.max( selectedVisualRange?.start ?? 0, Math.min( (selectedVisualRange?.start ?? 0) + (nextSegment?.duration ?? 0), (selectedVisualRange?.start ?? 0) + visualLocalTime * previousRate / nextRate, ), )); } return nextItems; }); }; const exportElapsedSeconds = useExportElapsed(exporting, exportStartRef); const { effectiveCaptionPlacement, previewSmartCropRect, previewVisionAnalysis, previewVisionFrameSize, previewVisionMaskUrl, previewVisionOptions, previewVisionOverlayBoxes, previewVisualObjectFit, previewVisualObjectPosition, previewVisualRenderSrc, } = usePreviewModel({ captionPlacement, captionSize, captionStyle, currentCaption, fitMode, previewFrameSize, previewVideoMediaTime, previewVisionBaseAnalysis, previewVisionRecord, previewVisualSourceTime, previewVisualSrc, previewVisualType, ratio, }); const { builtInAssets, filteredVoices } = useEditorCatalog(voiceFilter); const { canDropAssetOnTrack, findAssetById, getActiveDraggedAsset, getDraggedAsset, getTimelineDropPercent, handleAssetClick, handleAssetDragEnd, handleAssetDragStart, handleAssetPointerDown, handleStickerClick, handleTrackAssetDragLeave, handleTrackAssetDragOver, triggerAssetDropPulse, } = createAssetDragControls({ addStickerAssetToTimeline: (...args) => addStickerAssetToTimeline(...args), applyAssetToTrack: (...args) => applyAssetToTrack(...args), assetDropPulseTimerRef, builtInAssets, currentTime, draggedAssetId, draggedAssetIdRef, getStickerDragAsset, notify, pointerAssetDragRef, setAssetDragPreview, setAssetDropPosition, setAssetDropPulseTrack, setAssetDropTargetTrack, setDraggedAssetId, setSelectedLibraryAssetId, setSelectedStickerId, setSelectedStickerSegmentId, suppressAssetClickRef, t, trackLocks, trackScrollRef, userAssets, }); const analyzeCurrentVisual = useVisionAnalysis({ notify, previewVideoRef, previewVisionKey, previewVisualSegment, previewVisualSrc, previewVisualType, setVisionJob, setVisionRecords, visionAbortControllerRef, visionJob, visionJobGenerationRef, visionObjectUrlsRef, }); const { clearVisionAnalysis, downloadVisionCutout, removeVisionRecordsForAsset, setFitModeFromUser, toggleVisionOption, } = createVisionControls({ imageName, notify, previewVisionAnalysis, previewVisionBaseAnalysis, previewVisionKey, previewVisionOptions, previewVisionRecord, previewVisualSegment, previewVisualType, setFitMode, setVisionJob, setVisionRecords, visionAbortControllerRef, visionJob, visionJobGenerationRef, visionObjectUrlsRef, }); const { commitCaptionSegments, deleteCaptionSegment, handleCaptionPositionChange, startCaptionDrag, toggleCaptionSegmentHidden, updateCaptionSegmentText, updateScript, } = createCaptionEditingActions({ audioSegments, captionSegments, currentCaptionSegment, focusedSegmentIndex, notify, previewCanvasRef, previewVisionKey, previewVisionRecord, script, selectedSegmentId, setCaptionPlacement, setCaptionPosition, setCaptionSegments, setScript, setSelectedSegmentId, setSelectedTrack, setVisionRecords, trackLocks, }); const { clearAudioTrack, clearMusicTrack, clearSourceAudioTrack, commitAudio, replaceAudio, replaceMusic, replaceSourceAudio, } = createAudioTrackActions({ audioBlob, audioDuration, audioSegmentRefs, audioSegments, captionDuration, currentTimeRef, imageDuration, imageSrc, musicBlob, musicDuration, musicRef, musicUrlRef, notify, script, selectedVoice, selectedVoiceId, setActiveTool, setAudioSegments, setCaptionSegments, setCurrentTime, setHistoryItems, setIsPlaying, setMusicBlob, setMusicDuration, setMusicName, setMusicPeaks, setMusicStart, setMusicUrl, setProgress, setSelectedAudioSegmentId, setSelectedSegmentId, setSelectedTrack, setSourceAudioAssetId, setSourceAudioBlob, setSourceAudioDuration, setSourceAudioLinked, setSourceAudioName, setSourceAudioPeaks, setSourceAudioStart, setSourceAudioUrl, setSourceAudioVolume, setStatus, setStatusText, setTimelineHorizon, sourceAudioBlob, sourceAudioDuration, sourceAudioAssetId, sourceAudioRef, sourceAudioStart, sourceAudioUrlRef, t, }); const { separateSourceVocals, vocalSeparationJob } = useVocalSeparation({ sourceAudioBlob, sourceAudioName, replaceSourceAudio, replaceMusic, notify, t, }); const { chooseInterfaceLanguage, clearAllVisionState, selectTool, toggleTrackLock, toggleTrackVisibility, useHistoryItem, } = createEditorCommandActions({ notify, replaceAudio, script, setActiveTool, setAvatarPanelOpen, setCaptionSegments, setIntroClosing, setScript, setSelectedSegmentId, setSelectedTrack, setSelectedVoiceId, setTrackLocks, setTrackVisibility, setUiLanguage, setVisionJob, setVisionRecords, setVoiceTab, visionAbortControllerRef, visionJobGenerationRef, visionObjectUrlsRef, }); const { appendVisualAssetToTimeline, clearImageTrack, commitVisualSegments, getCurrentVisualAssetSnapshot, getVisualDurationForAsset, replaceVisualTimeline, setCurrentVisualAsset, updateVisualAssetInTimeline, } = createVisualTimelineActions({ audioBlob, audioDuration, captionDuration, extractVideoSourceAudio: (...args) => extractVideoSourceAudio(...args), imageDuration, imageMeta, imageName, imageSrc, musicBlob, musicDuration, notify, previewVisualSegment, script, seekTo: (...args) => seekTo(...args), setCurrentTime, setFitMode, setImageClipCount, setImageDuration, setImageMeta, setImageName, setImageSrc, setSelectedTrack, setSelectedVisualSegmentId, setVisualSegments, setTimelineZoom, setVisualType, sourceAudioBlob, sourceAudioDuration, sourceAudioStart, trackLocks, visualSegments, visualType, }); const { addStickerAssetToTimeline, commitStickerSegments, getTimelineTimeFromDropPercent, } = createStickerTimelineActions({ estimatedDuration, notify, seekTo: (...args) => seekTo(...args), setActiveTool, setSelectedStickerId, setSelectedStickerSegmentId, setSelectedTrack, setStickerSegments, stickerSegments, t, timelineDurationRef, trackLocks, }); const selectedStickerSegment = stickerSegments.find((segment) => segment.id === selectedStickerSegmentId) ?? currentStickerSegment; const updateSelectedStickerSegment = (change) => { if (!selectedStickerSegment?.id) return; setStickerSegments((segments) => segments.map((segment) => segment.id === selectedStickerSegment.id ? { ...segment, ...change } : segment)); }; const updateCanvasStickerSegment = (segmentId, change) => { if (!segmentId) return; setStickerSegments((segments) => segments.map((segment) => segment.id === segmentId ? { ...segment, ...change } : segment)); }; const selectCanvasStickerSegment = (segmentId) => { if (!segmentId) return; setSelectedStickerSegmentId(segmentId); setSelectedTrack("sticker"); }; const deleteSelectedStickerSegment = () => { if (!selectedStickerSegment?.id) return; commitStickerSegments(stickerSegments.filter((segment) => segment.id !== selectedStickerSegment.id), "已删除贴纸片段"); }; const { generateAvatarAcceptanceFrame, openAvatarPanel } = useAvatarGeneration({ audioBlob, audioDuration, avatarJob, avatarMotionCacheRef, avatarMotionWorkerRef, avatarRenderWorkerRef, imageDuration, imageUrlRefs, notify, previewVisualSegment, previewVisualSrc, previewVisualType, replaceVisualTimeline, setAvatarJob, setAvatarPanelOpen, setCurrentTime, setUserAssets, t, }); const { startVoiceRecording, stopVoiceRecording, useRecordedVoice } = useVoiceRecorder({ notify, recordingState, replaceAudio, setActiveTool, setProgress, setRecordedVoices, setRecordingElapsed, setRecordingState, setSelectedTrack, setStatus, setStatusText, setVoiceTab, t, voiceRecorderChunksRef, voiceRecorderRef, voiceRecorderStartedAtRef, voiceRecorderStreamRef, voiceRecorderTimerRef, }); const generateVoiceover = useVoiceGeneration({ commitAudio, notify, script, selectedVoice, setProgress, setStatus, setStatusText, setVoiceTab, speed, status, t, }); const { deleteAudioSegment, toggleAudioSegmentReverse, updateAudioSegment } = createAudioClipActions({ audioSegmentRefs, audioSegments, notify, setAudioSegments, setCaptionSegments, setSelectedAudioSegmentId, setTimelineHorizon, t, }); const { handleAddSegment, handleRemoveSegment } = createTimelineSegmentCountActions({ captionSegments, clearImageTrack, commitCaptionSegments, commitStickerSegments, commitVisualSegments, currentStickerSegmentIndex, currentTime, currentVisualSegmentIndex, deleteCaptionSegment, focusedSegmentIndex, getCurrentVisualAssetSnapshot, getStickerDragAsset, imageClipCount, imageDuration, imageSrc, notify, selectedSegmentId, selectedSegmentIndex, selectedSticker, selectedStickerSegmentId, selectedTrack, selectedVisualSegmentId, selectedVisualSegmentIndex, stickerSegments, t, trackLocks, visualSegments, }); const adjustSelectedSegmentWeight = createTimelineDurationActions({ captionSegments, commitCaptionSegments, commitStickerSegments, commitVisualSegments, currentStickerSegmentIndex, currentVisualSegmentIndex, focusedSegmentIndex, getCurrentVisualAssetSnapshot, imageDuration, imageSrc, notify, selectedSegmentId, selectedSegmentIndex, selectedStickerSegmentId, selectedTrack, selectedVisualSegmentId, selectedVisualSegmentIndex, stickerSegments, trackLocks, visualSegments, }); const { handleDeleteTrack, handleDuplicateTrack } = createTimelineClipboardActions({ audioBlob, captionSegments, clearImageTrack, clearMusicTrack, clearSourceAudioTrack, commitCaptionSegments, commitStickerSegments, commitVisualSegments, currentStickerSegmentIndex, currentVisualSegmentIndex, deleteAudioSegment, focusedSegmentIndex, getCurrentVisualAssetSnapshot, handleRemoveSegment, imageClipCount, imageDuration, imageMeta, imageName, imageSrc, musicBlob, musicName, notify, selectedAudioSegment, selectedAudioSegmentId, selectedSegmentId, selectedSegmentIndex, selectedStickerSegmentId, selectedTrack, selectedVisualSegmentId, selectedVisualSegmentIndex, setAudioSegments, setCaptionSegments, setSelectedAudioSegmentId, setUserAssets, sourceAudioBlob, sourceAudioName, stickerSegments, t, trackLocks, visualSegments, visualType, }); useEditorLifecycle({ activeLanguage, audioSegments, audioUrlRef, autoRatioSourceKeyRef, avatarMotionWorkerRef, avatarRenderWorkerRef, avatarTestAudioImportedRef, avatarTestImportedRef, captionSegments, currentVisualSegment, handleDeleteTrack, imageUrlRefs, musicBlob, musicUrlRef, notify, ratioId, replaceAudio, replaceVisualTimeline, selectedAudioSegmentId, selectedSegmentId, selectedStickerSegmentId, selectedTrack, selectedVisualSegmentId, setCurrentVisualAsset, setFitMode, setRatioId, setSelectedSegmentId, setSelectedVisualSegmentId, setUserAssets, sourceAudioBlob, sourceAudioUrlRef, stickerSegments, visionAbortControllerRef, visionObjectUrlsRef, visualSegments, voiceRecorderStreamRef, voiceRecorderTimerRef, }); const { handleCutTrack } = createTimelineCutActions({ captionSegments, commitCaptionSegments, commitStickerSegments, commitVisualSegments, currentStickerSegmentIndex, currentTime, focusedSegmentIndex, getCurrentVisualAssetSnapshot, imageDuration, imageSrc, notify, selectedSegmentId, selectedSegmentIndex, selectedStickerSegmentId, selectedTrack, stickerSegments, trackLocks, visualSegments, }); const { getTimelineTimeFromClientX, handlePlayToggle, pauseTimelineMedia, seekTo, startTimelineSeek } = createPlaybackControls({ audioSegmentRefs, audioSegments, canPreview, currentTimeRef, currentVisualRange, estimatedDuration, isPlaying, musicDuration, musicRef, musicStart, musicUrl, notify, linkedSourceAudioSegments, previewVideoRef, previewVisualType, setCurrentTime, setIsPlaying, sourceAudioDuration, sourceAudioLinked, sourceAudioRef, sourceAudioStart, sourceAudioUrl, timelineDuration, timelineDurationRef, trackScrollRef, trackVisibility, visualSegments, visualTimeline, }); const pauseForTimelineEdit = () => { if (!isPlaying) return; pauseTimelineMedia(); setIsPlaying(false); }; useMediaSync({ audioRef, audioSegmentRefs, audioSegments, currentTime, currentTimeRef, estimatedDuration, isPlaying, musicDuration, musicRef, musicStart, musicUrl, musicVolume, pauseTimelineMedia, previewVideoRef, previewVisualSegment, previewVisualSourceTime, previewVisualSrc, previewVisualType, linkedSourceAudioSegments, setCurrentTime, setIsPlaying, setPreviewVideoMediaTime, sourceAudioDuration, sourceAudioLinked, sourceAudioRef, sourceAudioStart, sourceAudioUrl, sourceAudioVolume, timelineDuration, trackVisibility, visualPlaybackFrameRef, visualPlaybackLastUpdateRef, visualPlaybackStartedAtRef, visualPlaybackStartTimeRef, }); const { startAudioSegmentMove, startMusicMove, startSourceAudioMove, startStickerSegmentMove } = createTimelineMoveControls({ audioSegments, captionSegments, captionTargetDuration, estimatedDuration, notify, seekTo, setActiveTool, setAudioSegments, setCaptionSegments, setSelectedAudioSegmentId, setSelectedStickerId, setSelectedStickerSegmentId, setSelectedTrack, setStickerSegments, setTimelineHorizon, setMusicStart, setSourceAudioLinked, setSourceAudioStart, musicDuration, musicStart, sourceAudioDuration, sourceAudioStart, stickerSegments, suppressTimelineClipClickRef, t, timelineDurationRef, trackLocks, trackScrollRef, pauseForTimelineEdit, visualSegments, setSnapGuide, }); const startImageResize = createImageResizeControl({ audioBlob, audioDuration, captionDuration, getCurrentVisualAssetSnapshot, imageDuration, imageSrc, musicBlob, musicDuration, notify, script, setCurrentTime, setImageClipCount, setImageDuration, setSelectedTrack, setSelectedVisualSegmentId, setSnapGuide, setVisualSegments, sourceAudioBlob, sourceAudioDuration, sourceAudioStart, timelineDuration, timelineDurationRef, trackLocks, trackScrollRef, visualSegments, pauseForTimelineEdit, }); const extractVideoSourceAudio = useSourceAudioExtraction({ clearSourceAudioTrack, notify, replaceSourceAudio, setProgress, setStatus, setStatusText, setVisualSegments, sourceAudioBlob, sourceAudioDuration, }); const generateCaptionsFromSourceAudio = useAutoCaptions({ notify, script, seekTo, setActiveTool, setCaptionSegments, setCaptionsEnabled, setProgress, setScript, setSelectedSegmentId, setSelectedTrack, setStatus, setStatusText, setTrackVisibility, sourceAudioBlob, sourceAudioStart, status, trackLocks, uiLanguage, }); const handleFiles = useFileUpload({ appendVisualAssetToTimeline, imageUrlRefs, notify, setSelectedLibraryAssetId, setSelectedTrack, setUserAssets, updateVisualAssetInTimeline, }); const { deleteUserAsset, selectAsset } = createAssetLibraryActions({ clearImageTrack, clearMusicTrack, clearSourceAudioTrack, commitVisualSegments, extractVideoSourceAudio, getVisualDurationForAsset, imageSrc, imageUrlRefs, musicBlob, notify, removeVisionRecordsForAsset, replaceMusic, replaceVisualTimeline, selectedLibraryAssetId, setSelectedLibraryAssetId, setUserAssets, sourceAudioBlob, userAssets, visualSegments, }); const { applyAssetToTrack, handleTrackAssetDrop, handleVisualStyleDrop } = createAssetDropActions({ addStickerAssetToTimeline, appendVisualAssetToTimeline, canDropAssetOnTrack, draggedAssetIdRef, extractVideoSourceAudio, getDraggedAsset, getTimelineDropPercent, notify, replaceAudio, selectAsset, setActiveTool, setAssetDropPosition, setAssetDropTargetTrack, setDraggedAssetId, setSelectedFilterId, setSelectedLibraryAssetId, setSelectedTrack, setSelectedTransitionId, setSelectedVisualSegmentId, setVisualSegments, trackScrollRef, triggerAssetDropPulse, }); const { handleExportProject, handleImportProject, handleNewProject } = useProjectFiles({ audioBlob, audioDuration, captionPlacement, captionPosition, captionSegments, captionSize, captionStyle, captionsEnabled, captionStyleFallback: captionStyle, clearAllVisionState, clearAudioTrack, clearImageTrack, clearMusicTrack, clearSourceAudioTrack, fitMode, imageUrlRefs, musicBlob, musicDuration, musicName, musicStart, musicVolume, notify, projectFileInputRef, ratioId, replaceAudio, replaceMusic, replaceSourceAudio, script, selectedFilterId, selectedStickerId, selectedTransitionId, selectedVoiceId, setCaptionPlacement, setCaptionPosition, setCaptionSegments, setCaptionSize, setCaptionStyle, setCaptionsEnabled, setCurrentTime, setFitMode, setImageClipCount, setImageDuration, setMusicStart, setMusicVolume, setRatioId, setScript, setSelectedFilterId, setSelectedSegmentId, setSelectedStickerId, setSelectedStickerSegmentId, setSelectedTransitionId, setSelectedVoiceId, setShowFileMenu, setSourceAudioAssetId, setSourceAudioLinked, setSourceAudioVolume, setSpeed, setStickerSegments, setTimelineZoom, setTrackVisibility, setTimelineHorizon, setVisualSegments, setVolume, setCurrentVisualAsset, sourceAudioBlob, sourceAudioDuration, markTimelineViewRestored: (hasContent) => { timelineImportRestoreRef.current = hasContent; }, sourceAudioAssetId, sourceAudioLinked, sourceAudioName, sourceAudioStart, sourceAudioVolume, speed, stickerSegments, timelineZoom, trackVisibility, visualSegments, volume, }); const { activeTimelineClipDrag, audioClipPercent, displayedCaptionSegments, displayedCaptionTimeline, displayedVisualSegments, exportPercent, musicClipPercent, musicStartPercent, playheadPercent, previewFrameStyle, previewRatio, progressPercent, renderedVisualSegments, renderedVisualTimeline, showStickerTrack, sourceAudioClipPercent, sourceAudioStartPercent, } = createTimelineViewModel({ assetDragPreview, assetDropTargetTrack, audioBlob, audioDuration, captionSegments, captionTargetDuration, captionTimeline, currentTime, draggedAssetId, exportProgress, findAssetById, getCurrentVisualAssetSnapshot, imageDuration, imageSrc, musicBlob, musicDuration, musicStart, previewFrameSize, progress, ratio, selectedTrack, sourceAudioBlob, linkedSourceAudioSegments, sourceAudioDuration, sourceAudioLinked, sourceAudioStart, stickerSegments, timelineClipDrag, timelineDuration, visualSegments, }); const handleExportVideo = useVideoExport({ audioSegments, captionDuration, captionPlacement, captionPosition, captionSegments, captionSize, captionStyle, captionsEnabled, exporting, exportStartRef, fitMode, imageDuration, imageSrc, musicBlob, musicDuration, musicStart, musicVolume, notify, previewFrameSize, ratio, renderedVisualSegments, script, selectedFilter, selectedSticker, selectedTransitionId, setExporting, setExportPhase, setExportProgress, setStatus, setStatusText, sourceAudioBlob, sourceAudioDuration, linkedSourceAudioSegments, sourceAudioLinked, sourceAudioStart, sourceAudioTimelineEnd, sourceAudioVolume, stickerDuration, stickerSegments, trackVisibility, visionRecords, visualType, voiceTrackDuration, volume, exportSettings: { ...exportSettings, ...getExportDimensions(ratio, Number(exportSettings.resolution)), videoBitsPerSecond: getExportBitrate(Number(exportSettings.resolution), exportSettings.quality, exportSettings.frameRate), }, }); const { startCaptionResize, startTimelineClipDrag } = createTimelineReorderControls({ audioSegments, captionSegments, captionTargetDuration, commitCaptionSegments, commitVisualSegments, notify, renderedVisualSegments, seekTo, setSelectedSegmentId, setSelectedTrack, setSelectedVisualSegmentId, setTimelineClipDrag, suppressTimelineClipClickRef, timelineClipDragRef, timelineDuration, trackLocks, visualSegments, pauseForTimelineEdit, stickerSegments, sourceAudioDuration, sourceAudioStart, musicDuration, musicStart, setSnapGuide, }); return (
{ if (event.dataTransfer?.types?.includes("Files")) event.preventDefault(); }} onDrop={async (event) => { const files = Array.from(event.dataTransfer?.files ?? []); if (!files.length) return; event.preventDefault(); const audioFile = files.find((file) => file.type.startsWith("audio/") || /\.(mp3|wav|m4a|aac|ogg)$/i.test(file.name)); const targetTrack = event.target.closest?.("[data-asset-drop-track]")?.dataset.assetDropTrack; if (audioFile && targetTrack === "music") { try { const decoded = await decodeWaveform(audioFile, 96); replaceMusic(audioFile, decoded.duration, decoded.peaks, audioFile.name, "音乐已安全加入时间线"); } catch (error) { notify(error instanceof Error ? `无法读取该音频:${error.message}` : "无法读取该音频文件"); } return; } handleFiles(files); }}>
updateSelectedVisualEffects({ mask })} previewRatio={previewRatio} previewFrameStyle={previewFrameStyle} previewFrameSize={previewFrameSize} trackVisibility={trackVisibility} fileInputRef={fileInputRef} selectedFilter={activePreviewFilter} fitMode={fitMode} visualObjectFit={previewVisualObjectFit} visualObjectPosition={previewVisualObjectPosition} visionOverlayBoxes={previewVisionOverlayBoxes} showVisionOverlays={previewVisionOptions.showDetections} backgroundRemoved={ previewVisionOptions.removeBackground && Boolean(previewVisionAnalysis?.cutoutUrl) } smartCropActive={Boolean(previewSmartCropRect)} captionAvoidanceActive={ previewVisionOptions.avoidCaptions && Boolean(previewVisionAnalysis?.subject?.box) } setFitMode={setFitModeFromUser} captionsEnabled={captionsEnabled} currentCaption={currentCaption} captionSize={captionSize} captionStyle={captionStyle} captionPlacement={effectiveCaptionPlacement} startCaptionDrag={startCaptionDrag} setActiveTool={setActiveTool} selectedSticker={previewSticker} stickers={previewStickers} selectedStickerId={selectedStickerSegment?.id ?? ""} stickerEditable onSelectSticker={selectCanvasStickerSegment} onUpdateSticker={updateCanvasStickerSegment} isPlaying={isPlaying} canPreview={canPreview} handlePlayToggle={handlePlayToggle} estimatedDuration={estimatedDuration} currentTime={currentTime} seekTo={seekTo} notify={notify} getDraggedAsset={getDraggedAsset} applyAssetToTrack={applyAssetToTrack} />
setCaptionVoiceFocusRequest((request) => request + 1)} trackVisibility={trackVisibility} toggleTrackVisibility={toggleTrackVisibility} trackLocks={trackLocks} toggleTrackLock={toggleTrackLock} trackScrollRef={trackScrollRef} startTimelineSeek={startTimelineSeek} timelineDuration={timelineDuration} currentTime={currentTime} playheadPercent={playheadPercent} snapGuide={snapGuide} assetDropTargetTrack={assetDropTargetTrack} assetDropPosition={assetDropPosition} assetDropPulseTrack={assetDropPulseTrack} assetDragPreview={assetDragPreview} handleTrackAssetDragOver={handleTrackAssetDragOver} handleTrackAssetDragLeave={handleTrackAssetDragLeave} handleTrackAssetDrop={handleTrackAssetDrop} handleVisualStyleDrop={handleVisualStyleDrop} activeTimelineClipDrag={activeTimelineClipDrag} showStickerTrack={showStickerTrack} stickerSegments={stickerSegments} currentStickerSegment={currentStickerSegment} selectedStickerSegmentId={selectedStickerSegmentId} setSelectedStickerSegmentId={setSelectedStickerSegmentId} imageSrc={imageSrc} displayedVisualSegments={displayedVisualSegments} setVisualSegments={setVisualSegments} renderedVisualTimeline={renderedVisualTimeline} visualType={visualType} currentVisualSegment={currentVisualSegment} selectedVisualSegmentId={selectedVisualSegmentId} currentVisualSegmentIndex={currentVisualSegmentIndex} setSelectedVisualSegmentId={setSelectedVisualSegmentId} seekTo={seekTo} suppressTimelineClipClickRef={suppressTimelineClipClickRef} startTimelineClipDrag={startTimelineClipDrag} startCaptionResize={startCaptionResize} startImageResize={startImageResize} startStickerSegmentMove={startStickerSegmentMove} displayedCaptionSegments={displayedCaptionSegments} displayedCaptionTimeline={displayedCaptionTimeline} currentCaptionSegment={currentCaptionSegment} selectedSegmentId={selectedSegmentId} setSelectedSegmentId={setSelectedSegmentId} captionTargetDuration={captionTargetDuration} sourceAudioLinked={sourceAudioLinked} setSourceAudioLinked={setSourceAudioLinked} linkedSourceAudioSegments={linkedSourceAudioSegments} sourceAudioBlob={sourceAudioBlob} sourceAudioPeaks={sourceAudioPeaks} sourceAudioClipPercent={sourceAudioClipPercent} sourceAudioStartPercent={sourceAudioStartPercent} sourceAudioDuration={sourceAudioDuration} audioBlob={audioBlob} peaks={peaks} audioClipPercent={audioClipPercent} audioDuration={audioDuration} audioSegments={audioSegments} selectedAudioSegmentId={selectedAudioSegmentId} setSelectedAudioSegmentId={setSelectedAudioSegmentId} startAudioSegmentMove={startAudioSegmentMove} startSourceAudioMove={startSourceAudioMove} musicBlob={musicBlob} musicPeaks={musicPeaks} musicClipPercent={musicClipPercent} musicStartPercent={musicStartPercent} musicDuration={musicDuration} startMusicMove={startMusicMove} /> {shouldShowLanguageIntro ? ( ) : null} {toast ?
{toast}
: null}
); }