Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 712ec5b2f1 | |||
| 34c0af6ef3 | |||
| 156375c60d | |||
| 680074e921 | |||
| 39187a0660 | |||
| f0bbedab23 | |||
| 750444c8bb | |||
| 581a146d2f | |||
| eab45e0819 | |||
| e243d70082 | |||
| d4c3743e45 | |||
| 07d9b56fe7 | |||
| 11b3f83368 | |||
| 1dc40b4760 | |||
| 29448aaf9b | |||
| c73c4be367 | |||
| c31bc96855 | |||
| 002384bad4 | |||
| db6b742ebb | |||
| 88ca8b4406 | |||
| 005b34d0dd | |||
| e2ddc679bb | |||
| 39990f7a07 | |||
| 6fcb4cd70c | |||
| 14fefc7b1a | |||
| fa8ab58fc0 | |||
| 4765d83c8b | |||
| 395bc37cf0 | |||
| 15b6e17552 | |||
| a6e147ed30 | |||
| 0d46d71b2d | |||
| b40ae9cec7 | |||
| eb636b8fef | |||
| 09b069a65d | |||
| 378e4c8751 | |||
| f9afdd95ce | |||
| 456718ad84 | |||
| edd4b6b1ea | |||
| 4578b65965 |
@@ -1805,7 +1805,7 @@ jobs:
|
||||
echo "❌ CI Gate: FAILED"
|
||||
echo "失败项: ${FAILED_ITEMS[*]}"
|
||||
echo "gate_result=failure" >> $GITHUB_OUTPUT
|
||||
exit 0
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Report CI trace
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import React from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
import { MATERIAL_TYPE_LABELS, MATERIAL_TYPE_ICONS } from "@/api/template-editor"
|
||||
import { formatSize, formatDuration, getQualityLevel } from "./utils"
|
||||
|
||||
interface AssetCardProps {
|
||||
asset: MediaAsset
|
||||
isSelected: boolean
|
||||
isDragging: boolean
|
||||
isDragOver: boolean
|
||||
showCheckbox: boolean
|
||||
compact?: boolean
|
||||
onToggleSelect: (asset: MediaAsset, shiftKey: boolean) => void
|
||||
onCardClick: (asset: MediaAsset, e: React.MouseEvent) => void
|
||||
onDragStart: (e: React.DragEvent) => void
|
||||
onDragOver: (e: React.DragEvent) => void
|
||||
onDrop: (e: React.DragEvent) => void
|
||||
onDragEnd: () => void
|
||||
onMouseEnter: (e: React.MouseEvent) => void
|
||||
onMouseLeave: () => void
|
||||
}
|
||||
|
||||
const AssetCard: React.FC<AssetCardProps> = ({
|
||||
asset,
|
||||
isSelected,
|
||||
isDragging,
|
||||
isDragOver,
|
||||
showCheckbox,
|
||||
onToggleSelect,
|
||||
onCardClick,
|
||||
onDragStart,
|
||||
onDragOver,
|
||||
onDrop,
|
||||
onDragEnd,
|
||||
onMouseEnter,
|
||||
onMouseLeave,
|
||||
}) => {
|
||||
const qualityLevel = getQualityLevel(asset.quality_score)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"as-card",
|
||||
isSelected ? "selected" : "",
|
||||
isDragging ? "dragging" : "",
|
||||
isDragOver ? "drag-over" : "",
|
||||
showCheckbox ? "has-checkbox" : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
draggable
|
||||
onDragStart={onDragStart}
|
||||
onDragOver={onDragOver}
|
||||
onDrop={onDrop}
|
||||
onDragEnd={onDragEnd}
|
||||
onClick={(e) => onCardClick(asset, e)}
|
||||
onMouseEnter={onMouseEnter}
|
||||
onMouseLeave={onMouseLeave}
|
||||
>
|
||||
{/* 缩略图 */}
|
||||
<div className="as-card-thumb">
|
||||
{asset.thumbnail_url ? (
|
||||
<img src={asset.thumbnail_url} alt={asset.name} loading="lazy" />
|
||||
) : (
|
||||
<span className="as-card-thumb-icon">{MATERIAL_TYPE_ICONS[asset.type]}</span>
|
||||
)}
|
||||
|
||||
{/* Checkbox */}
|
||||
{showCheckbox && (
|
||||
<span
|
||||
data-checkbox
|
||||
className={`as-card-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggleSelect(asset, e.shiftKey)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 类型角标 */}
|
||||
<span className="as-card-type-badge">{MATERIAL_TYPE_LABELS[asset.type]}</span>
|
||||
|
||||
{/* 时长角标 */}
|
||||
{asset.duration != null && (
|
||||
<span className="as-card-duration">{formatDuration(asset.duration)}</span>
|
||||
)}
|
||||
|
||||
{/* 质量分角标 */}
|
||||
{asset.quality_score != null && (
|
||||
<span
|
||||
className={`as-card-quality ${qualityLevel}`}
|
||||
title={`质量分: ${asset.quality_score}`}
|
||||
>
|
||||
{asset.quality_score}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 信息 */}
|
||||
<div className="as-card-info">
|
||||
<p className="as-card-name" title={asset.name}>
|
||||
{asset.name}
|
||||
</p>
|
||||
<div className="as-card-meta">{formatSize(asset.size)}</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default AssetCard
|
||||
@@ -0,0 +1,99 @@
|
||||
import React from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
import { MATERIAL_TYPE_LABELS, MATERIAL_TYPE_ICONS } from "@/api/template-editor"
|
||||
import { formatSize, formatDuration, getQualityColor } from "./utils"
|
||||
|
||||
interface AssetListItemProps {
|
||||
asset: MediaAsset
|
||||
isSelected: boolean
|
||||
isDragging: boolean
|
||||
isDragOver: boolean
|
||||
showCheckbox: boolean
|
||||
onToggleSelect: (asset: MediaAsset, shiftKey: boolean) => void
|
||||
onCardClick: (asset: MediaAsset, e: React.MouseEvent) => void
|
||||
onDragStart: (e: React.DragEvent) => void
|
||||
onDragOver: (e: React.DragEvent) => void
|
||||
onDrop: (e: React.DragEvent) => void
|
||||
onDragEnd: () => void
|
||||
onMouseEnter: (e: React.MouseEvent) => void
|
||||
onMouseLeave: () => void
|
||||
}
|
||||
|
||||
const AssetListItem: React.FC<AssetListItemProps> = ({
|
||||
asset,
|
||||
isSelected,
|
||||
isDragging,
|
||||
isDragOver,
|
||||
showCheckbox,
|
||||
onToggleSelect,
|
||||
onCardClick,
|
||||
onDragStart,
|
||||
onDragOver,
|
||||
onDrop,
|
||||
onDragEnd,
|
||||
onMouseEnter,
|
||||
onMouseLeave,
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"as-list-item",
|
||||
isSelected ? "selected" : "",
|
||||
isDragging ? "dragging" : "",
|
||||
isDragOver ? "drag-over" : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
draggable
|
||||
onDragStart={onDragStart}
|
||||
onDragOver={onDragOver}
|
||||
onDrop={onDrop}
|
||||
onDragEnd={onDragEnd}
|
||||
onClick={(e) => onCardClick(asset, e)}
|
||||
onMouseEnter={onMouseEnter}
|
||||
onMouseLeave={onMouseLeave}
|
||||
>
|
||||
{/* 拖拽手柄 */}
|
||||
<span className="as-list-item-drag" title="拖拽排序">
|
||||
⠿
|
||||
</span>
|
||||
|
||||
{/* Checkbox */}
|
||||
{showCheckbox && (
|
||||
<span
|
||||
data-checkbox
|
||||
className={`as-list-item-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggleSelect(asset, e.shiftKey)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 图标 */}
|
||||
<span className="as-list-item-icon">{MATERIAL_TYPE_ICONS[asset.type]}</span>
|
||||
|
||||
{/* 信息 */}
|
||||
<div className="as-list-item-info">
|
||||
<div className="as-list-item-name">{asset.name}</div>
|
||||
<div className="as-list-item-meta">
|
||||
{MATERIAL_TYPE_LABELS[asset.type]}
|
||||
{asset.duration != null && ` · ${formatDuration(asset.duration)}`}
|
||||
{asset.size != null && ` · ${formatSize(asset.size)}`}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 质量分 */}
|
||||
{asset.quality_score != null && (
|
||||
<span
|
||||
className="as-list-item-quality"
|
||||
style={{ color: getQualityColor(asset.quality_score) }}
|
||||
>
|
||||
{asset.quality_score}分
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default AssetListItem
|
||||
@@ -9,73 +9,20 @@
|
||||
* - 筛选增强:类型筛选 + 质量分筛选
|
||||
* - 视图切换:网格视图 / 列表视图
|
||||
*/
|
||||
import React, { useState, useMemo, useCallback, useRef, useEffect } from "react"
|
||||
import React, { useCallback } from "react"
|
||||
import "./AssetSelector.css"
|
||||
import { Input, Select, Button } from "@/components/ui"
|
||||
import type { AssetSelectorProps } from "./types"
|
||||
import useAssetFilter from "./hooks/useAssetFilter"
|
||||
import useAssetSelection from "./hooks/useAssetSelection"
|
||||
import useDragReorder from "./hooks/useDragReorder"
|
||||
import useAssetPreview from "./hooks/useAssetPreview"
|
||||
import SelectorToolbar from "./SelectorToolbar"
|
||||
import SelectorBatchBar from "./SelectorBatchBar"
|
||||
import AssetCard from "./AssetCard"
|
||||
import AssetListItem from "./AssetListItem"
|
||||
import PreviewOverlay from "./PreviewOverlay"
|
||||
import EmptyState from "./EmptyState"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
import { MATERIAL_TYPE_LABELS, MATERIAL_TYPE_ICONS, QUALITY_OPTIONS } from "@/api/template-editor"
|
||||
|
||||
/* ──────────── 类型 ──────────── */
|
||||
|
||||
export interface AssetSelectorProps {
|
||||
assets: MediaAsset[]
|
||||
selectedIds?: string[]
|
||||
onSelectionChange?: (ids: string[]) => void
|
||||
onAssetDragStart?: (asset: MediaAsset) => void
|
||||
onReorder?: (fromIdx: number, toIdx: number) => void
|
||||
showQualityFilter?: boolean
|
||||
showBatchSelect?: boolean
|
||||
compact?: boolean
|
||||
}
|
||||
|
||||
type ViewMode = "grid" | "list"
|
||||
|
||||
/* ──────────── 工具函数 ──────────── */
|
||||
|
||||
/** 格式化文件大小 */
|
||||
const formatSize = (bytes?: number): string => {
|
||||
if (!bytes) return ""
|
||||
if (bytes < 1024) return `${bytes}B`
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
|
||||
}
|
||||
|
||||
/** 格式化时长 */
|
||||
const formatDuration = (seconds?: number): string => {
|
||||
if (!seconds) return ""
|
||||
const m = Math.floor(seconds / 60)
|
||||
const s = Math.floor(seconds % 60)
|
||||
return m > 0 ? `${m}:${s.toString().padStart(2, "0")}` : `${s}s`
|
||||
}
|
||||
|
||||
/** 获取质量分等级 */
|
||||
const getQualityLevel = (score?: number): string => {
|
||||
if (score == null) return "none"
|
||||
if (score >= 90) return "excellent"
|
||||
if (score >= 70) return "good"
|
||||
if (score >= 50) return "fair"
|
||||
return "poor"
|
||||
}
|
||||
|
||||
/** 质量分颜色 */
|
||||
const getQualityColor = (score?: number): string => {
|
||||
if (score == null) return "var(--text-secondary)"
|
||||
if (score >= 90) return "var(--success-color, #10b981)"
|
||||
if (score >= 70) return "var(--primary-color, #6366f1)"
|
||||
if (score >= 50) return "var(--warning-color, #f59e0b)"
|
||||
return "var(--error-color, #ef4444)"
|
||||
}
|
||||
|
||||
/* ──────────── 类型筛选选项 ──────────── */
|
||||
|
||||
const TYPE_OPTIONS = [
|
||||
{ value: "", label: "全部类型" },
|
||||
{ value: "video", label: "🎬 视频" },
|
||||
{ value: "image", label: "🖼️ 图片" },
|
||||
{ value: "audio", label: "🎵 音频" },
|
||||
]
|
||||
|
||||
/* ──────────── 组件 ──────────── */
|
||||
|
||||
const AssetSelector: React.FC<AssetSelectorProps> = ({
|
||||
assets,
|
||||
@@ -87,162 +34,41 @@ const AssetSelector: React.FC<AssetSelectorProps> = ({
|
||||
showBatchSelect = true,
|
||||
compact = false,
|
||||
}) => {
|
||||
/* ── 搜索 & 筛选 ── */
|
||||
const [searchText, setSearchText] = useState("")
|
||||
const [filterType, setFilterType] = useState("")
|
||||
const [filterQuality, setFilterQuality] = useState("")
|
||||
const [viewMode, setViewMode] = useState<ViewMode>("grid")
|
||||
// 搜索 & 筛选
|
||||
const {
|
||||
searchText,
|
||||
filterType,
|
||||
filterQuality,
|
||||
viewMode,
|
||||
filteredAssets,
|
||||
setSearchText,
|
||||
setFilterType,
|
||||
setFilterQuality,
|
||||
setViewMode,
|
||||
} = useAssetFilter(assets)
|
||||
|
||||
/* ── 拖拽状态 ── */
|
||||
const [dragIdx, setDragIdx] = useState<number | null>(null)
|
||||
const [dragOverIdx, setDragOverIdx] = useState<number | null>(null)
|
||||
// 选中操作
|
||||
const { selectedSet, toggleSelect, clearSelection } = useAssetSelection({
|
||||
filteredAssets,
|
||||
selectedIds,
|
||||
onSelectionChange,
|
||||
})
|
||||
|
||||
/* ── 悬浮预览 ── */
|
||||
const [previewAsset, setPreviewAsset] = useState<MediaAsset | null>(null)
|
||||
const [previewPos, setPreviewPos] = useState({ x: 0, y: 0 })
|
||||
const previewTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
// 拖拽排序
|
||||
const { dragIdx, dragOverIdx, handleDragStart, handleDragOver, handleDrop, handleDragEnd } =
|
||||
useDragReorder({
|
||||
filteredAssets,
|
||||
selectedIds,
|
||||
onAssetDragStart,
|
||||
onReorder,
|
||||
})
|
||||
|
||||
/* ── Shift 连选 ── */
|
||||
const lastClickedIdx = useRef<number | null>(null)
|
||||
|
||||
/* ── 过滤后的素材列表 ── */
|
||||
const filteredAssets = useMemo(() => {
|
||||
let list = assets
|
||||
if (searchText) {
|
||||
const q = searchText.toLowerCase()
|
||||
list = list.filter(
|
||||
(a) => a.name.toLowerCase().includes(q) || a.tags.some((t) => t.toLowerCase().includes(q)),
|
||||
)
|
||||
}
|
||||
if (filterType) {
|
||||
list = list.filter((a) => a.type === filterType)
|
||||
}
|
||||
if (filterQuality) {
|
||||
const opt = QUALITY_OPTIONS.find((o) => o.value === filterQuality)
|
||||
if (opt?.min != null && opt?.max != null) {
|
||||
list = list.filter(
|
||||
(a) =>
|
||||
a.quality_score != null && a.quality_score >= opt.min! && a.quality_score <= opt.max!,
|
||||
)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}, [assets, searchText, filterType, filterQuality])
|
||||
|
||||
/* ── 选中状态 ── */
|
||||
const selectedSet = useMemo(() => new Set(selectedIds), [selectedIds])
|
||||
|
||||
/* ── 选择操作 ── */
|
||||
const toggleSelect = useCallback(
|
||||
(asset: MediaAsset, idx: number, shiftKey: boolean) => {
|
||||
if (!onSelectionChange) return
|
||||
|
||||
if (shiftKey && lastClickedIdx.current !== null) {
|
||||
// Shift 连选
|
||||
const start = Math.min(lastClickedIdx.current, idx)
|
||||
const end = Math.max(lastClickedIdx.current, idx)
|
||||
const rangeIds = filteredAssets.slice(start, end + 1).map((a) => a.id)
|
||||
const newSet = new Set(selectedIds)
|
||||
rangeIds.forEach((id) => newSet.add(id))
|
||||
onSelectionChange(Array.from(newSet))
|
||||
} else {
|
||||
const newSet = new Set(selectedIds)
|
||||
if (newSet.has(asset.id)) {
|
||||
newSet.delete(asset.id)
|
||||
} else {
|
||||
newSet.add(asset.id)
|
||||
}
|
||||
onSelectionChange(Array.from(newSet))
|
||||
}
|
||||
lastClickedIdx.current = idx
|
||||
},
|
||||
[onSelectionChange, selectedIds, filteredAssets],
|
||||
)
|
||||
|
||||
const clearSelection = useCallback(() => {
|
||||
onSelectionChange?.([])
|
||||
}, [onSelectionChange])
|
||||
|
||||
/* ── 拖拽排序 ── */
|
||||
const handleDragStart = useCallback(
|
||||
(e: React.DragEvent, idx: number) => {
|
||||
setDragIdx(idx)
|
||||
e.dataTransfer.effectAllowed = "move"
|
||||
e.dataTransfer.setData("text/plain", String(idx))
|
||||
// 设置素材数据,供 TimelinePanel 接收(P1-2 修复)
|
||||
e.dataTransfer.setData("application/x-media-asset", JSON.stringify(filteredAssets[idx]))
|
||||
// 批量拖拽:如果有多个选中素材,一起携带
|
||||
if (selectedIds.length > 1 && selectedIds.includes(filteredAssets[idx].id)) {
|
||||
const batchAssets = filteredAssets.filter((a) => selectedIds.includes(a.id))
|
||||
e.dataTransfer.setData("application/x-media-assets", JSON.stringify(batchAssets))
|
||||
}
|
||||
// 通知父组件素材拖拽开始
|
||||
if (onAssetDragStart) {
|
||||
onAssetDragStart(filteredAssets[idx])
|
||||
}
|
||||
},
|
||||
[filteredAssets, selectedIds, onAssetDragStart],
|
||||
)
|
||||
|
||||
const handleDragOver = useCallback(
|
||||
(e: React.DragEvent, idx: number) => {
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = "move"
|
||||
if (dragIdx === null || dragIdx === idx) return
|
||||
setDragOverIdx(idx)
|
||||
},
|
||||
[dragIdx],
|
||||
)
|
||||
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent, toIdx: number) => {
|
||||
e.preventDefault()
|
||||
if (dragIdx !== null && dragIdx !== toIdx && onReorder) {
|
||||
onReorder(dragIdx, toIdx)
|
||||
}
|
||||
setDragIdx(null)
|
||||
setDragOverIdx(null)
|
||||
},
|
||||
[dragIdx, onReorder],
|
||||
)
|
||||
|
||||
const handleDragEnd = useCallback(() => {
|
||||
setDragIdx(null)
|
||||
setDragOverIdx(null)
|
||||
}, [])
|
||||
|
||||
/* ── 悬浮预览 ── */
|
||||
const handleMouseEnter = useCallback((asset: MediaAsset, e: React.MouseEvent) => {
|
||||
if (previewTimer.current) clearTimeout(previewTimer.current)
|
||||
previewTimer.current = setTimeout(() => {
|
||||
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect()
|
||||
setPreviewAsset(asset)
|
||||
setPreviewPos({
|
||||
x: rect.right + 12,
|
||||
y: Math.max(8, rect.top - 20),
|
||||
})
|
||||
}, 400)
|
||||
}, [])
|
||||
|
||||
const handleMouseLeave = useCallback(() => {
|
||||
if (previewTimer.current) {
|
||||
clearTimeout(previewTimer.current)
|
||||
previewTimer.current = null
|
||||
}
|
||||
setPreviewAsset(null)
|
||||
}, [])
|
||||
|
||||
// 清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (previewTimer.current) clearTimeout(previewTimer.current)
|
||||
}
|
||||
}, [])
|
||||
// 悬浮预览
|
||||
const { previewAsset, previewPos, handleMouseEnter, handleMouseLeave } = useAssetPreview()
|
||||
|
||||
/* ── 点击卡片 ── */
|
||||
const handleCardClick = useCallback(
|
||||
(asset: MediaAsset, idx: number, e: React.MouseEvent) => {
|
||||
// 如果点击的是 checkbox 区域,不触发卡片点击
|
||||
const target = e.target as HTMLElement
|
||||
if (target.closest("[data-checkbox]")) return
|
||||
toggleSelect(asset, idx, e.shiftKey)
|
||||
@@ -250,253 +76,82 @@ const AssetSelector: React.FC<AssetSelectorProps> = ({
|
||||
[toggleSelect],
|
||||
)
|
||||
|
||||
/* ──────────── 渲染 ──────────── */
|
||||
|
||||
const hasSelection = selectedIds.length > 0
|
||||
|
||||
return (
|
||||
<div className="as-container">
|
||||
{/* ═══ 工具栏 ═══ */}
|
||||
<div className="as-toolbar">
|
||||
<div className="as-toolbar-left">
|
||||
<div className="as-search">
|
||||
<Input
|
||||
placeholder="搜索素材..."
|
||||
value={searchText}
|
||||
onChange={(e) => setSearchText(e.target.value)}
|
||||
prefix="🔍"
|
||||
/>
|
||||
</div>
|
||||
<Select
|
||||
value={filterType}
|
||||
onChange={(v: string) => setFilterType(v)}
|
||||
options={TYPE_OPTIONS}
|
||||
/>
|
||||
{showQualityFilter && (
|
||||
<Select
|
||||
value={filterQuality}
|
||||
onChange={(v: string) => setFilterQuality(v)}
|
||||
options={QUALITY_OPTIONS}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<div className="as-toolbar-right">
|
||||
<button
|
||||
className={`as-view-btn${viewMode === "grid" ? " active" : ""}`}
|
||||
onClick={() => setViewMode("grid")}
|
||||
title="网格视图"
|
||||
>
|
||||
⊞
|
||||
</button>
|
||||
<button
|
||||
className={`as-view-btn${viewMode === "list" ? " active" : ""}`}
|
||||
onClick={() => setViewMode("list")}
|
||||
title="列表视图"
|
||||
>
|
||||
☰
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/* 工具栏 */}
|
||||
<SelectorToolbar
|
||||
searchText={searchText}
|
||||
filterType={filterType}
|
||||
filterQuality={filterQuality}
|
||||
viewMode={viewMode}
|
||||
showQualityFilter={showQualityFilter}
|
||||
onSearchChange={setSearchText}
|
||||
onTypeChange={setFilterType}
|
||||
onQualityChange={setFilterQuality}
|
||||
onViewModeChange={setViewMode}
|
||||
/>
|
||||
|
||||
{/* ═══ 批量操作栏 ═══ */}
|
||||
{/* 批量操作栏 */}
|
||||
{showBatchSelect && hasSelection && (
|
||||
<div className="as-batch-bar">
|
||||
<span className="as-batch-bar-count">已选 {selectedIds.length} 项</span>
|
||||
<div className="as-batch-bar-actions">
|
||||
<Button buttonType="ghost" buttonSize="sm" onClick={clearSelection}>
|
||||
取消选择
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<SelectorBatchBar selectedCount={selectedIds.length} onClear={clearSelection} />
|
||||
)}
|
||||
|
||||
{/* ═══ 素材列表 ═══ */}
|
||||
{/* 素材列表 */}
|
||||
<div className="as-body">
|
||||
{filteredAssets.length === 0 ? (
|
||||
<div className="as-empty">
|
||||
<div className="as-empty-icon">📂</div>
|
||||
<p>暂无素材</p>
|
||||
</div>
|
||||
<EmptyState />
|
||||
) : viewMode === "grid" ? (
|
||||
/* ── 网格视图 ── */
|
||||
/* 网格视图 */
|
||||
<div className={`as-grid${compact ? " compact" : ""}`}>
|
||||
{filteredAssets.map((asset, idx) => {
|
||||
const isSelected = selectedSet.has(asset.id)
|
||||
const isDragging = dragIdx === idx
|
||||
const isDragOver = dragOverIdx === idx
|
||||
const qualityLevel = getQualityLevel(asset.quality_score)
|
||||
|
||||
return (
|
||||
<div
|
||||
key={asset.id}
|
||||
className={[
|
||||
"as-card",
|
||||
isSelected ? "selected" : "",
|
||||
isDragging ? "dragging" : "",
|
||||
isDragOver ? "drag-over" : "",
|
||||
showBatchSelect ? "has-checkbox" : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
draggable
|
||||
onDragStart={(e) => handleDragStart(e, idx)}
|
||||
onDragOver={(e) => handleDragOver(e, idx)}
|
||||
onDrop={(e) => handleDrop(e, idx)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onClick={(e) => handleCardClick(asset, idx, e)}
|
||||
onMouseEnter={(e) => handleMouseEnter(asset, e)}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
{/* 缩略图 */}
|
||||
<div className="as-card-thumb">
|
||||
{asset.thumbnail_url ? (
|
||||
<img src={asset.thumbnail_url} alt={asset.name} loading="lazy" />
|
||||
) : (
|
||||
<span className="as-card-thumb-icon">{MATERIAL_TYPE_ICONS[asset.type]}</span>
|
||||
)}
|
||||
|
||||
{/* Checkbox */}
|
||||
{showBatchSelect && (
|
||||
<span
|
||||
data-checkbox
|
||||
className={`as-card-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
toggleSelect(asset, idx, e.shiftKey)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 类型角标 */}
|
||||
<span className="as-card-type-badge">{MATERIAL_TYPE_LABELS[asset.type]}</span>
|
||||
|
||||
{/* 时长角标 */}
|
||||
{asset.duration != null && (
|
||||
<span className="as-card-duration">{formatDuration(asset.duration)}</span>
|
||||
)}
|
||||
|
||||
{/* 质量分角标 */}
|
||||
{asset.quality_score != null && (
|
||||
<span
|
||||
className={`as-card-quality ${qualityLevel}`}
|
||||
title={`质量分: ${asset.quality_score}`}
|
||||
>
|
||||
{asset.quality_score}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 信息 */}
|
||||
<div className="as-card-info">
|
||||
<p className="as-card-name" title={asset.name}>
|
||||
{asset.name}
|
||||
</p>
|
||||
<div className="as-card-meta">{formatSize(asset.size)}</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{filteredAssets.map((asset, idx) => (
|
||||
<AssetCard
|
||||
key={asset.id}
|
||||
asset={asset}
|
||||
isSelected={selectedSet.has(asset.id)}
|
||||
isDragging={dragIdx === idx}
|
||||
isDragOver={dragOverIdx === idx}
|
||||
showCheckbox={showBatchSelect}
|
||||
compact={compact}
|
||||
onToggleSelect={(a, shiftKey) => toggleSelect(a, idx, shiftKey)}
|
||||
onCardClick={(a, e) => handleCardClick(a, idx, e)}
|
||||
onDragStart={(e) => handleDragStart(e, idx)}
|
||||
onDragOver={(e) => handleDragOver(e, idx)}
|
||||
onDrop={(e) => handleDrop(e, idx)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onMouseEnter={(e) => handleMouseEnter(asset, e)}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
/* ── 列表视图 ── */
|
||||
/* 列表视图 */
|
||||
<div className="as-list">
|
||||
{filteredAssets.map((asset, idx) => {
|
||||
const isSelected = selectedSet.has(asset.id)
|
||||
const isDragging = dragIdx === idx
|
||||
const isDragOver = dragOverIdx === idx
|
||||
|
||||
return (
|
||||
<div
|
||||
key={asset.id}
|
||||
className={[
|
||||
"as-list-item",
|
||||
isSelected ? "selected" : "",
|
||||
isDragging ? "dragging" : "",
|
||||
isDragOver ? "drag-over" : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
draggable
|
||||
onDragStart={(e) => handleDragStart(e, idx)}
|
||||
onDragOver={(e) => handleDragOver(e, idx)}
|
||||
onDrop={(e) => handleDrop(e, idx)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onClick={(e) => handleCardClick(asset, idx, e)}
|
||||
onMouseEnter={(e) => handleMouseEnter(asset, e)}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
{/* 拖拽手柄 */}
|
||||
<span className="as-list-item-drag" title="拖拽排序">
|
||||
⠿
|
||||
</span>
|
||||
|
||||
{/* Checkbox */}
|
||||
{showBatchSelect && (
|
||||
<span
|
||||
data-checkbox
|
||||
className={`as-list-item-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
toggleSelect(asset, idx, e.shiftKey)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 图标 */}
|
||||
<span className="as-list-item-icon">{MATERIAL_TYPE_ICONS[asset.type]}</span>
|
||||
|
||||
{/* 信息 */}
|
||||
<div className="as-list-item-info">
|
||||
<div className="as-list-item-name">{asset.name}</div>
|
||||
<div className="as-list-item-meta">
|
||||
{MATERIAL_TYPE_LABELS[asset.type]}
|
||||
{asset.duration != null && ` · ${formatDuration(asset.duration)}`}
|
||||
{asset.size != null && ` · ${formatSize(asset.size)}`}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 质量分 */}
|
||||
{asset.quality_score != null && (
|
||||
<span
|
||||
className="as-list-item-quality"
|
||||
style={{ color: getQualityColor(asset.quality_score) }}
|
||||
>
|
||||
{asset.quality_score}分
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{filteredAssets.map((asset, idx) => (
|
||||
<AssetListItem
|
||||
key={asset.id}
|
||||
asset={asset}
|
||||
isSelected={selectedSet.has(asset.id)}
|
||||
isDragging={dragIdx === idx}
|
||||
isDragOver={dragOverIdx === idx}
|
||||
showCheckbox={showBatchSelect}
|
||||
onToggleSelect={(a, shiftKey) => toggleSelect(a, idx, shiftKey)}
|
||||
onCardClick={(a, e) => handleCardClick(a, idx, e)}
|
||||
onDragStart={(e) => handleDragStart(e, idx)}
|
||||
onDragOver={(e) => handleDragOver(e, idx)}
|
||||
onDrop={(e) => handleDrop(e, idx)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onMouseEnter={(e) => handleMouseEnter(asset, e)}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* ═══ 悬浮预览 ═══ */}
|
||||
{previewAsset && (
|
||||
<div className="as-preview-overlay" style={{ left: previewPos.x, top: previewPos.y }}>
|
||||
<div className="as-preview-overlay-thumb">
|
||||
{previewAsset.thumbnail_url ? (
|
||||
<img src={previewAsset.thumbnail_url} alt={previewAsset.name} />
|
||||
) : (
|
||||
<span className="as-preview-overlay-thumb-icon">
|
||||
{MATERIAL_TYPE_ICONS[previewAsset.type]}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="as-preview-overlay-name">{previewAsset.name}</p>
|
||||
<div className="as-preview-overlay-meta">
|
||||
<span>类型: {MATERIAL_TYPE_LABELS[previewAsset.type]}</span>
|
||||
{previewAsset.duration != null && (
|
||||
<span>时长: {formatDuration(previewAsset.duration)}</span>
|
||||
)}
|
||||
{previewAsset.size != null && <span>大小: {formatSize(previewAsset.size)}</span>}
|
||||
{previewAsset.quality_score != null && (
|
||||
<span>质量分: {previewAsset.quality_score}</span>
|
||||
)}
|
||||
{previewAsset.tags.length > 0 && <span>标签: {previewAsset.tags.join(", ")}</span>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* 悬浮预览 */}
|
||||
{previewAsset && <PreviewOverlay asset={previewAsset} position={previewPos} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import React from "react"
|
||||
|
||||
const EmptyState: React.FC = () => {
|
||||
return (
|
||||
<div className="as-empty">
|
||||
<div className="as-empty-icon">📂</div>
|
||||
<p>暂无素材</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default EmptyState
|
||||
@@ -0,0 +1,33 @@
|
||||
import React from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
import { MATERIAL_TYPE_LABELS, MATERIAL_TYPE_ICONS } from "@/api/template-editor"
|
||||
import { formatSize, formatDuration } from "./utils"
|
||||
|
||||
interface PreviewOverlayProps {
|
||||
asset: MediaAsset
|
||||
position: { x: number; y: number }
|
||||
}
|
||||
|
||||
const PreviewOverlay: React.FC<PreviewOverlayProps> = ({ asset, position }) => {
|
||||
return (
|
||||
<div className="as-preview-overlay" style={{ left: position.x, top: position.y }}>
|
||||
<div className="as-preview-overlay-thumb">
|
||||
{asset.thumbnail_url ? (
|
||||
<img src={asset.thumbnail_url} alt={asset.name} />
|
||||
) : (
|
||||
<span className="as-preview-overlay-thumb-icon">{MATERIAL_TYPE_ICONS[asset.type]}</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="as-preview-overlay-name">{asset.name}</p>
|
||||
<div className="as-preview-overlay-meta">
|
||||
<span>类型: {MATERIAL_TYPE_LABELS[asset.type]}</span>
|
||||
{asset.duration != null && <span>时长: {formatDuration(asset.duration)}</span>}
|
||||
{asset.size != null && <span>大小: {formatSize(asset.size)}</span>}
|
||||
{asset.quality_score != null && <span>质量分: {asset.quality_score}</span>}
|
||||
{asset.tags.length > 0 && <span>标签: {asset.tags.join(", ")}</span>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PreviewOverlay
|
||||
@@ -0,0 +1,22 @@
|
||||
import React from "react"
|
||||
import { Button } from "@/components/ui"
|
||||
|
||||
interface SelectorBatchBarProps {
|
||||
selectedCount: number
|
||||
onClear: () => void
|
||||
}
|
||||
|
||||
const SelectorBatchBar: React.FC<SelectorBatchBarProps> = ({ selectedCount, onClear }) => {
|
||||
return (
|
||||
<div className="as-batch-bar">
|
||||
<span className="as-batch-bar-count">已选 {selectedCount} 项</span>
|
||||
<div className="as-batch-bar-actions">
|
||||
<Button buttonType="ghost" buttonSize="sm" onClick={onClear}>
|
||||
取消选择
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SelectorBatchBar
|
||||
@@ -0,0 +1,74 @@
|
||||
import React from "react"
|
||||
import { Input, Select } from "@/components/ui"
|
||||
import { TYPE_OPTIONS } from "./constants"
|
||||
import { QUALITY_OPTIONS } from "@/api/template-editor"
|
||||
import type { ViewMode } from "./types"
|
||||
|
||||
interface SelectorToolbarProps {
|
||||
searchText: string
|
||||
filterType: string
|
||||
filterQuality: string
|
||||
viewMode: ViewMode
|
||||
showQualityFilter: boolean
|
||||
onSearchChange: (value: string) => void
|
||||
onTypeChange: (value: string) => void
|
||||
onQualityChange: (value: string) => void
|
||||
onViewModeChange: (mode: ViewMode) => void
|
||||
}
|
||||
|
||||
const SelectorToolbar: React.FC<SelectorToolbarProps> = ({
|
||||
searchText,
|
||||
filterType,
|
||||
filterQuality,
|
||||
viewMode,
|
||||
showQualityFilter,
|
||||
onSearchChange,
|
||||
onTypeChange,
|
||||
onQualityChange,
|
||||
onViewModeChange,
|
||||
}) => {
|
||||
return (
|
||||
<div className="as-toolbar">
|
||||
<div className="as-toolbar-left">
|
||||
<div className="as-search">
|
||||
<Input
|
||||
placeholder="搜索素材..."
|
||||
value={searchText}
|
||||
onChange={(e) => onSearchChange(e.target.value)}
|
||||
prefix="🔍"
|
||||
/>
|
||||
</div>
|
||||
<Select
|
||||
value={filterType}
|
||||
onChange={(v: string) => onTypeChange(v)}
|
||||
options={TYPE_OPTIONS}
|
||||
/>
|
||||
{showQualityFilter && (
|
||||
<Select
|
||||
value={filterQuality}
|
||||
onChange={(v: string) => onQualityChange(v)}
|
||||
options={QUALITY_OPTIONS}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<div className="as-toolbar-right">
|
||||
<button
|
||||
className={`as-view-btn${viewMode === "grid" ? " active" : ""}`}
|
||||
onClick={() => onViewModeChange("grid")}
|
||||
title="网格视图"
|
||||
>
|
||||
⊞
|
||||
</button>
|
||||
<button
|
||||
className={`as-view-btn${viewMode === "list" ? " active" : ""}`}
|
||||
onClick={() => onViewModeChange("list")}
|
||||
title="列表视图"
|
||||
>
|
||||
☰
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SelectorToolbar
|
||||
@@ -0,0 +1,7 @@
|
||||
/** 类型筛选选项 */
|
||||
export const TYPE_OPTIONS = [
|
||||
{ value: "", label: "全部类型" },
|
||||
{ value: "video", label: "🎬 视频" },
|
||||
{ value: "image", label: "🖼️ 图片" },
|
||||
{ value: "audio", label: "🎵 音频" },
|
||||
]
|
||||
@@ -0,0 +1,63 @@
|
||||
import { useState, useMemo } from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
import { QUALITY_OPTIONS } from "@/api/template-editor"
|
||||
import type { ViewMode } from "../types"
|
||||
|
||||
interface UseAssetFilterReturn {
|
||||
searchText: string
|
||||
filterType: string
|
||||
filterQuality: string
|
||||
viewMode: ViewMode
|
||||
filteredAssets: MediaAsset[]
|
||||
setSearchText: (value: string) => void
|
||||
setFilterType: (value: string) => void
|
||||
setFilterQuality: (value: string) => void
|
||||
setViewMode: (mode: ViewMode) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 素材筛选 Hook —— 搜索 + 类型 + 质量分 + 视图切换
|
||||
*/
|
||||
const useAssetFilter = (assets: MediaAsset[]): UseAssetFilterReturn => {
|
||||
const [searchText, setSearchText] = useState("")
|
||||
const [filterType, setFilterType] = useState("")
|
||||
const [filterQuality, setFilterQuality] = useState("")
|
||||
const [viewMode, setViewMode] = useState<ViewMode>("grid")
|
||||
|
||||
const filteredAssets = useMemo(() => {
|
||||
let list = assets
|
||||
if (searchText) {
|
||||
const q = searchText.toLowerCase()
|
||||
list = list.filter(
|
||||
(a) => a.name.toLowerCase().includes(q) || a.tags.some((t) => t.toLowerCase().includes(q)),
|
||||
)
|
||||
}
|
||||
if (filterType) {
|
||||
list = list.filter((a) => a.type === filterType)
|
||||
}
|
||||
if (filterQuality) {
|
||||
const opt = QUALITY_OPTIONS.find((o) => o.value === filterQuality)
|
||||
if (opt?.min != null && opt?.max != null) {
|
||||
list = list.filter(
|
||||
(a) =>
|
||||
a.quality_score != null && a.quality_score >= opt.min! && a.quality_score <= opt.max!,
|
||||
)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}, [assets, searchText, filterType, filterQuality])
|
||||
|
||||
return {
|
||||
searchText,
|
||||
filterType,
|
||||
filterQuality,
|
||||
viewMode,
|
||||
filteredAssets,
|
||||
setSearchText,
|
||||
setFilterType,
|
||||
setFilterQuality,
|
||||
setViewMode,
|
||||
}
|
||||
}
|
||||
|
||||
export default useAssetFilter
|
||||
@@ -0,0 +1,54 @@
|
||||
import { useState, useRef, useCallback, useEffect } from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
|
||||
interface UseAssetPreviewReturn {
|
||||
previewAsset: MediaAsset | null
|
||||
previewPos: { x: number; y: number }
|
||||
handleMouseEnter: (asset: MediaAsset, e: React.MouseEvent) => void
|
||||
handleMouseLeave: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 悬浮预览 Hook —— 延迟 400ms 显示预览浮层
|
||||
*/
|
||||
const useAssetPreview = (): UseAssetPreviewReturn => {
|
||||
const [previewAsset, setPreviewAsset] = useState<MediaAsset | null>(null)
|
||||
const [previewPos, setPreviewPos] = useState({ x: 0, y: 0 })
|
||||
const previewTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
|
||||
const handleMouseEnter = useCallback((asset: MediaAsset, e: React.MouseEvent) => {
|
||||
if (previewTimer.current) clearTimeout(previewTimer.current)
|
||||
previewTimer.current = setTimeout(() => {
|
||||
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect()
|
||||
setPreviewAsset(asset)
|
||||
setPreviewPos({
|
||||
x: rect.right + 12,
|
||||
y: Math.max(8, rect.top - 20),
|
||||
})
|
||||
}, 400)
|
||||
}, [])
|
||||
|
||||
const handleMouseLeave = useCallback(() => {
|
||||
if (previewTimer.current) {
|
||||
clearTimeout(previewTimer.current)
|
||||
previewTimer.current = null
|
||||
}
|
||||
setPreviewAsset(null)
|
||||
}, [])
|
||||
|
||||
// 清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (previewTimer.current) clearTimeout(previewTimer.current)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return {
|
||||
previewAsset,
|
||||
previewPos,
|
||||
handleMouseEnter,
|
||||
handleMouseLeave,
|
||||
}
|
||||
}
|
||||
|
||||
export default useAssetPreview
|
||||
@@ -0,0 +1,65 @@
|
||||
import { useCallback, useRef, useMemo } from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
|
||||
interface UseAssetSelectionOptions {
|
||||
filteredAssets: MediaAsset[]
|
||||
selectedIds: string[]
|
||||
onSelectionChange?: (ids: string[]) => void
|
||||
}
|
||||
|
||||
interface UseAssetSelectionReturn {
|
||||
selectedSet: Set<string>
|
||||
toggleSelect: (asset: MediaAsset, idx: number, shiftKey: boolean) => void
|
||||
clearSelection: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 素材选择 Hook —— 单选/多选/Shift 连选
|
||||
*/
|
||||
const useAssetSelection = ({
|
||||
filteredAssets,
|
||||
selectedIds,
|
||||
onSelectionChange,
|
||||
}: UseAssetSelectionOptions): UseAssetSelectionReturn => {
|
||||
const lastClickedIdx = useRef<number | null>(null)
|
||||
|
||||
const selectedSet = useMemo(() => new Set(selectedIds), [selectedIds])
|
||||
|
||||
const toggleSelect = useCallback(
|
||||
(asset: MediaAsset, idx: number, shiftKey: boolean) => {
|
||||
if (!onSelectionChange) return
|
||||
|
||||
if (shiftKey && lastClickedIdx.current !== null) {
|
||||
// Shift 连选
|
||||
const start = Math.min(lastClickedIdx.current, idx)
|
||||
const end = Math.max(lastClickedIdx.current, idx)
|
||||
const rangeIds = filteredAssets.slice(start, end + 1).map((a) => a.id)
|
||||
const newSet = new Set(selectedIds)
|
||||
rangeIds.forEach((id) => newSet.add(id))
|
||||
onSelectionChange(Array.from(newSet))
|
||||
} else {
|
||||
const newSet = new Set(selectedIds)
|
||||
if (newSet.has(asset.id)) {
|
||||
newSet.delete(asset.id)
|
||||
} else {
|
||||
newSet.add(asset.id)
|
||||
}
|
||||
onSelectionChange(Array.from(newSet))
|
||||
}
|
||||
lastClickedIdx.current = idx
|
||||
},
|
||||
[onSelectionChange, selectedIds, filteredAssets],
|
||||
)
|
||||
|
||||
const clearSelection = useCallback(() => {
|
||||
onSelectionChange?.([])
|
||||
}, [onSelectionChange])
|
||||
|
||||
return {
|
||||
selectedSet,
|
||||
toggleSelect,
|
||||
clearSelection,
|
||||
}
|
||||
}
|
||||
|
||||
export default useAssetSelection
|
||||
@@ -0,0 +1,89 @@
|
||||
import { useState, useCallback } from "react"
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
|
||||
interface UseDragReorderOptions {
|
||||
filteredAssets: MediaAsset[]
|
||||
selectedIds: string[]
|
||||
onAssetDragStart?: (asset: MediaAsset) => void
|
||||
onReorder?: (fromIdx: number, toIdx: number) => void
|
||||
}
|
||||
|
||||
interface UseDragReorderReturn {
|
||||
dragIdx: number | null
|
||||
dragOverIdx: number | null
|
||||
handleDragStart: (e: React.DragEvent, idx: number) => void
|
||||
handleDragOver: (e: React.DragEvent, idx: number) => void
|
||||
handleDrop: (e: React.DragEvent, toIdx: number) => void
|
||||
handleDragEnd: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖拽排序 Hook —— HTML5 DnD,支持批量拖拽携带数据
|
||||
*/
|
||||
const useDragReorder = ({
|
||||
filteredAssets,
|
||||
selectedIds,
|
||||
onAssetDragStart,
|
||||
onReorder,
|
||||
}: UseDragReorderOptions): UseDragReorderReturn => {
|
||||
const [dragIdx, setDragIdx] = useState<number | null>(null)
|
||||
const [dragOverIdx, setDragOverIdx] = useState<number | null>(null)
|
||||
|
||||
const handleDragStart = useCallback(
|
||||
(e: React.DragEvent, idx: number) => {
|
||||
setDragIdx(idx)
|
||||
e.dataTransfer.effectAllowed = "move"
|
||||
e.dataTransfer.setData("text/plain", String(idx))
|
||||
// 设置素材数据,供 TimelinePanel 接收
|
||||
e.dataTransfer.setData("application/x-media-asset", JSON.stringify(filteredAssets[idx]))
|
||||
// 批量拖拽:如果有多个选中素材,一起携带
|
||||
if (selectedIds.length > 1 && selectedIds.includes(filteredAssets[idx].id)) {
|
||||
const batchAssets = filteredAssets.filter((a) => selectedIds.includes(a.id))
|
||||
e.dataTransfer.setData("application/x-media-assets", JSON.stringify(batchAssets))
|
||||
}
|
||||
// 通知父组件素材拖拽开始
|
||||
if (onAssetDragStart) {
|
||||
onAssetDragStart(filteredAssets[idx])
|
||||
}
|
||||
},
|
||||
[filteredAssets, selectedIds, onAssetDragStart],
|
||||
)
|
||||
|
||||
const handleDragOver = useCallback(
|
||||
(e: React.DragEvent, idx: number) => {
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = "move"
|
||||
if (dragIdx === null || dragIdx === idx) return
|
||||
setDragOverIdx(idx)
|
||||
},
|
||||
[dragIdx],
|
||||
)
|
||||
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent, toIdx: number) => {
|
||||
e.preventDefault()
|
||||
if (dragIdx !== null && dragIdx !== toIdx && onReorder) {
|
||||
onReorder(dragIdx, toIdx)
|
||||
}
|
||||
setDragIdx(null)
|
||||
setDragOverIdx(null)
|
||||
},
|
||||
[dragIdx, onReorder],
|
||||
)
|
||||
|
||||
const handleDragEnd = useCallback(() => {
|
||||
setDragIdx(null)
|
||||
setDragOverIdx(null)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
dragIdx,
|
||||
dragOverIdx,
|
||||
handleDragStart,
|
||||
handleDragOver,
|
||||
handleDrop,
|
||||
handleDragEnd,
|
||||
}
|
||||
}
|
||||
|
||||
export default useDragReorder
|
||||
@@ -1,2 +1,2 @@
|
||||
export { default as AssetSelector } from "./AssetSelector"
|
||||
export type { AssetSelectorProps } from "./AssetSelector"
|
||||
export type { AssetSelectorProps, ViewMode } from "./types"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { MediaAsset } from "@/api/template-editor"
|
||||
|
||||
export interface AssetSelectorProps {
|
||||
assets: MediaAsset[]
|
||||
selectedIds?: string[]
|
||||
onSelectionChange?: (ids: string[]) => void
|
||||
onAssetDragStart?: (asset: MediaAsset) => void
|
||||
onReorder?: (fromIdx: number, toIdx: number) => void
|
||||
showQualityFilter?: boolean
|
||||
showBatchSelect?: boolean
|
||||
compact?: boolean
|
||||
}
|
||||
|
||||
export type ViewMode = "grid" | "list"
|
||||
@@ -0,0 +1,33 @@
|
||||
/** 格式化文件大小 */
|
||||
export const formatSize = (bytes?: number): string => {
|
||||
if (!bytes) return ""
|
||||
if (bytes < 1024) return `${bytes}B`
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
|
||||
}
|
||||
|
||||
/** 格式化时长 */
|
||||
export const formatDuration = (seconds?: number): string => {
|
||||
if (!seconds) return ""
|
||||
const m = Math.floor(seconds / 60)
|
||||
const s = Math.floor(seconds % 60)
|
||||
return m > 0 ? `${m}:${s.toString().padStart(2, "0")}` : `${s}s`
|
||||
}
|
||||
|
||||
/** 获取质量分等级 */
|
||||
export const getQualityLevel = (score?: number): string => {
|
||||
if (score == null) return "none"
|
||||
if (score >= 90) return "excellent"
|
||||
if (score >= 70) return "good"
|
||||
if (score >= 50) return "fair"
|
||||
return "poor"
|
||||
}
|
||||
|
||||
/** 质量分颜色 */
|
||||
export const getQualityColor = (score?: number): string => {
|
||||
if (score == null) return "var(--text-secondary)"
|
||||
if (score >= 90) return "var(--success-color, #10b981)"
|
||||
if (score >= 70) return "var(--primary-color, #6366f1)"
|
||||
if (score >= 50) return "var(--warning-color, #f59e0b)"
|
||||
return "var(--error-color, #ef4444)"
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import { useState, useRef, useCallback, useEffect } from "react"
|
||||
import type { ModalPhase } from "../types/cloneModal"
|
||||
import { MIN_VOICE_NAME_LENGTH, MAX_VOICE_NAME_LENGTH } from "../constants/cloneModal"
|
||||
import useAudioRecorder from "./useAudioRecorder"
|
||||
|
||||
/**
|
||||
* 克隆弹窗表单状态 Hook
|
||||
* 管理表单字段、录音、文件选择、验证逻辑
|
||||
*/
|
||||
export function useCloneFormState({ open, onClose }: { open: boolean; onClose: () => void }) {
|
||||
const [phase, setPhase] = useState<ModalPhase>("input")
|
||||
const [voiceName, setVoiceName] = useState("")
|
||||
const [voiceDescription, setVoiceDescription] = useState("")
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null)
|
||||
const [dragActive, setDragActive] = useState(false)
|
||||
const [errorMessage, setErrorMessage] = useState("")
|
||||
|
||||
const { isRecording, recordTime, recordedBlob, toggleRecording, resetRecording } =
|
||||
useAudioRecorder()
|
||||
|
||||
/** 默认音色名称计数器 */
|
||||
const cloneCounterRef = useRef(1)
|
||||
|
||||
const getNextDefaultName = useCallback((): string => {
|
||||
const name = `我的声音 ${cloneCounterRef.current}`
|
||||
cloneCounterRef.current += 1
|
||||
return name
|
||||
}, [])
|
||||
|
||||
const hasAudio = selectedFile !== null || recordedBlob !== null
|
||||
|
||||
const canSubmit =
|
||||
voiceName.trim().length >= MIN_VOICE_NAME_LENGTH &&
|
||||
voiceName.trim().length <= MAX_VOICE_NAME_LENGTH &&
|
||||
hasAudio
|
||||
|
||||
const isProcessing = phase === "uploading" || phase === "cloning"
|
||||
|
||||
/** 重置弹窗状态 */
|
||||
const resetState = useCallback(() => {
|
||||
setPhase("input")
|
||||
setVoiceName(getNextDefaultName())
|
||||
setVoiceDescription("")
|
||||
setSelectedFile(null)
|
||||
setDragActive(false)
|
||||
setErrorMessage("")
|
||||
resetRecording()
|
||||
}, [getNextDefaultName, resetRecording])
|
||||
|
||||
/** 关闭弹窗 */
|
||||
const handleClose = useCallback(() => {
|
||||
resetState()
|
||||
onClose()
|
||||
}, [resetState, onClose])
|
||||
|
||||
/** 弹窗打开时重置状态 */
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
resetState()
|
||||
}
|
||||
}, [open, resetState])
|
||||
|
||||
/** 选择文件(来自上传或拖拽) */
|
||||
const handleFileSelect = useCallback(
|
||||
(file: File | null, error: string) => {
|
||||
if (error) {
|
||||
setErrorMessage(error)
|
||||
setSelectedFile(null)
|
||||
} else {
|
||||
setErrorMessage("")
|
||||
setSelectedFile(file)
|
||||
// 清除录音
|
||||
resetRecording()
|
||||
}
|
||||
},
|
||||
[resetRecording],
|
||||
)
|
||||
|
||||
/** 录音切换 */
|
||||
const handleRecordToggle = useCallback(() => {
|
||||
setErrorMessage("")
|
||||
if (isRecording) {
|
||||
toggleRecording()
|
||||
} else {
|
||||
// 开始录制前清除已选文件
|
||||
setSelectedFile(null)
|
||||
toggleRecording()
|
||||
}
|
||||
}, [isRecording, toggleRecording])
|
||||
|
||||
/** 表单验证 */
|
||||
const validateForm = useCallback((): string | null => {
|
||||
const name = voiceName.trim()
|
||||
if (!name) {
|
||||
return "请输入音色名称"
|
||||
}
|
||||
if (name.length < MIN_VOICE_NAME_LENGTH || name.length > MAX_VOICE_NAME_LENGTH) {
|
||||
return `音色名称需在 ${MIN_VOICE_NAME_LENGTH}-${MAX_VOICE_NAME_LENGTH} 个字符之间`
|
||||
}
|
||||
if (!hasAudio) {
|
||||
return "请上传音频文件或录制一段声音"
|
||||
}
|
||||
return null
|
||||
}, [voiceName, hasAudio])
|
||||
|
||||
return {
|
||||
// 状态
|
||||
phase,
|
||||
setPhase,
|
||||
voiceName,
|
||||
setVoiceName,
|
||||
voiceDescription,
|
||||
setVoiceDescription,
|
||||
selectedFile,
|
||||
dragActive,
|
||||
setDragActive,
|
||||
errorMessage,
|
||||
setErrorMessage,
|
||||
// 录音
|
||||
isRecording,
|
||||
recordTime,
|
||||
recordedBlob,
|
||||
// 计算属性
|
||||
hasAudio,
|
||||
canSubmit,
|
||||
isProcessing,
|
||||
// handlers
|
||||
handleFileSelect,
|
||||
handleRecordToggle,
|
||||
handleClose,
|
||||
validateForm,
|
||||
resetState,
|
||||
}
|
||||
}
|
||||
Regular → Executable
+33
-202
@@ -1,213 +1,44 @@
|
||||
import { useState, useRef, useCallback, useEffect } from "react"
|
||||
import { createVoiceClone, toVoiceClone } from "@/api/voice-clone"
|
||||
import { uploadAsset } from "@/api/assets"
|
||||
import type { ModalPhase, CloneModalProps } from "../types/cloneModal"
|
||||
import { MIN_VOICE_NAME_LENGTH, MAX_VOICE_NAME_LENGTH } from "../constants/cloneModal"
|
||||
import useAudioRecorder from "./useAudioRecorder"
|
||||
|
||||
interface UseCloneModalReturn {
|
||||
phase: ModalPhase
|
||||
voiceName: string
|
||||
voiceDescription: string
|
||||
selectedFile: File | null
|
||||
dragActive: boolean
|
||||
errorMessage: string
|
||||
isRecording: boolean
|
||||
recordTime: number
|
||||
recordedBlob: Blob | null
|
||||
canSubmit: boolean
|
||||
isProcessing: boolean
|
||||
setVoiceName: (value: string) => void
|
||||
setVoiceDescription: (value: string) => void
|
||||
setDragActive: (active: boolean) => void
|
||||
handleFileSelect: (file: File | null, error: string) => void
|
||||
handleRecordToggle: () => void
|
||||
handleClose: () => void
|
||||
handleSubmit: () => void
|
||||
}
|
||||
import type { CloneModalProps } from "../types/cloneModal"
|
||||
import { useCloneFormState } from "./useCloneFormState"
|
||||
import { useCloneSubmit } from "./useCloneSubmit"
|
||||
|
||||
/**
|
||||
* 音色克隆弹窗主业务 Hook
|
||||
* 组合表单状态 + 提交流程两个子 Hook
|
||||
*/
|
||||
const useCloneModal = ({ open, onClose, onSuccess }: CloneModalProps): UseCloneModalReturn => {
|
||||
const [phase, setPhase] = useState<ModalPhase>("input")
|
||||
const [voiceName, setVoiceName] = useState("")
|
||||
const [voiceDescription, setVoiceDescription] = useState("")
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null)
|
||||
const [dragActive, setDragActive] = useState(false)
|
||||
const [errorMessage, setErrorMessage] = useState("")
|
||||
const useCloneModal = ({ open, onClose, onSuccess }: CloneModalProps) => {
|
||||
const formState = useCloneFormState({ open, onClose })
|
||||
|
||||
const { isRecording, recordTime, recordedBlob, toggleRecording, resetRecording } =
|
||||
useAudioRecorder()
|
||||
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
/** 默认音色名称计数器 */
|
||||
const cloneCounterRef = useRef(1)
|
||||
|
||||
const getNextDefaultName = useCallback((): string => {
|
||||
const name = `我的声音 ${cloneCounterRef.current}`
|
||||
cloneCounterRef.current += 1
|
||||
return name
|
||||
}, [])
|
||||
|
||||
const hasAudio = selectedFile !== null || recordedBlob !== null
|
||||
|
||||
const canSubmit =
|
||||
voiceName.trim().length >= MIN_VOICE_NAME_LENGTH &&
|
||||
voiceName.trim().length <= MAX_VOICE_NAME_LENGTH &&
|
||||
hasAudio
|
||||
|
||||
const isProcessing = phase === "uploading" || phase === "cloning"
|
||||
|
||||
/** 重置弹窗状态 */
|
||||
const resetState = useCallback(() => {
|
||||
setPhase("input")
|
||||
setVoiceName(getNextDefaultName())
|
||||
setVoiceDescription("")
|
||||
setSelectedFile(null)
|
||||
setDragActive(false)
|
||||
setErrorMessage("")
|
||||
resetRecording()
|
||||
}, [getNextDefaultName, resetRecording])
|
||||
|
||||
/** 关闭弹窗 */
|
||||
const handleClose = useCallback(() => {
|
||||
resetState()
|
||||
onClose()
|
||||
}, [resetState, onClose])
|
||||
|
||||
/** 弹窗打开时重置状态 */
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
resetState()
|
||||
}
|
||||
}, [open, resetState])
|
||||
|
||||
/** 组件卸载时清理定时器 */
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timerRef.current) clearTimeout(timerRef.current)
|
||||
}
|
||||
}, [])
|
||||
|
||||
/** 选择文件(来自上传或拖拽) */
|
||||
const handleFileSelect = useCallback(
|
||||
(file: File | null, error: string) => {
|
||||
if (error) {
|
||||
setErrorMessage(error)
|
||||
setSelectedFile(null)
|
||||
} else {
|
||||
setErrorMessage("")
|
||||
setSelectedFile(file)
|
||||
// 清除录音
|
||||
resetRecording()
|
||||
}
|
||||
},
|
||||
[resetRecording],
|
||||
)
|
||||
|
||||
/** 录音切换 */
|
||||
const handleRecordToggle = useCallback(() => {
|
||||
setErrorMessage("")
|
||||
if (isRecording) {
|
||||
toggleRecording()
|
||||
} else {
|
||||
// 开始录制前清除已选文件
|
||||
setSelectedFile(null)
|
||||
toggleRecording()
|
||||
}
|
||||
}, [isRecording, toggleRecording])
|
||||
|
||||
/** 表单验证 */
|
||||
const validateForm = useCallback((): string | null => {
|
||||
const name = voiceName.trim()
|
||||
if (!name) {
|
||||
return "请输入音色名称"
|
||||
}
|
||||
if (name.length < MIN_VOICE_NAME_LENGTH || name.length > MAX_VOICE_NAME_LENGTH) {
|
||||
return `音色名称需在 ${MIN_VOICE_NAME_LENGTH}-${MAX_VOICE_NAME_LENGTH} 个字符之间`
|
||||
}
|
||||
if (!hasAudio) {
|
||||
return "请上传音频文件或录制一段声音"
|
||||
}
|
||||
return null
|
||||
}, [voiceName, hasAudio])
|
||||
|
||||
/** 提交克隆 */
|
||||
const handleSubmit = useCallback(async () => {
|
||||
const formError = validateForm()
|
||||
if (formError) {
|
||||
setErrorMessage(formError)
|
||||
return
|
||||
}
|
||||
|
||||
setErrorMessage("")
|
||||
|
||||
try {
|
||||
// 阶段 1:上传音频
|
||||
setPhase("uploading")
|
||||
|
||||
let fileToUpload: File
|
||||
if (selectedFile) {
|
||||
fileToUpload = selectedFile
|
||||
} else {
|
||||
fileToUpload = new File([recordedBlob!], `recorded-${Date.now()}.webm`, {
|
||||
type: "audio/webm",
|
||||
})
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append("file", fileToUpload)
|
||||
const uploadResult = await uploadAsset(formData)
|
||||
|
||||
// 阶段 2:克隆
|
||||
setPhase("cloning")
|
||||
const result = await createVoiceClone({
|
||||
name: voiceName.trim(),
|
||||
description: voiceDescription.trim() || undefined,
|
||||
audio_url: uploadResult.url,
|
||||
})
|
||||
|
||||
// 阶段 3:完成
|
||||
setPhase("done")
|
||||
|
||||
// 2秒后自动关闭
|
||||
timerRef.current = setTimeout(() => {
|
||||
onSuccess?.(toVoiceClone(result))
|
||||
handleClose()
|
||||
}, 2000)
|
||||
} catch (err) {
|
||||
setPhase("input")
|
||||
setErrorMessage(err instanceof Error ? err.message : "克隆失败,请重试")
|
||||
}
|
||||
}, [
|
||||
validateForm,
|
||||
selectedFile,
|
||||
recordedBlob,
|
||||
voiceName,
|
||||
voiceDescription,
|
||||
const { handleSubmit } = useCloneSubmit({
|
||||
voiceName: formState.voiceName,
|
||||
voiceDescription: formState.voiceDescription,
|
||||
selectedFile: formState.selectedFile,
|
||||
recordedBlob: formState.recordedBlob,
|
||||
setPhase: formState.setPhase,
|
||||
setErrorMessage: formState.setErrorMessage,
|
||||
validateForm: formState.validateForm,
|
||||
onSuccess,
|
||||
handleClose,
|
||||
])
|
||||
onClose: formState.handleClose,
|
||||
})
|
||||
|
||||
return {
|
||||
phase,
|
||||
voiceName,
|
||||
voiceDescription,
|
||||
selectedFile,
|
||||
dragActive,
|
||||
errorMessage,
|
||||
isRecording,
|
||||
recordTime,
|
||||
recordedBlob,
|
||||
canSubmit,
|
||||
isProcessing,
|
||||
setVoiceName,
|
||||
setVoiceDescription,
|
||||
setDragActive,
|
||||
handleFileSelect,
|
||||
handleRecordToggle,
|
||||
handleClose,
|
||||
phase: formState.phase,
|
||||
voiceName: formState.voiceName,
|
||||
voiceDescription: formState.voiceDescription,
|
||||
selectedFile: formState.selectedFile,
|
||||
dragActive: formState.dragActive,
|
||||
errorMessage: formState.errorMessage,
|
||||
isRecording: formState.isRecording,
|
||||
recordTime: formState.recordTime,
|
||||
recordedBlob: formState.recordedBlob,
|
||||
canSubmit: formState.canSubmit,
|
||||
isProcessing: formState.isProcessing,
|
||||
setVoiceName: formState.setVoiceName,
|
||||
setVoiceDescription: formState.setVoiceDescription,
|
||||
setDragActive: formState.setDragActive,
|
||||
handleFileSelect: formState.handleFileSelect,
|
||||
handleRecordToggle: formState.handleRecordToggle,
|
||||
handleClose: formState.handleClose,
|
||||
handleSubmit,
|
||||
}
|
||||
}
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
import { useRef, useCallback, useEffect } from "react"
|
||||
import { createVoiceClone, toVoiceClone } from "@/api/voice-clone"
|
||||
import { uploadAsset } from "@/api/assets"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
|
||||
interface UseCloneSubmitOptions {
|
||||
voiceName: string
|
||||
voiceDescription: string
|
||||
selectedFile: File | null
|
||||
recordedBlob: Blob | null
|
||||
setPhase: (phase: "input" | "uploading" | "cloning" | "done") => void
|
||||
setErrorMessage: (msg: string) => void
|
||||
validateForm: () => string | null
|
||||
onSuccess?: (clone: VoiceClone) => void
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆提交流程 Hook
|
||||
* 封装上传 + 克隆 + 完成的三阶段流程
|
||||
*/
|
||||
export function useCloneSubmit({
|
||||
voiceName,
|
||||
voiceDescription,
|
||||
selectedFile,
|
||||
recordedBlob,
|
||||
setPhase,
|
||||
setErrorMessage,
|
||||
validateForm,
|
||||
onSuccess,
|
||||
onClose,
|
||||
}: UseCloneSubmitOptions) {
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
|
||||
/** 组件卸载时清理定时器 */
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timerRef.current) clearTimeout(timerRef.current)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
const formError = validateForm()
|
||||
if (formError) {
|
||||
setErrorMessage(formError)
|
||||
return
|
||||
}
|
||||
|
||||
setErrorMessage("")
|
||||
|
||||
try {
|
||||
// 阶段 1:上传音频
|
||||
setPhase("uploading")
|
||||
|
||||
let fileToUpload: File
|
||||
if (selectedFile) {
|
||||
fileToUpload = selectedFile
|
||||
} else {
|
||||
fileToUpload = new File([recordedBlob!], `recorded-${Date.now()}.webm`, {
|
||||
type: "audio/webm",
|
||||
})
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append("file", fileToUpload)
|
||||
const uploadResult = await uploadAsset(formData)
|
||||
|
||||
// 阶段 2:克隆
|
||||
setPhase("cloning")
|
||||
const result = await createVoiceClone({
|
||||
name: voiceName.trim(),
|
||||
description: voiceDescription.trim() || undefined,
|
||||
audio_url: uploadResult.url,
|
||||
})
|
||||
|
||||
// 阶段 3:完成
|
||||
setPhase("done")
|
||||
|
||||
// 2秒后自动关闭
|
||||
timerRef.current = setTimeout(() => {
|
||||
onSuccess?.(toVoiceClone(result))
|
||||
onClose()
|
||||
}, 2000)
|
||||
} catch (err) {
|
||||
setPhase("input")
|
||||
setErrorMessage(err instanceof Error ? err.message : "克隆失败,请重试")
|
||||
}
|
||||
}, [
|
||||
validateForm,
|
||||
selectedFile,
|
||||
recordedBlob,
|
||||
voiceName,
|
||||
voiceDescription,
|
||||
setPhase,
|
||||
setErrorMessage,
|
||||
onSuccess,
|
||||
onClose,
|
||||
])
|
||||
|
||||
return { handleSubmit }
|
||||
}
|
||||
Regular → Executable
+36
-184
@@ -1,116 +1,35 @@
|
||||
/**
|
||||
* 查重上传页面 — V21 设计系统
|
||||
* 左右分栏:拖拽上传区 + 格式说明
|
||||
* 零 antd 依赖
|
||||
*/
|
||||
import React, { useState, useRef, useCallback } from "react"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { Button, Card, Tag } from "@/components/ui"
|
||||
import { uploadForDuplication } from "@/api/duplication"
|
||||
import React from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import "./duplication.css"
|
||||
import { Card } from "@/components/ui"
|
||||
import PageHead from "@/components/layout/PageHead"
|
||||
|
||||
/** 支持的视频格式 */
|
||||
const ACCEPT_FORMATS = ".mp4,.avi,.mov,.mkv,.wmv,.flv,.webm"
|
||||
const FORMAT_LIST = ["MP4", "AVI", "MOV", "MKV", "WMV", "FLV", "WebM"]
|
||||
/** 最大文件大小:2GB */
|
||||
const MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024
|
||||
|
||||
/** 简易 toast */
|
||||
interface ToastState {
|
||||
message: string
|
||||
type: "success" | "error" | "warning"
|
||||
}
|
||||
import UploadZone from "./duplication-upload/UploadZone"
|
||||
import UploadActions from "./duplication-upload/UploadActions"
|
||||
import UploadProgress from "./duplication-upload/UploadProgress"
|
||||
import UploadResultPanel from "./duplication-upload/UploadResultPanel"
|
||||
import InfoSidebar from "./duplication-upload/InfoSidebar"
|
||||
import { useDuplicationUpload } from "./duplication-upload/useDuplicationUpload"
|
||||
import { ACCEPT_FORMATS } from "./duplication-upload/constants"
|
||||
import "./duplication.css"
|
||||
|
||||
const DuplicationUpload: React.FC = () => {
|
||||
const navigate = useNavigate()
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const [dragging, setDragging] = useState(false)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const [uploadResult, setUploadResult] = useState<{
|
||||
id: string
|
||||
message: string
|
||||
} | null>(null)
|
||||
const [toast, setToast] = useState<ToastState | null>(null)
|
||||
|
||||
/** 显示 toast */
|
||||
const showToast = useCallback((message: string, type: "success" | "error" | "warning") => {
|
||||
setToast({ message, type })
|
||||
setTimeout(() => setToast(null), 3000)
|
||||
}, [])
|
||||
|
||||
// 上传查重 mutation
|
||||
const uploadMutation = useMutation({
|
||||
mutationFn: (file: File) => uploadForDuplication(file),
|
||||
onSuccess: (data) => {
|
||||
setUploading(false)
|
||||
setUploadResult({ id: data.id, message: data.message })
|
||||
showToast("查重任务已提交", "success")
|
||||
},
|
||||
onError: () => {
|
||||
setUploading(false)
|
||||
showToast("上传失败,请重试", "error")
|
||||
},
|
||||
})
|
||||
|
||||
/** 校验并上传文件 */
|
||||
const handleFile = useCallback(
|
||||
(file: File) => {
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
showToast("文件大小不能超过 2GB", "error")
|
||||
return
|
||||
}
|
||||
const ext = file.name.toLowerCase().split(".").pop()
|
||||
const allowedExts = ACCEPT_FORMATS.replace(/\./g, "").split(",")
|
||||
if (!allowedExts.includes(ext || "")) {
|
||||
showToast(`不支持的文件格式,支持:${FORMAT_LIST.join("、")}`, "error")
|
||||
return
|
||||
}
|
||||
setUploading(true)
|
||||
setUploadResult(null)
|
||||
uploadMutation.mutate(file)
|
||||
},
|
||||
[uploadMutation, showToast],
|
||||
)
|
||||
|
||||
/** 拖拽事件 */
|
||||
const handleDragOver = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
setDragging(true)
|
||||
}, [])
|
||||
|
||||
const handleDragLeave = useCallback(() => {
|
||||
setDragging(false)
|
||||
}, [])
|
||||
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
setDragging(false)
|
||||
const file = e.dataTransfer.files[0]
|
||||
if (file) handleFile(file)
|
||||
},
|
||||
[handleFile],
|
||||
)
|
||||
|
||||
/** 点击选择文件 */
|
||||
const handleSelectFile = () => {
|
||||
fileInputRef.current?.click()
|
||||
}
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) handleFile(file)
|
||||
// 重置 input 以便重复选择同一文件
|
||||
e.target.value = ""
|
||||
}
|
||||
|
||||
/** 重置状态 */
|
||||
const handleReset = () => {
|
||||
setUploadResult(null)
|
||||
setUploading(false)
|
||||
}
|
||||
const {
|
||||
fileInputRef,
|
||||
dragging,
|
||||
uploading,
|
||||
uploadResult,
|
||||
toast,
|
||||
handleDragOver,
|
||||
handleDragLeave,
|
||||
handleDrop,
|
||||
handleSelectFile,
|
||||
handleFileChange,
|
||||
handleReset,
|
||||
} = useDuplicationUpload()
|
||||
|
||||
return (
|
||||
<div className="dup-page">
|
||||
@@ -125,25 +44,14 @@ const DuplicationUpload: React.FC = () => {
|
||||
<div className="dup-upload-grid">
|
||||
{/* 左侧:上传区域 */}
|
||||
<Card>
|
||||
{/* 拖拽上传区 */}
|
||||
<div
|
||||
className={`dup-upload-zone ${dragging ? "dragging" : ""} ${uploading ? "disabled" : ""}`}
|
||||
<UploadZone
|
||||
dragging={dragging}
|
||||
uploading={uploading}
|
||||
onDragOver={handleDragOver}
|
||||
onDragLeave={handleDragLeave}
|
||||
onDrop={handleDrop}
|
||||
onClick={uploading ? undefined : handleSelectFile}
|
||||
>
|
||||
<div className="dup-upload-icon">{uploading ? "⏳" : "📁"}</div>
|
||||
<h3>{uploading ? "正在上传并查重..." : "点击或拖拽视频文件到此区域"}</h3>
|
||||
<p>支持 MP4、AVI、MOV、MKV 等格式,单个文件不超过 2GB</p>
|
||||
<div className="dup-upload-formats">
|
||||
{FORMAT_LIST.map((fmt) => (
|
||||
<Tag key={fmt} variant="info">
|
||||
{fmt}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
onClick={handleSelectFile}
|
||||
/>
|
||||
|
||||
{/* 隐藏的文件 input */}
|
||||
<input
|
||||
@@ -154,77 +62,21 @@ const DuplicationUpload: React.FC = () => {
|
||||
onChange={handleFileChange}
|
||||
/>
|
||||
|
||||
{/* 上传按钮 */}
|
||||
<div className="dup-upload-actions">
|
||||
<Button
|
||||
buttonType="primary"
|
||||
buttonSize="md"
|
||||
onClick={handleSelectFile}
|
||||
disabled={uploading}
|
||||
>
|
||||
📂 选择文件
|
||||
</Button>
|
||||
</div>
|
||||
<UploadActions uploading={uploading} onSelectFile={handleSelectFile} />
|
||||
|
||||
{/* 上传进度 */}
|
||||
{uploading && (
|
||||
<div className="dup-progress">
|
||||
<div className="dup-progress-circle">
|
||||
<span className="dup-progress-icon">⏳</span>
|
||||
<span className="dup-progress-text">查重中...</span>
|
||||
</div>
|
||||
<p>正在分析视频内容,请稍候...</p>
|
||||
</div>
|
||||
)}
|
||||
{uploading && <UploadProgress />}
|
||||
|
||||
{/* 上传结果 */}
|
||||
{uploadResult && !uploading && (
|
||||
<div className="dup-result">
|
||||
<div className="dup-result-icon">✅</div>
|
||||
<h3>查重任务已提交</h3>
|
||||
<p>{uploadResult.message}</p>
|
||||
<div className="dup-result-actions">
|
||||
<Button
|
||||
buttonType="primary"
|
||||
buttonSize="md"
|
||||
onClick={() => navigate("/app/duplication/results")}
|
||||
>
|
||||
查看结果
|
||||
</Button>
|
||||
<Button buttonType="secondary" buttonSize="md" onClick={handleReset}>
|
||||
继续上传
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<UploadResultPanel
|
||||
result={uploadResult}
|
||||
onViewResult={() => navigate("/app/duplication/results")}
|
||||
onReset={handleReset}
|
||||
/>
|
||||
)}
|
||||
</Card>
|
||||
|
||||
{/* 右侧:格式说明 + 提示 */}
|
||||
<div className="dup-info-card">
|
||||
<h3>📋 查重说明</h3>
|
||||
<ul className="dup-info-list">
|
||||
<li>系统会对比您上传的视频与视频库中的已有视频</li>
|
||||
<li>查重完成后,可查看重复片段的具体位置</li>
|
||||
<li>查重过程通常需要几分钟,取决于视频大小</li>
|
||||
<li>高相似度片段建议进行替换或裁剪</li>
|
||||
</ul>
|
||||
|
||||
<h3 style={{ marginTop: 24 }}>🎬 支持格式</h3>
|
||||
<div className="dup-format-tags">
|
||||
{FORMAT_LIST.map((fmt) => (
|
||||
<Tag key={fmt} variant="info">
|
||||
{fmt}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<h3 style={{ marginTop: 24 }}>💡 温馨提示</h3>
|
||||
<ul className="dup-info-list">
|
||||
<li>单个文件不超过 2GB</li>
|
||||
<li>视频时长建议不超过 60 分钟</li>
|
||||
<li>查重结果可在「查重记录」中随时查看</li>
|
||||
</ul>
|
||||
</div>
|
||||
<InfoSidebar />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
Regular → Executable
+7
-1
@@ -51,7 +51,13 @@ export const FILTER_OPTIONS: { key: RiskFilter; label: string }[] = [
|
||||
{ key: "high", label: "高风险" },
|
||||
]
|
||||
|
||||
/** Toast 类型 */
|
||||
/** 支持的视频格式 */
|
||||
export const ACCEPT_FORMATS = ".mp4,.avi,.mov,.mkv,.wmv,.flv,.webm"
|
||||
export const FORMAT_LIST = ["MP4", "AVI", "MOV", "MKV", "WMV", "FLV", "WebM"]
|
||||
/** 最大文件大小:2GB */
|
||||
export const MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024
|
||||
|
||||
/** 简易 toast */
|
||||
export interface ToastState {
|
||||
message: string
|
||||
type: "success" | "error" | "warning"
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import React from "react"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { FORMAT_LIST } from "./constants"
|
||||
|
||||
/** 右侧说明卡 */
|
||||
const InfoSidebar: React.FC = () => {
|
||||
return (
|
||||
<div className="dup-info-card">
|
||||
<h3>📋 查重说明</h3>
|
||||
<ul className="dup-info-list">
|
||||
<li>系统会对比您上传的视频与视频库中的已有视频</li>
|
||||
<li>查重完成后,可查看重复片段的具体位置</li>
|
||||
<li>查重过程通常需要几分钟,取决于视频大小</li>
|
||||
<li>高相似度片段建议进行替换或裁剪</li>
|
||||
</ul>
|
||||
|
||||
<h3 style={{ marginTop: 24 }}>🎬 支持格式</h3>
|
||||
<div className="dup-format-tags">
|
||||
{FORMAT_LIST.map((fmt) => (
|
||||
<Tag key={fmt} variant="info">
|
||||
{fmt}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<h3 style={{ marginTop: 24 }}>💡 温馨提示</h3>
|
||||
<ul className="dup-info-list">
|
||||
<li>单个文件不超过 2GB</li>
|
||||
<li>视频时长建议不超过 60 分钟</li>
|
||||
<li>查重结果可在「查重记录」中随时查看</li>
|
||||
</ul>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default InfoSidebar
|
||||
@@ -0,0 +1,20 @@
|
||||
import React from "react"
|
||||
import { Button } from "@/components/ui"
|
||||
|
||||
interface UploadActionsProps {
|
||||
uploading: boolean
|
||||
onSelectFile: () => void
|
||||
}
|
||||
|
||||
/** 上传按钮区 */
|
||||
const UploadActions: React.FC<UploadActionsProps> = ({ uploading, onSelectFile }) => {
|
||||
return (
|
||||
<div className="dup-upload-actions">
|
||||
<Button buttonType="primary" buttonSize="md" onClick={onSelectFile} disabled={uploading}>
|
||||
📂 选择文件
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UploadActions
|
||||
@@ -0,0 +1,16 @@
|
||||
import React from "react"
|
||||
|
||||
/** 上传进度展示 */
|
||||
const UploadProgress: React.FC = () => {
|
||||
return (
|
||||
<div className="dup-progress">
|
||||
<div className="dup-progress-circle">
|
||||
<span className="dup-progress-icon">⏳</span>
|
||||
<span className="dup-progress-text">查重中...</span>
|
||||
</div>
|
||||
<p>正在分析视频内容,请稍候...</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UploadProgress
|
||||
@@ -0,0 +1,30 @@
|
||||
import React from "react"
|
||||
import { Button } from "@/components/ui"
|
||||
import type { UploadResult } from "./constants"
|
||||
|
||||
interface UploadResultPanelProps {
|
||||
result: UploadResult
|
||||
onViewResult: () => void
|
||||
onReset: () => void
|
||||
}
|
||||
|
||||
/** 上传结果展示 */
|
||||
const UploadResultPanel: React.FC<UploadResultPanelProps> = ({ result, onViewResult, onReset }) => {
|
||||
return (
|
||||
<div className="dup-result">
|
||||
<div className="dup-result-icon">✅</div>
|
||||
<h3>查重任务已提交</h3>
|
||||
<p>{result.message}</p>
|
||||
<div className="dup-result-actions">
|
||||
<Button buttonType="primary" buttonSize="md" onClick={onViewResult}>
|
||||
查看结果
|
||||
</Button>
|
||||
<Button buttonType="secondary" buttonSize="md" onClick={onReset}>
|
||||
继续上传
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UploadResultPanel
|
||||
@@ -0,0 +1,45 @@
|
||||
import React from "react"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { FORMAT_LIST } from "./constants"
|
||||
|
||||
interface UploadZoneProps {
|
||||
dragging: boolean
|
||||
uploading: boolean
|
||||
onDragOver: (e: React.DragEvent) => void
|
||||
onDragLeave: () => void
|
||||
onDrop: (e: React.DragEvent) => void
|
||||
onClick: () => void
|
||||
}
|
||||
|
||||
/** 拖拽上传区 */
|
||||
const UploadZone: React.FC<UploadZoneProps> = ({
|
||||
dragging,
|
||||
uploading,
|
||||
onDragOver,
|
||||
onDragLeave,
|
||||
onDrop,
|
||||
onClick,
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
className={`dup-upload-zone ${dragging ? "dragging" : ""} ${uploading ? "disabled" : ""}`}
|
||||
onDragOver={onDragOver}
|
||||
onDragLeave={onDragLeave}
|
||||
onDrop={onDrop}
|
||||
onClick={uploading ? undefined : onClick}
|
||||
>
|
||||
<div className="dup-upload-icon">{uploading ? "⏳" : "📁"}</div>
|
||||
<h3>{uploading ? "正在上传并查重..." : "点击或拖拽视频文件到此区域"}</h3>
|
||||
<p>支持 MP4、AVI、MOV、MKV 等格式,单个文件不超过 2GB</p>
|
||||
<div className="dup-upload-formats">
|
||||
{FORMAT_LIST.map((fmt) => (
|
||||
<Tag key={fmt} variant="info">
|
||||
{fmt}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UploadZone
|
||||
@@ -0,0 +1,17 @@
|
||||
/** 支持的视频格式 */
|
||||
export const ACCEPT_FORMATS = ".mp4,.avi,.mov,.mkv,.wmv,.flv,.webm"
|
||||
export const FORMAT_LIST = ["MP4", "AVI", "MOV", "MKV", "WMV", "FLV", "WebM"]
|
||||
/** 最大文件大小:2GB */
|
||||
export const MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024
|
||||
|
||||
/** 简易 toast */
|
||||
export interface ToastState {
|
||||
message: string
|
||||
type: "success" | "error" | "warning"
|
||||
}
|
||||
|
||||
/** 上传结果 */
|
||||
export interface UploadResult {
|
||||
id: string
|
||||
message: string
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { useState, useRef, useCallback } from "react"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { uploadForDuplication } from "@/api/duplication"
|
||||
import { ACCEPT_FORMATS, FORMAT_LIST, MAX_FILE_SIZE } from "./constants"
|
||||
import type { ToastState, UploadResult } from "./constants"
|
||||
|
||||
/**
|
||||
* 查重上传逻辑 Hook
|
||||
* 封装文件校验、上传 mutation、toast 提示
|
||||
*/
|
||||
export function useDuplicationUpload() {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const [dragging, setDragging] = useState(false)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const [uploadResult, setUploadResult] = useState<UploadResult | null>(null)
|
||||
const [toast, setToast] = useState<ToastState | null>(null)
|
||||
|
||||
/** 显示 toast */
|
||||
const showToast = useCallback((message: string, type: "success" | "error" | "warning") => {
|
||||
setToast({ message, type })
|
||||
setTimeout(() => setToast(null), 3000)
|
||||
}, [])
|
||||
|
||||
// 上传查重 mutation
|
||||
const uploadMutation = useMutation({
|
||||
mutationFn: (file: File) => uploadForDuplication(file),
|
||||
onSuccess: (data) => {
|
||||
setUploading(false)
|
||||
setUploadResult({ id: data.id, message: data.message })
|
||||
showToast("查重任务已提交", "success")
|
||||
},
|
||||
onError: () => {
|
||||
setUploading(false)
|
||||
showToast("上传失败,请重试", "error")
|
||||
},
|
||||
})
|
||||
|
||||
/** 校验并上传文件 */
|
||||
const handleFile = useCallback(
|
||||
(file: File) => {
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
showToast("文件大小不能超过 2GB", "error")
|
||||
return
|
||||
}
|
||||
const ext = file.name.toLowerCase().split(".").pop()
|
||||
const allowedExts = ACCEPT_FORMATS.replace(/\./g, "").split(",")
|
||||
if (!allowedExts.includes(ext || "")) {
|
||||
showToast(`不支持的文件格式,支持:${FORMAT_LIST.join("、")}`, "error")
|
||||
return
|
||||
}
|
||||
setUploading(true)
|
||||
setUploadResult(null)
|
||||
uploadMutation.mutate(file)
|
||||
},
|
||||
[uploadMutation, showToast],
|
||||
)
|
||||
|
||||
/** 拖拽事件 */
|
||||
const handleDragOver = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
setDragging(true)
|
||||
}, [])
|
||||
|
||||
const handleDragLeave = useCallback(() => {
|
||||
setDragging(false)
|
||||
}, [])
|
||||
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
setDragging(false)
|
||||
const file = e.dataTransfer.files[0]
|
||||
if (file) handleFile(file)
|
||||
},
|
||||
[handleFile],
|
||||
)
|
||||
|
||||
/** 点击选择文件 */
|
||||
const handleSelectFile = useCallback(() => {
|
||||
fileInputRef.current?.click()
|
||||
}, [])
|
||||
|
||||
const handleFileChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) handleFile(file)
|
||||
// 重置 input 以便重复选择同一文件
|
||||
e.target.value = ""
|
||||
},
|
||||
[handleFile],
|
||||
)
|
||||
|
||||
/** 重置状态 */
|
||||
const handleReset = useCallback(() => {
|
||||
setUploadResult(null)
|
||||
setUploading(false)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
// refs
|
||||
fileInputRef,
|
||||
// 状态
|
||||
dragging,
|
||||
uploading,
|
||||
uploadResult,
|
||||
toast,
|
||||
// 事件
|
||||
handleDragOver,
|
||||
handleDragLeave,
|
||||
handleDrop,
|
||||
handleSelectFile,
|
||||
handleFileChange,
|
||||
handleReset,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useState, useEffect } from "react"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { getAssets, getAssetLibraries } from "@/api/assets"
|
||||
import type { AssetItem } from "@/api/assets"
|
||||
|
||||
/**
|
||||
* 素材库加载 Hook
|
||||
* 管理素材库列表、当前选中库、素材列表加载
|
||||
*/
|
||||
export function useMaterialLibrary() {
|
||||
/* ── 素材库数据 API ── */
|
||||
const { data: libraries = [] } = useQuery({
|
||||
queryKey: ["asset-libraries"],
|
||||
queryFn: getAssetLibraries,
|
||||
})
|
||||
const [selectedLibraryId, setSelectedLibraryId] = useState<string>("")
|
||||
|
||||
// 自动选中第一个视频库
|
||||
useEffect(() => {
|
||||
if (libraries.length > 0 && !selectedLibraryId) {
|
||||
setSelectedLibraryId(libraries[0].id)
|
||||
}
|
||||
}, [libraries, selectedLibraryId])
|
||||
|
||||
const { data: materials = { items: [], total: 0 }, isLoading: materialsLoading } = useQuery<{
|
||||
items: AssetItem[]
|
||||
total: number
|
||||
}>({
|
||||
queryKey: ["generate-assets", selectedLibraryId],
|
||||
queryFn: () => getAssets(selectedLibraryId),
|
||||
enabled: !!selectedLibraryId,
|
||||
})
|
||||
|
||||
return {
|
||||
libraries,
|
||||
selectedLibraryId,
|
||||
setSelectedLibraryId,
|
||||
materials,
|
||||
materialsLoading,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import { useState, useCallback, useMemo } from "react"
|
||||
import { message } from "antd"
|
||||
import type { AssetItem } from "@/api/assets"
|
||||
import { SMART_MATCH_REASONS } from "../../constants"
|
||||
|
||||
interface SmartMatchedResult {
|
||||
asset: AssetItem
|
||||
matchScore: number
|
||||
matchReason: string
|
||||
}
|
||||
|
||||
interface UseSmartMatchOptions {
|
||||
materials: { items: AssetItem[]; total: number }
|
||||
smartSelectedIds: string[]
|
||||
onSmartSelectedIdsChange: (ids: string[]) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能素材匹配 Hook
|
||||
* 封装 AI 匹配、换一批、全选/清空等逻辑
|
||||
*/
|
||||
export function useSmartMatch({
|
||||
materials,
|
||||
smartSelectedIds,
|
||||
onSmartSelectedIdsChange,
|
||||
}: UseSmartMatchOptions) {
|
||||
const [smartMatchInput, setSmartMatchInput] = useState("")
|
||||
const [smartMatching, setSmartMatching] = useState(false)
|
||||
const [smartMatchedResults, setSmartMatchedResults] = useState<SmartMatchedResult[]>([])
|
||||
const [hasMatched, setHasMatched] = useState(false)
|
||||
|
||||
/* ── 智能素材匹配 ── */
|
||||
const handleSmartMatch = useCallback(async () => {
|
||||
if (!smartMatchInput.trim()) {
|
||||
message.warning("请先输入视频内容描述")
|
||||
return
|
||||
}
|
||||
if (materials.items.length === 0) {
|
||||
message.warning("当前视频库暂无素材")
|
||||
return
|
||||
}
|
||||
|
||||
setSmartMatching(true)
|
||||
setHasMatched(true)
|
||||
|
||||
// 模拟 AI 匹配延迟
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500))
|
||||
|
||||
// 从素材库中随机选取 5-8 个作为推荐结果
|
||||
const shuffled = [...materials.items].sort(() => Math.random() - 0.5)
|
||||
const count = Math.min(shuffled.length, 5 + Math.floor(Math.random() * 4))
|
||||
const picked = shuffled.slice(0, count)
|
||||
|
||||
const results = picked.map((asset, idx) => ({
|
||||
asset,
|
||||
matchScore: Math.round(85 + Math.random() * 14), // 85-99 分
|
||||
matchReason:
|
||||
SMART_MATCH_REASONS[idx % SMART_MATCH_REASONS.length] +
|
||||
(Math.random() > 0.5 ? ",画面质感优秀" : ""),
|
||||
}))
|
||||
|
||||
// 按匹配度从高到低排序
|
||||
results.sort((a, b) => b.matchScore - a.matchScore)
|
||||
|
||||
setSmartMatchedResults(results)
|
||||
// 默认选中匹配度 >= 90 的素材
|
||||
const defaultSelected = results.filter((r) => r.matchScore >= 90).map((r) => r.asset.id)
|
||||
onSmartSelectedIdsChange(
|
||||
defaultSelected.length > 0 ? defaultSelected : results.slice(0, 3).map((r) => r.asset.id),
|
||||
)
|
||||
setSmartMatching(false)
|
||||
}, [smartMatchInput, materials.items, onSmartSelectedIdsChange])
|
||||
|
||||
const handleToggleSmartSelect = useCallback(
|
||||
(assetId: string) => {
|
||||
onSmartSelectedIdsChange(
|
||||
smartSelectedIds.includes(assetId)
|
||||
? smartSelectedIds.filter((id) => id !== assetId)
|
||||
: [...smartSelectedIds, assetId],
|
||||
)
|
||||
},
|
||||
[smartSelectedIds, onSmartSelectedIdsChange],
|
||||
)
|
||||
|
||||
const handleRefreshMatch = useCallback(async () => {
|
||||
if (materials.items.length <= 5) {
|
||||
message.info("视频库素材较少,无法换一批")
|
||||
return
|
||||
}
|
||||
setSmartMatching(true)
|
||||
await new Promise((resolve) => setTimeout(resolve, 800))
|
||||
|
||||
const remaining = materials.items.filter(
|
||||
(m) => !smartMatchedResults.some((r) => r.asset.id === m.id),
|
||||
)
|
||||
const shuffled = [...remaining].sort(() => Math.random() - 0.5)
|
||||
const count = Math.min(shuffled.length, 5 + Math.floor(Math.random() * 3))
|
||||
const picked = shuffled.slice(0, count)
|
||||
|
||||
const results = picked.map((asset, idx) => ({
|
||||
asset,
|
||||
matchScore: Math.round(80 + Math.random() * 19),
|
||||
matchReason:
|
||||
SMART_MATCH_REASONS[(idx + 2) % SMART_MATCH_REASONS.length] +
|
||||
(Math.random() > 0.5 ? ",节奏明快" : ""),
|
||||
}))
|
||||
results.sort((a, b) => b.matchScore - a.matchScore)
|
||||
|
||||
setSmartMatchedResults(results)
|
||||
onSmartSelectedIdsChange([])
|
||||
setSmartMatching(false)
|
||||
}, [materials.items, smartMatchedResults, onSmartSelectedIdsChange])
|
||||
|
||||
const handleSelectAllMatched = useCallback(() => {
|
||||
onSmartSelectedIdsChange(smartMatchedResults.map((r) => r.asset.id))
|
||||
}, [smartMatchedResults, onSmartSelectedIdsChange])
|
||||
|
||||
const handleClearSmartSelect = useCallback(() => {
|
||||
onSmartSelectedIdsChange([])
|
||||
}, [onSmartSelectedIdsChange])
|
||||
|
||||
/* ── 计算已选智能匹配素材的总时长 ── */
|
||||
const smartSelectedTotalDuration = useMemo(() => {
|
||||
return smartMatchedResults
|
||||
.filter((r) => smartSelectedIds.includes(r.asset.id))
|
||||
.reduce((sum, r) => sum + (r.asset.duration || 0), 0)
|
||||
}, [smartMatchedResults, smartSelectedIds])
|
||||
|
||||
return {
|
||||
smartMatchInput,
|
||||
setSmartMatchInput,
|
||||
smartMatching,
|
||||
smartMatchedResults,
|
||||
hasMatched,
|
||||
smartSelectedIds,
|
||||
handleSmartMatch,
|
||||
handleToggleSmartSelect,
|
||||
handleRefreshMatch,
|
||||
handleSelectAllMatched,
|
||||
handleClearSmartSelect,
|
||||
smartSelectedTotalDuration,
|
||||
}
|
||||
}
|
||||
Regular → Executable
+23
-149
@@ -1,20 +1,11 @@
|
||||
/**
|
||||
* Step 2 素材选择 Hook
|
||||
* 封装素材库加载、手动选择、智能匹配等逻辑
|
||||
* 组合素材库加载 + 智能匹配两个子 Hook
|
||||
*/
|
||||
import { useState, useCallback, useEffect, useMemo } from "react"
|
||||
import { message } from "antd"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { getAssets, getAssetLibraries } from "@/api/assets"
|
||||
import type { AssetItem } from "@/api/assets"
|
||||
import { useCallback } from "react"
|
||||
import { formatDuration } from "../utils/formatDuration"
|
||||
import { SMART_MATCH_REASONS } from "../constants"
|
||||
|
||||
interface SmartMatchedResult {
|
||||
asset: AssetItem
|
||||
matchScore: number
|
||||
matchReason: string
|
||||
}
|
||||
import { useMaterialLibrary } from "./step2-materials/useMaterialLibrary"
|
||||
import { useSmartMatch } from "./step2-materials/useSmartMatch"
|
||||
|
||||
interface UseStep2MaterialsProps {
|
||||
materialMode: "manual" | "auto"
|
||||
@@ -33,34 +24,14 @@ export function useStep2Materials({
|
||||
smartSelectedIds,
|
||||
onSmartSelectedIdsChange,
|
||||
}: UseStep2MaterialsProps) {
|
||||
/* ── 素材库数据 API ── */
|
||||
const { data: libraries = [] } = useQuery({
|
||||
queryKey: ["asset-libraries"],
|
||||
queryFn: getAssetLibraries,
|
||||
const { libraries, selectedLibraryId, setSelectedLibraryId, materials, materialsLoading } =
|
||||
useMaterialLibrary()
|
||||
|
||||
const smartMatch = useSmartMatch({
|
||||
materials,
|
||||
smartSelectedIds,
|
||||
onSmartSelectedIdsChange,
|
||||
})
|
||||
const [selectedLibraryId, setSelectedLibraryId] = useState<string>("")
|
||||
|
||||
// 自动选中第一个视频库
|
||||
useEffect(() => {
|
||||
if (libraries.length > 0 && !selectedLibraryId) {
|
||||
setSelectedLibraryId(libraries[0].id)
|
||||
}
|
||||
}, [libraries, selectedLibraryId])
|
||||
|
||||
const { data: materials = { items: [], total: 0 }, isLoading: materialsLoading } = useQuery<{
|
||||
items: AssetItem[]
|
||||
total: number
|
||||
}>({
|
||||
queryKey: ["generate-assets", selectedLibraryId],
|
||||
queryFn: () => getAssets(selectedLibraryId),
|
||||
enabled: !!selectedLibraryId,
|
||||
})
|
||||
|
||||
/* ── 智能素材匹配状态 ── */
|
||||
const [smartMatchInput, setSmartMatchInput] = useState("")
|
||||
const [smartMatching, setSmartMatching] = useState(false)
|
||||
const [smartMatchedResults, setSmartMatchedResults] = useState<SmartMatchedResult[]>([])
|
||||
const [hasMatched, setHasMatched] = useState(false)
|
||||
|
||||
/* ── 手动选择素材 ── */
|
||||
const handleToggleMaterial = useCallback(
|
||||
@@ -74,103 +45,6 @@ export function useStep2Materials({
|
||||
[selectedMaterials, onSelectedMaterialsChange],
|
||||
)
|
||||
|
||||
/* ── 智能素材匹配 ── */
|
||||
const handleSmartMatch = useCallback(async () => {
|
||||
if (!smartMatchInput.trim()) {
|
||||
message.warning("请先输入视频内容描述")
|
||||
return
|
||||
}
|
||||
if (materials.items.length === 0) {
|
||||
message.warning("当前视频库暂无素材")
|
||||
return
|
||||
}
|
||||
|
||||
setSmartMatching(true)
|
||||
setHasMatched(true)
|
||||
|
||||
// 模拟 AI 匹配延迟
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500))
|
||||
|
||||
// 从素材库中随机选取 5-8 个作为推荐结果
|
||||
const shuffled = [...materials.items].sort(() => Math.random() - 0.5)
|
||||
const count = Math.min(shuffled.length, 5 + Math.floor(Math.random() * 4))
|
||||
const picked = shuffled.slice(0, count)
|
||||
|
||||
const results = picked.map((asset, idx) => ({
|
||||
asset,
|
||||
matchScore: Math.round(85 + Math.random() * 14), // 85-99 分
|
||||
matchReason:
|
||||
SMART_MATCH_REASONS[idx % SMART_MATCH_REASONS.length] +
|
||||
(Math.random() > 0.5 ? ",画面质感优秀" : ""),
|
||||
}))
|
||||
|
||||
// 按匹配度从高到低排序
|
||||
results.sort((a, b) => b.matchScore - a.matchScore)
|
||||
|
||||
setSmartMatchedResults(results)
|
||||
// 默认选中匹配度 >= 90 的素材
|
||||
const defaultSelected = results.filter((r) => r.matchScore >= 90).map((r) => r.asset.id)
|
||||
onSmartSelectedIdsChange(
|
||||
defaultSelected.length > 0 ? defaultSelected : results.slice(0, 3).map((r) => r.asset.id),
|
||||
)
|
||||
setSmartMatching(false)
|
||||
}, [smartMatchInput, materials.items, onSmartSelectedIdsChange])
|
||||
|
||||
const handleToggleSmartSelect = useCallback(
|
||||
(assetId: string) => {
|
||||
onSmartSelectedIdsChange(
|
||||
smartSelectedIds.includes(assetId)
|
||||
? smartSelectedIds.filter((id) => id !== assetId)
|
||||
: [...smartSelectedIds, assetId],
|
||||
)
|
||||
},
|
||||
[smartSelectedIds, onSmartSelectedIdsChange],
|
||||
)
|
||||
|
||||
const handleRefreshMatch = useCallback(async () => {
|
||||
if (materials.items.length <= 5) {
|
||||
message.info("视频库素材较少,无法换一批")
|
||||
return
|
||||
}
|
||||
setSmartMatching(true)
|
||||
await new Promise((resolve) => setTimeout(resolve, 800))
|
||||
|
||||
const remaining = materials.items.filter(
|
||||
(m) => !smartMatchedResults.some((r) => r.asset.id === m.id),
|
||||
)
|
||||
const shuffled = [...remaining].sort(() => Math.random() - 0.5)
|
||||
const count = Math.min(shuffled.length, 5 + Math.floor(Math.random() * 3))
|
||||
const picked = shuffled.slice(0, count)
|
||||
|
||||
const results = picked.map((asset, idx) => ({
|
||||
asset,
|
||||
matchScore: Math.round(80 + Math.random() * 19),
|
||||
matchReason:
|
||||
SMART_MATCH_REASONS[(idx + 2) % SMART_MATCH_REASONS.length] +
|
||||
(Math.random() > 0.5 ? ",节奏明快" : ""),
|
||||
}))
|
||||
results.sort((a, b) => b.matchScore - a.matchScore)
|
||||
|
||||
setSmartMatchedResults(results)
|
||||
onSmartSelectedIdsChange([])
|
||||
setSmartMatching(false)
|
||||
}, [materials.items, smartMatchedResults, onSmartSelectedIdsChange])
|
||||
|
||||
const handleSelectAllMatched = useCallback(() => {
|
||||
onSmartSelectedIdsChange(smartMatchedResults.map((r) => r.asset.id))
|
||||
}, [smartMatchedResults, onSmartSelectedIdsChange])
|
||||
|
||||
const handleClearSmartSelect = useCallback(() => {
|
||||
onSmartSelectedIdsChange([])
|
||||
}, [onSmartSelectedIdsChange])
|
||||
|
||||
/* ── 计算已选智能匹配素材的总时长 ── */
|
||||
const smartSelectedTotalDuration = useMemo(() => {
|
||||
return smartMatchedResults
|
||||
.filter((r) => smartSelectedIds.includes(r.asset.id))
|
||||
.reduce((sum, r) => sum + (r.asset.duration || 0), 0)
|
||||
}, [smartMatchedResults, smartSelectedIds])
|
||||
|
||||
return {
|
||||
// 素材库
|
||||
libraries,
|
||||
@@ -185,18 +59,18 @@ export function useStep2Materials({
|
||||
selectedMaterials,
|
||||
handleToggleMaterial,
|
||||
// 智能匹配
|
||||
smartMatchInput,
|
||||
setSmartMatchInput,
|
||||
smartMatching,
|
||||
smartMatchedResults,
|
||||
hasMatched,
|
||||
smartSelectedIds,
|
||||
handleSmartMatch,
|
||||
handleToggleSmartSelect,
|
||||
handleRefreshMatch,
|
||||
handleSelectAllMatched,
|
||||
handleClearSmartSelect,
|
||||
smartSelectedTotalDuration,
|
||||
smartMatchInput: smartMatch.smartMatchInput,
|
||||
setSmartMatchInput: smartMatch.setSmartMatchInput,
|
||||
smartMatching: smartMatch.smartMatching,
|
||||
smartMatchedResults: smartMatch.smartMatchedResults,
|
||||
hasMatched: smartMatch.hasMatched,
|
||||
smartSelectedIds: smartMatch.smartSelectedIds,
|
||||
handleSmartMatch: smartMatch.handleSmartMatch,
|
||||
handleToggleSmartSelect: smartMatch.handleToggleSmartSelect,
|
||||
handleRefreshMatch: smartMatch.handleRefreshMatch,
|
||||
handleSelectAllMatched: smartMatch.handleSelectAllMatched,
|
||||
handleClearSmartSelect: smartMatch.handleClearSmartSelect,
|
||||
smartSelectedTotalDuration: smartMatch.smartSelectedTotalDuration,
|
||||
// utils
|
||||
formatDuration,
|
||||
}
|
||||
|
||||
Regular → Executable
+20
-136
@@ -1,25 +1,13 @@
|
||||
import React from "react"
|
||||
import { Button, Descriptions, Tooltip } from "antd"
|
||||
import { CopyOutlined, ThunderboltOutlined } from "@ant-design/icons"
|
||||
import type { TemplateItem, TemplateSegment } from "@/api/templates"
|
||||
import {
|
||||
gradientForCategory,
|
||||
getTypeColor,
|
||||
formatDuration,
|
||||
formatConfig,
|
||||
getMaterialTypeLabel,
|
||||
calcTotalSegmentDuration,
|
||||
} from "../../utils/templateLibrary"
|
||||
import { Descriptions } from "antd"
|
||||
import type { TemplateDetailModalProps } from "./template-detail-modal/types"
|
||||
import PreviewArea from "./template-detail-modal/PreviewArea"
|
||||
import SegmentList from "./template-detail-modal/SegmentList"
|
||||
import StyleConfig from "./template-detail-modal/StyleConfig"
|
||||
import DetailFooter from "./template-detail-modal/DetailFooter"
|
||||
import { getTypeColor, formatDuration } from "../../utils/templateLibrary"
|
||||
import { TEMPLATE_TYPES } from "../../constants/templateLibrary"
|
||||
|
||||
interface TemplateDetailModalProps {
|
||||
template: TemplateItem
|
||||
isFavorite: boolean
|
||||
onClose: () => void
|
||||
onToggleFavorite: (id: string) => void
|
||||
onUse: (template: TemplateItem) => void
|
||||
onCopy: (template: TemplateItem) => void
|
||||
}
|
||||
import { calcTotalSegmentDuration } from "../../utils/templateLibrary"
|
||||
|
||||
export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
|
||||
template,
|
||||
@@ -31,6 +19,7 @@ export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
|
||||
}) => {
|
||||
const segments = template.segments ?? []
|
||||
const totalSegmentDuration = calcTotalSegmentDuration(segments)
|
||||
const typeInfo = TEMPLATE_TYPES.find((t) => t.type === template.category)
|
||||
|
||||
return (
|
||||
<div className="xx-template-modal-overlay" onClick={onClose}>
|
||||
@@ -38,33 +27,8 @@ export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
|
||||
className="xx-template-modal xx-template-modal-wide"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{/* 关闭按钮 */}
|
||||
<button className="xx-template-modal-close" onClick={onClose} title="关闭">
|
||||
✕
|
||||
</button>
|
||||
<PreviewArea template={template} onClose={onClose} />
|
||||
|
||||
{/* 预览区域 */}
|
||||
<div
|
||||
className="xx-template-modal-preview"
|
||||
style={{ background: gradientForCategory(template.category) }}
|
||||
>
|
||||
{template.thumbnail_url ? (
|
||||
<img
|
||||
src={template.thumbnail_url}
|
||||
alt={template.name}
|
||||
className="xx-template-modal-thumb-img"
|
||||
/>
|
||||
) : (
|
||||
<div className="xx-template-modal-preview-content">
|
||||
<span className="xx-template-preview-icon">
|
||||
{TEMPLATE_TYPES.find((t) => t.type === template.category)?.icon ?? "📋"}
|
||||
</span>
|
||||
<span className="xx-template-preview-title">{template.name}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 内容区域 */}
|
||||
<div className="xx-template-modal-content">
|
||||
{/* 标题行 */}
|
||||
<div className="xx-template-modal-title-row">
|
||||
@@ -76,7 +40,7 @@ export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
|
||||
background: `${getTypeColor(template.category)}18`,
|
||||
}}
|
||||
>
|
||||
{TEMPLATE_TYPES.find((t) => t.type === template.category)?.icon} {template.category}
|
||||
{typeInfo?.icon} {template.category}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -123,95 +87,15 @@ export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
|
||||
]}
|
||||
/>
|
||||
|
||||
{/* 素材规则(片段配置) */}
|
||||
{segments.length > 0 && (
|
||||
<div className="xx-template-modal-section">
|
||||
<h4>🎬 素材规则</h4>
|
||||
<div className="xx-template-modal-clip-list">
|
||||
{segments
|
||||
.sort((a, b) => a.segment_order - b.segment_order)
|
||||
.map((seg: TemplateSegment, idx: number) => (
|
||||
<div key={seg.id ?? idx} className="xx-template-modal-clip-item">
|
||||
<span className="xx-template-modal-clip-order">#{seg.segment_order}</span>
|
||||
<span
|
||||
className="xx-template-modal-clip-badge"
|
||||
style={{
|
||||
color: seg.material_type ? getTypeColor(seg.material_type) : "#64748b",
|
||||
background: seg.material_type
|
||||
? `${getTypeColor(seg.material_type)}18`
|
||||
: "#f1f5f9",
|
||||
}}
|
||||
>
|
||||
{getMaterialTypeLabel(seg.material_type)}
|
||||
</span>
|
||||
<span className="xx-template-modal-clip-desc">
|
||||
{seg.description || `片段 ${seg.segment_order}`}
|
||||
</span>
|
||||
<Tooltip title={`时长范围: ${seg.duration_min}秒 - ${seg.duration_max}秒`}>
|
||||
<span className="xx-template-modal-clip-duration">
|
||||
{seg.duration_min}-{seg.duration_max}秒
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="xx-template-modal-total-duration">
|
||||
预估总时长:{formatDuration(Math.round(totalSegmentDuration))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 样式配置 */}
|
||||
<div className="xx-template-modal-section">
|
||||
<h4>🎨 样式配置</h4>
|
||||
<div className="xx-template-modal-style-grid">
|
||||
<div className="xx-template-modal-style-item">
|
||||
<span className="xx-template-modal-style-label">字幕样式</span>
|
||||
<span className="xx-template-modal-style-value">
|
||||
{formatConfig(template.subtitle_config)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="xx-template-modal-style-item">
|
||||
<span className="xx-template-modal-style-label">标题样式</span>
|
||||
<span className="xx-template-modal-style-value">
|
||||
{formatConfig(template.title_config)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="xx-template-modal-style-item">
|
||||
<span className="xx-template-modal-style-label">BGM 配置</span>
|
||||
<span className="xx-template-modal-style-value">
|
||||
{formatConfig(template.bgm_config)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="xx-template-modal-style-item">
|
||||
<span className="xx-template-modal-style-label">视频比例</span>
|
||||
<span className="xx-template-modal-style-value">
|
||||
{template.aspect_ratio ?? "16:9"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 统计信息 */}
|
||||
<div className="xx-template-modal-stats">
|
||||
<span>已使用 {template.usage_count ?? 0} 次</span>
|
||||
<button
|
||||
className={`xx-template-modal-fav-btn${isFavorite ? " is-favorite" : ""}`}
|
||||
onClick={() => onToggleFavorite(template.id)}
|
||||
>
|
||||
{isFavorite ? "★ 已收藏" : "☆ 收藏"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<div className="xx-template-modal-actions">
|
||||
<Button icon={<CopyOutlined />} onClick={() => onCopy(template)}>
|
||||
复制模板
|
||||
</Button>
|
||||
<Button type="primary" icon={<ThunderboltOutlined />} onClick={() => onUse(template)}>
|
||||
使用此模板生成
|
||||
</Button>
|
||||
</div>
|
||||
<SegmentList segments={segments} totalDuration={totalSegmentDuration} />
|
||||
<StyleConfig template={template} />
|
||||
<DetailFooter
|
||||
template={template}
|
||||
isFavorite={isFavorite}
|
||||
onToggleFavorite={onToggleFavorite}
|
||||
onUse={onUse}
|
||||
onCopy={onCopy}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
import React from "react"
|
||||
import { Button } from "antd"
|
||||
import { CopyOutlined, ThunderboltOutlined } from "@ant-design/icons"
|
||||
import type { TemplateItem } from "@/api/templates"
|
||||
|
||||
interface DetailFooterProps {
|
||||
template: TemplateItem
|
||||
isFavorite: boolean
|
||||
onToggleFavorite: (id: string) => void
|
||||
onUse: (template: TemplateItem) => void
|
||||
onCopy: (template: TemplateItem) => void
|
||||
}
|
||||
|
||||
/** 底部操作区:统计 + 收藏 + 按钮 */
|
||||
const DetailFooter: React.FC<DetailFooterProps> = ({
|
||||
template,
|
||||
isFavorite,
|
||||
onToggleFavorite,
|
||||
onUse,
|
||||
onCopy,
|
||||
}) => {
|
||||
return (
|
||||
<>
|
||||
<div className="xx-template-modal-stats">
|
||||
<span>已使用 {template.usage_count ?? 0} 次</span>
|
||||
<button
|
||||
className={`xx-template-modal-fav-btn${isFavorite ? " is-favorite" : ""}`}
|
||||
onClick={() => onToggleFavorite(template.id)}
|
||||
>
|
||||
{isFavorite ? "★ 已收藏" : "☆ 收藏"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="xx-template-modal-actions">
|
||||
<Button icon={<CopyOutlined />} onClick={() => onCopy(template)}>
|
||||
复制模板
|
||||
</Button>
|
||||
<Button type="primary" icon={<ThunderboltOutlined />} onClick={() => onUse(template)}>
|
||||
使用此模板生成
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default DetailFooter
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
import React from "react"
|
||||
import type { TemplateItem } from "@/api/templates"
|
||||
import { gradientForCategory } from "../../../utils/templateLibrary"
|
||||
import { TEMPLATE_TYPES } from "../../../constants/templateLibrary"
|
||||
|
||||
interface PreviewAreaProps {
|
||||
template: TemplateItem
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
/** 预览区域 */
|
||||
const PreviewArea: React.FC<PreviewAreaProps> = ({ template, onClose }) => {
|
||||
const typeInfo = TEMPLATE_TYPES.find((t) => t.type === template.category)
|
||||
|
||||
return (
|
||||
<div
|
||||
className="xx-template-modal-preview"
|
||||
style={{ background: gradientForCategory(template.category) }}
|
||||
>
|
||||
<button className="xx-template-modal-close" onClick={onClose} title="关闭">
|
||||
✕
|
||||
</button>
|
||||
{template.thumbnail_url ? (
|
||||
<img
|
||||
src={template.thumbnail_url}
|
||||
alt={template.name}
|
||||
className="xx-template-modal-thumb-img"
|
||||
/>
|
||||
) : (
|
||||
<div className="xx-template-modal-preview-content">
|
||||
<span className="xx-template-preview-icon">{typeInfo?.icon ?? "📋"}</span>
|
||||
<span className="xx-template-preview-title">{template.name}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PreviewArea
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
import React from "react"
|
||||
import { Tooltip } from "antd"
|
||||
import type { TemplateSegment } from "@/api/templates"
|
||||
import { getMaterialTypeLabel, getTypeColor, formatDuration } from "../../../utils/templateLibrary"
|
||||
|
||||
interface SegmentListProps {
|
||||
segments: TemplateSegment[]
|
||||
totalDuration: number
|
||||
}
|
||||
|
||||
/** 素材规则 / 片段列表 */
|
||||
const SegmentList: React.FC<SegmentListProps> = ({ segments, totalDuration }) => {
|
||||
if (segments.length === 0) return null
|
||||
|
||||
return (
|
||||
<div className="xx-template-modal-section">
|
||||
<h4>🎬 素材规则</h4>
|
||||
<div className="xx-template-modal-clip-list">
|
||||
{segments
|
||||
.sort((a, b) => a.segment_order - b.segment_order)
|
||||
.map((seg, idx) => (
|
||||
<div key={seg.id ?? idx} className="xx-template-modal-clip-item">
|
||||
<span className="xx-template-modal-clip-order">#{seg.segment_order}</span>
|
||||
<span
|
||||
className="xx-template-modal-clip-badge"
|
||||
style={{
|
||||
color: seg.material_type ? getTypeColor(seg.material_type) : "#64748b",
|
||||
background: seg.material_type
|
||||
? `${getTypeColor(seg.material_type)}18`
|
||||
: "#f1f5f9",
|
||||
}}
|
||||
>
|
||||
{getMaterialTypeLabel(seg.material_type)}
|
||||
</span>
|
||||
<span className="xx-template-modal-clip-desc">
|
||||
{seg.description || `片段 ${seg.segment_order}`}
|
||||
</span>
|
||||
<Tooltip title={`时长范围: ${seg.duration_min}秒 - ${seg.duration_max}秒`}>
|
||||
<span className="xx-template-modal-clip-duration">
|
||||
{seg.duration_min}-{seg.duration_max}秒
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="xx-template-modal-total-duration">
|
||||
预估总时长:{formatDuration(Math.round(totalDuration))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SegmentList
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
import React from "react"
|
||||
import type { TemplateItem } from "@/api/templates"
|
||||
import { formatConfig } from "../../../utils/templateLibrary"
|
||||
|
||||
interface StyleConfigProps {
|
||||
template: TemplateItem
|
||||
}
|
||||
|
||||
/** 样式配置网格 */
|
||||
const StyleConfig: React.FC<StyleConfigProps> = ({ template }) => {
|
||||
const items = [
|
||||
{ label: "字幕样式", value: formatConfig(template.subtitle_config) },
|
||||
{ label: "标题样式", value: formatConfig(template.title_config) },
|
||||
{ label: "BGM 配置", value: formatConfig(template.bgm_config) },
|
||||
{ label: "视频比例", value: template.aspect_ratio ?? "16:9" },
|
||||
]
|
||||
|
||||
return (
|
||||
<div className="xx-template-modal-section">
|
||||
<h4>🎨 样式配置</h4>
|
||||
<div className="xx-template-modal-style-grid">
|
||||
{items.map((item) => (
|
||||
<div key={item.label} className="xx-template-modal-style-item">
|
||||
<span className="xx-template-modal-style-label">{item.label}</span>
|
||||
<span className="xx-template-modal-style-value">{item.value}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default StyleConfig
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export { TemplateDetailModal } from "../TemplateDetailModal"
|
||||
export * from "./types"
|
||||
export { default as PreviewArea } from "./PreviewArea"
|
||||
export { default as SegmentList } from "./SegmentList"
|
||||
export { default as StyleConfig } from "./StyleConfig"
|
||||
export { default as DetailFooter } from "./DetailFooter"
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { TemplateItem } from "@/api/templates"
|
||||
|
||||
export interface TemplateDetailModalProps {
|
||||
template: TemplateItem
|
||||
isFavorite: boolean
|
||||
onClose: () => void
|
||||
onToggleFavorite: (id: string) => void
|
||||
onUse: (template: TemplateItem) => void
|
||||
onCopy: (template: TemplateItem) => void
|
||||
}
|
||||
Regular → Executable
+3
-3
@@ -9,7 +9,7 @@
|
||||
* - 删除素材
|
||||
*/
|
||||
import React from "react"
|
||||
import { AudioOutlined, PlusOutlined, RobotOutlined } from "@ant-design/icons"
|
||||
import { PlusOutlined, RobotOutlined } from "@ant-design/icons"
|
||||
import { Button, Modal } from "@/components/ui"
|
||||
import PageHead from "@/components/layout/PageHead"
|
||||
import { useVoiceMaterials } from "./hooks/useVoiceMaterials"
|
||||
@@ -315,8 +315,8 @@ const VoiceMaterialLibrary: React.FC = () => {
|
||||
voiceId={ttsVoiceId}
|
||||
speed={ttsSpeed}
|
||||
status={ttsStatus}
|
||||
audioUrl={ttsAudioUrl}
|
||||
error={ttsError}
|
||||
audioUrl={ttsAudioUrl ?? ""}
|
||||
error={ttsError ?? ""}
|
||||
presetVoices={presetVoices}
|
||||
onClose={handleTtsClose}
|
||||
onTextChange={setTtsText}
|
||||
|
||||
Regular → Executable
+5
-81
@@ -1,11 +1,10 @@
|
||||
import React, { useState, useRef } from "react"
|
||||
import { UploadOutlined, SoundOutlined, CloseOutlined } from "@ant-design/icons"
|
||||
import React, { useState } from "react"
|
||||
import { Button, Input } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { type VoiceGender, type VoiceMaterial } from "../types"
|
||||
import { GENDER_OPTIONS } from "../constants"
|
||||
import { genderClass, formatFileSize } from "../utils/format"
|
||||
import TagSelector from "./TagSelector"
|
||||
import FileUploadField from "./material-form/FileUploadField"
|
||||
import GenderSelector from "./material-form/GenderSelector"
|
||||
|
||||
export interface MaterialFormProps {
|
||||
initial?: VoiceMaterial
|
||||
@@ -33,7 +32,6 @@ const MaterialForm: React.FC<MaterialFormProps> = ({
|
||||
const [gender, setGender] = useState<VoiceGender>(initial?.gender ?? "female")
|
||||
const [selectedTagIds, setSelectedTagIds] = useState<string[]>(initial?.tagIds ?? [])
|
||||
const [file, setFile] = useState<File | undefined>(undefined)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!name.trim()) return
|
||||
@@ -53,65 +51,10 @@ const MaterialForm: React.FC<MaterialFormProps> = ({
|
||||
|
||||
return (
|
||||
<div className="vmat-form">
|
||||
{/* 音频文件上传(编辑模式不显示) */}
|
||||
{!initial && (
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">音频文件 *</label>
|
||||
<div
|
||||
className="vmat-upload-zone"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
onDragOver={(e) => e.preventDefault()}
|
||||
onDrop={(e) => {
|
||||
e.preventDefault()
|
||||
const f = e.dataTransfer.files[0]
|
||||
if (f?.type.startsWith("audio/")) setFile(f)
|
||||
}}
|
||||
>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="audio/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0]
|
||||
if (f) setFile(f)
|
||||
}}
|
||||
/>
|
||||
{file ? (
|
||||
<div className="vmat-upload-selected">
|
||||
<SoundOutlined className="vmat-upload-icon" />
|
||||
<span className="vmat-upload-filename">{file.name}</span>
|
||||
<span className="vmat-upload-filesize">{formatFileSize(file.size)}</span>
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-upload-clear"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setFile(undefined)
|
||||
}}
|
||||
>
|
||||
<CloseOutlined />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="vmat-upload-placeholder">
|
||||
<UploadOutlined className="vmat-upload-icon" />
|
||||
<p>点击或拖拽音频文件到此处</p>
|
||||
<span>支持 MP3、WAV、AAC、FLAC 等格式</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{/* 上传进度条 */}
|
||||
{uploadProgress !== null && uploadProgress !== undefined && (
|
||||
<div className="vmat-upload-progress">
|
||||
<div className="vmat-upload-progress-bar" style={{ width: `${uploadProgress}%` }} />
|
||||
<span className="vmat-upload-progress-text">{uploadProgress}%</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<FileUploadField file={file} onChange={setFile} uploadProgress={uploadProgress} />
|
||||
)}
|
||||
|
||||
{/* 名称 */}
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">名称 *</label>
|
||||
<Input
|
||||
@@ -122,7 +65,6 @@ const MaterialForm: React.FC<MaterialFormProps> = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 音色描述 */}
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">音色描述</label>
|
||||
<Input.TextArea
|
||||
@@ -134,25 +76,8 @@ const MaterialForm: React.FC<MaterialFormProps> = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 性别 */}
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">性别</label>
|
||||
<div className="vmat-gender-group">
|
||||
{GENDER_OPTIONS.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
className={`vmat-gender-btn${gender === opt.value ? " active" : ""} ${genderClass(opt.value)}`}
|
||||
onClick={() => setGender(opt.value)}
|
||||
>
|
||||
{opt.icon}
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<GenderSelector value={gender} onChange={setGender} />
|
||||
|
||||
{/* 风格标签 */}
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">风格标签</label>
|
||||
<TagSelector
|
||||
@@ -164,7 +89,6 @@ const MaterialForm: React.FC<MaterialFormProps> = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<div className="vmat-form-actions">
|
||||
<Button buttonType="ghost" buttonSize="md" onClick={onCancel}>
|
||||
取消
|
||||
|
||||
Regular → Executable
+16
-72
@@ -1,7 +1,8 @@
|
||||
import React, { useState, useRef, useCallback, useMemo } from "react"
|
||||
import React from "react"
|
||||
import { CheckOutlined } from "@ant-design/icons"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import type { TagItem } from "@/api/tags"
|
||||
import { useTagInput } from "./tag-selector/useTagInput"
|
||||
|
||||
export interface TagSelectorProps {
|
||||
/** 已选标签 ID 列表 */
|
||||
@@ -24,77 +25,22 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
onCreateTag,
|
||||
placeholder = "输入标签后回车添加",
|
||||
}) => {
|
||||
const [inputVal, setInputVal] = useState("")
|
||||
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
/** 按名称查找已有标签(大小写不敏感) */
|
||||
const findTagByName = useCallback(
|
||||
(name: string) => tags.find((t) => t.name.toLowerCase() === name.toLowerCase()),
|
||||
[tags],
|
||||
)
|
||||
|
||||
/** 去重添加标签(按 ID) */
|
||||
const addTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
if (value.includes(tagId)) return
|
||||
onChange([...value, tagId])
|
||||
setInputVal("")
|
||||
setShowSuggestions(false)
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
/** 输入自定义标签名:若已存在则直接选,否则创建新标签 */
|
||||
const addTagByName = useCallback(
|
||||
async (name: string) => {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return
|
||||
const existing = findTagByName(trimmed)
|
||||
if (existing) {
|
||||
addTagId(existing.id)
|
||||
} else {
|
||||
try {
|
||||
const created = await onCreateTag(trimmed)
|
||||
addTagId(created.id)
|
||||
} catch {
|
||||
/* 创建失败静默忽略 */
|
||||
}
|
||||
}
|
||||
},
|
||||
[findTagByName, addTagId, onCreateTag],
|
||||
)
|
||||
|
||||
const removeTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
onChange(value.filter((t) => t !== tagId))
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
/** 输入补全建议(排除已选) */
|
||||
const suggestions = useMemo(() => {
|
||||
if (!inputVal.trim()) return []
|
||||
const lower = inputVal.toLowerCase()
|
||||
return tags.filter((t) => t.name.toLowerCase().includes(lower) && !value.includes(t.id))
|
||||
}, [inputVal, tags, value])
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault()
|
||||
if (suggestions.length > 0) {
|
||||
addTagId(suggestions[0].id)
|
||||
} else {
|
||||
addTagByName(inputVal)
|
||||
}
|
||||
} else if (e.key === "Backspace" && !inputVal && value.length > 0) {
|
||||
removeTagId(value[value.length - 1])
|
||||
}
|
||||
}
|
||||
const {
|
||||
inputVal,
|
||||
setInputVal,
|
||||
showSuggestions,
|
||||
setShowSuggestions,
|
||||
inputRef,
|
||||
suggestions,
|
||||
addTagId,
|
||||
removeTagId,
|
||||
handleKeyDown,
|
||||
focus,
|
||||
} = useTagInput({ value, onChange, tags, onCreateTag })
|
||||
|
||||
return (
|
||||
<div className="vmat-tag-selector-wrapper">
|
||||
<div className="vmat-tag-selector" onClick={() => inputRef.current?.focus()}>
|
||||
<div className="vmat-tag-selector" onClick={focus}>
|
||||
{value.map((tagId) => (
|
||||
<Tag key={tagId} variant="info" closable onClose={() => removeTagId(tagId)}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
@@ -115,7 +61,6 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 自动补全下拉 */}
|
||||
{showSuggestions && suggestions.length > 0 && (
|
||||
<div className="vmat-tag-suggestions">
|
||||
{suggestions.slice(0, 6).map((tag) => (
|
||||
@@ -134,7 +79,6 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 已有标签快捷选择 */}
|
||||
{tags.length > 0 && (
|
||||
<div className="vmat-tag-selector-presets">
|
||||
{tags.map((tag) => {
|
||||
|
||||
Regular → Executable
+1
-1
@@ -35,7 +35,7 @@ const TtsModal: React.FC<TtsModalProps> = ({
|
||||
audioUrl,
|
||||
error,
|
||||
presetVoices,
|
||||
onClose,
|
||||
onClose: _onClose,
|
||||
onTextChange,
|
||||
onVoiceChange,
|
||||
onSpeedChange,
|
||||
|
||||
Regular → Executable
+35
-208
@@ -1,45 +1,12 @@
|
||||
import React, { useRef } from "react"
|
||||
import {
|
||||
AudioOutlined,
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
EditOutlined,
|
||||
DeleteOutlined,
|
||||
CheckOutlined,
|
||||
SoundOutlined,
|
||||
MutedOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { type VoiceMaterial } from "../types"
|
||||
import { MAX_CARD_TAGS, TAG_VARIANTS } from "../constants"
|
||||
import {
|
||||
genderClass,
|
||||
genderIcon,
|
||||
genderLabel,
|
||||
formatDuration,
|
||||
formatFileSize,
|
||||
formatDate,
|
||||
} from "../utils/format"
|
||||
|
||||
export interface VoiceCardProps {
|
||||
material: VoiceMaterial
|
||||
isPlaying: boolean
|
||||
currentTime: number
|
||||
isSelected: boolean
|
||||
batchMode: boolean
|
||||
volume: number
|
||||
tagMap: Map<string, TagItem>
|
||||
onPlay: () => void
|
||||
onPause: () => void
|
||||
onSeek: (time: number) => void
|
||||
onEdit: () => void
|
||||
onDelete: () => void
|
||||
onToggleSelect: (id: string) => void
|
||||
onVolumeChange: (e: React.ChangeEvent<HTMLInputElement>) => void
|
||||
onToggleMute: () => void
|
||||
}
|
||||
import React from "react"
|
||||
import { type VoiceCardProps } from "./voice-material-card/types"
|
||||
import BatchCheckbox from "./voice-material-card/BatchCheckbox"
|
||||
import CardActions from "./voice-material-card/CardActions"
|
||||
import CardHeader from "./voice-material-card/CardHeader"
|
||||
import CardTags from "./voice-material-card/CardTags"
|
||||
import CardMeta from "./voice-material-card/CardMeta"
|
||||
import CardPlayer from "./voice-material-card/CardPlayer"
|
||||
import { genderClass } from "../utils/format"
|
||||
|
||||
const VoiceMaterialCard: React.FC<VoiceCardProps> = ({
|
||||
material,
|
||||
@@ -58,29 +25,6 @@ const VoiceMaterialCard: React.FC<VoiceCardProps> = ({
|
||||
onVolumeChange,
|
||||
onToggleMute,
|
||||
}) => {
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleProgressMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * material.duration)
|
||||
}
|
||||
doSeek(e.nativeEvent)
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
}
|
||||
|
||||
const progress = material.duration > 0 ? (currentTime / material.duration) * 100 : 0
|
||||
|
||||
const handleCardClick = () => {
|
||||
if (batchMode) {
|
||||
onToggleSelect(material.id)
|
||||
@@ -92,154 +36,37 @@ const VoiceMaterialCard: React.FC<VoiceCardProps> = ({
|
||||
className={`vmat-card ${genderClass(material.gender)}${isPlaying ? " playing" : ""}${isSelected ? " selected" : ""}${batchMode ? " batch-mode" : ""}`}
|
||||
onClick={handleCardClick}
|
||||
>
|
||||
{/* 批量选择 checkbox */}
|
||||
{(batchMode || isSelected) && (
|
||||
<div
|
||||
className={`vmat-card-checkbox vmat-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggleSelect(material.id)
|
||||
}}
|
||||
>
|
||||
{isSelected && <CheckOutlined />}
|
||||
</div>
|
||||
)}
|
||||
<BatchCheckbox
|
||||
isSelected={isSelected}
|
||||
visible={batchMode || isSelected}
|
||||
onToggle={() => onToggleSelect(material.id)}
|
||||
/>
|
||||
<CardActions onEdit={onEdit} onDelete={onDelete} />
|
||||
<CardHeader material={material} />
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<div className="vmat-card-actions">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-card-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onEdit()
|
||||
}}
|
||||
title="编辑"
|
||||
>
|
||||
<EditOutlined />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-card-action-btn vmat-card-action-btn--danger"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onDelete()
|
||||
}}
|
||||
title="删除"
|
||||
>
|
||||
<DeleteOutlined />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 头部:图标 + 名称 + 性别 */}
|
||||
<div className="vmat-card-header">
|
||||
<div className="vmat-card-avatar">
|
||||
<AudioOutlined />
|
||||
</div>
|
||||
<div className="vmat-card-title-area">
|
||||
<h4 className="vmat-card-name" title={material.name}>
|
||||
{material.name}
|
||||
</h4>
|
||||
<span className={`vmat-card-gender ${genderClass(material.gender)}`}>
|
||||
{genderIcon(material.gender)}
|
||||
{genderLabel(material.gender)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
{material.description && <p className="vmat-card-desc">{material.description}</p>}
|
||||
|
||||
{/* 标签 */}
|
||||
<div className="vmat-card-tags">
|
||||
{material.tagIds.length === 0 ? (
|
||||
<span
|
||||
className="vmat-tag-empty"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onEdit()
|
||||
}}
|
||||
>
|
||||
添加标签
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
{material.tagIds.slice(0, MAX_CARD_TAGS).map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{material.tagIds.length > MAX_CARD_TAGS && (
|
||||
<Tooltip
|
||||
title={material.tagIds
|
||||
.slice(MAX_CARD_TAGS)
|
||||
.map((id) => tagMap.get(id)?.name ?? id)
|
||||
.join("、")}
|
||||
>
|
||||
<Tag className="vmat-tag-overflow">+{material.tagIds.length - MAX_CARD_TAGS}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 元信息 */}
|
||||
<div className="vmat-card-meta">
|
||||
<span>{formatDuration(material.duration)}</span>
|
||||
<span>{formatFileSize(material.fileSize)}</span>
|
||||
<span>{formatDate(material.createdAt)}</span>
|
||||
</div>
|
||||
|
||||
{/* 播放控制 */}
|
||||
<div className="vmat-card-player">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
disabled={!material.fileUrl}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div ref={progressRef} className="vmat-progress" onMouseDown={handleProgressMouseDown}>
|
||||
<div className="vmat-progress-bar" style={{ width: `${progress}%` }} />
|
||||
{isPlaying && <div className="vmat-progress-thumb" style={{ left: `${progress}%` }} />}
|
||||
</div>
|
||||
<span className="vmat-time">
|
||||
{isPlaying ? formatDuration(currentTime) : formatDuration(material.duration)}
|
||||
</span>
|
||||
{/* 音量控制 */}
|
||||
<div className="vmat-volume">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-volume-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggleMute()
|
||||
}}
|
||||
title={volume === 0 ? "取消静音" : "静音"}
|
||||
>
|
||||
{volume === 0 ? <MutedOutlined /> : <SoundOutlined />}
|
||||
</button>
|
||||
<input
|
||||
type="range"
|
||||
className="vmat-volume-slider"
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.05}
|
||||
value={volume}
|
||||
onChange={(e) => {
|
||||
e.stopPropagation()
|
||||
onVolumeChange(e)
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<CardTags tagIds={material.tagIds} tagMap={tagMap} onEdit={onEdit} />
|
||||
<CardMeta
|
||||
duration={material.duration}
|
||||
fileSize={material.fileSize}
|
||||
createdAt={material.createdAt}
|
||||
/>
|
||||
<CardPlayer
|
||||
isPlaying={isPlaying}
|
||||
currentTime={currentTime}
|
||||
duration={material.duration}
|
||||
volume={volume}
|
||||
fileUrl={material.fileUrl}
|
||||
onPlay={onPlay}
|
||||
onPause={onPause}
|
||||
onSeek={onSeek}
|
||||
onVolumeChange={onVolumeChange}
|
||||
onToggleMute={onToggleMute}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default VoiceMaterialCard
|
||||
export type { VoiceCardProps }
|
||||
|
||||
Regular → Executable
+9
-57
@@ -1,4 +1,4 @@
|
||||
import React, { useRef } from "react"
|
||||
import React from "react"
|
||||
import {
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
DeleteOutlined,
|
||||
CheckOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { type VoiceMaterial } from "../types"
|
||||
import { MAX_ROW_TAGS, TAG_VARIANTS } from "../constants"
|
||||
import {
|
||||
genderClass,
|
||||
genderIcon,
|
||||
@@ -18,6 +15,8 @@ import {
|
||||
formatDuration,
|
||||
formatFileSize,
|
||||
} from "../utils/format"
|
||||
import { useRowProgress } from "./voice-material-row/useRowProgress"
|
||||
import TagDisplay from "./voice-material-row/TagDisplay"
|
||||
|
||||
export interface VoiceRowProps {
|
||||
material: VoiceMaterial
|
||||
@@ -48,26 +47,10 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
onDelete,
|
||||
onToggleSelect,
|
||||
}) => {
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleProgressMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * material.duration)
|
||||
}
|
||||
doSeek(e.nativeEvent)
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
}
|
||||
const { progressRef, handleMouseDown } = useRowProgress({
|
||||
duration: material.duration,
|
||||
onSeek,
|
||||
})
|
||||
|
||||
const progress = material.duration > 0 ? (currentTime / material.duration) * 100 : 0
|
||||
|
||||
@@ -75,7 +58,6 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
<div
|
||||
className={`vmat-row ${genderClass(material.gender)}${isPlaying ? " playing" : ""}${isSelected ? " selected" : ""}${batchMode ? " batch-mode" : ""}`}
|
||||
>
|
||||
{/* 批量选择 checkbox */}
|
||||
{(batchMode || isSelected) && (
|
||||
<div
|
||||
className={`vmat-row-checkbox vmat-checkbox${isSelected ? " checked" : ""}`}
|
||||
@@ -88,7 +70,6 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 播放按钮 */}
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-row-play"
|
||||
@@ -101,60 +82,31 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
|
||||
{/* 名称 + 描述 */}
|
||||
<div className="vmat-row-info">
|
||||
<h4 className="vmat-row-name">{material.name}</h4>
|
||||
{material.description && <p className="vmat-row-desc">{material.description}</p>}
|
||||
</div>
|
||||
|
||||
{/* 性别 */}
|
||||
<span className={`vmat-row-gender ${genderClass(material.gender)}`}>
|
||||
{genderIcon(material.gender)}
|
||||
{genderLabel(material.gender)}
|
||||
</span>
|
||||
|
||||
{/* 标签 */}
|
||||
<div className="vmat-row-tags">
|
||||
{material.tagIds.length === 0 ? (
|
||||
<span className="vmat-tag-empty" onClick={() => onEdit()}>
|
||||
添加标签
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
{material.tagIds.slice(0, MAX_ROW_TAGS).map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{material.tagIds.length > MAX_ROW_TAGS && (
|
||||
<Tooltip
|
||||
title={material.tagIds
|
||||
.slice(MAX_ROW_TAGS)
|
||||
.map((id) => tagMap.get(id)?.name ?? id)
|
||||
.join("、")}
|
||||
>
|
||||
<Tag className="vmat-tag-overflow">+{material.tagIds.length - MAX_ROW_TAGS}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
<TagDisplay tagIds={material.tagIds} tagMap={tagMap} onAddTag={() => onEdit()} />
|
||||
</div>
|
||||
|
||||
{/* 进度条(可拖拽) */}
|
||||
<div ref={progressRef} className="vmat-row-progress" onMouseDown={handleProgressMouseDown}>
|
||||
<div ref={progressRef} className="vmat-row-progress" onMouseDown={handleMouseDown}>
|
||||
<div className="vmat-row-progress-bar" style={{ width: `${progress}%` }} />
|
||||
{isPlaying && <div className="vmat-progress-thumb" style={{ left: `${progress}%` }} />}
|
||||
</div>
|
||||
|
||||
{/* 时长 */}
|
||||
<span className="vmat-row-time">
|
||||
{isPlaying ? formatDuration(currentTime) : formatDuration(material.duration)}
|
||||
</span>
|
||||
|
||||
{/* 文件大小 */}
|
||||
<span className="vmat-row-size">{formatFileSize(material.fileSize)}</span>
|
||||
|
||||
{/* 操作 */}
|
||||
<div className="vmat-row-actions">
|
||||
<button
|
||||
type="button"
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
import React, { useRef } from "react"
|
||||
import { UploadOutlined, SoundOutlined, CloseOutlined } from "@ant-design/icons"
|
||||
import { formatFileSize } from "../../utils/format"
|
||||
|
||||
interface FileUploadFieldProps {
|
||||
file: File | undefined
|
||||
onChange: (file: File | undefined) => void
|
||||
uploadProgress?: number | null
|
||||
}
|
||||
|
||||
const FileUploadField: React.FC<FileUploadFieldProps> = ({ file, onChange, uploadProgress }) => {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const handleDrop = (e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
const f = e.dataTransfer.files[0]
|
||||
if (f?.type.startsWith("audio/")) onChange(f)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">音频文件 *</label>
|
||||
<div
|
||||
className="vmat-upload-zone"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
onDragOver={(e) => e.preventDefault()}
|
||||
onDrop={handleDrop}
|
||||
>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="audio/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0]
|
||||
if (f) onChange(f)
|
||||
}}
|
||||
/>
|
||||
{file ? (
|
||||
<div className="vmat-upload-selected">
|
||||
<SoundOutlined className="vmat-upload-icon" />
|
||||
<span className="vmat-upload-filename">{file.name}</span>
|
||||
<span className="vmat-upload-filesize">{formatFileSize(file.size)}</span>
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-upload-clear"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onChange(undefined)
|
||||
}}
|
||||
>
|
||||
<CloseOutlined />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="vmat-upload-placeholder">
|
||||
<UploadOutlined className="vmat-upload-icon" />
|
||||
<p>点击或拖拽音频文件到此处</p>
|
||||
<span>支持 MP3、WAV、AAC、FLAC 等格式</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{uploadProgress !== null && uploadProgress !== undefined && (
|
||||
<div className="vmat-upload-progress">
|
||||
<div className="vmat-upload-progress-bar" style={{ width: `${uploadProgress}%` }} />
|
||||
<span className="vmat-upload-progress-text">{uploadProgress}%</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default FileUploadField
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import React from "react"
|
||||
import type { VoiceGender } from "../../types"
|
||||
import { GENDER_OPTIONS } from "../../constants"
|
||||
import { genderClass } from "../../utils/format"
|
||||
|
||||
interface GenderSelectorProps {
|
||||
value: VoiceGender
|
||||
onChange: (value: VoiceGender) => void
|
||||
}
|
||||
|
||||
const GenderSelector: React.FC<GenderSelectorProps> = ({ value, onChange }) => {
|
||||
return (
|
||||
<div className="vmat-form-field">
|
||||
<label className="vmat-form-label">性别</label>
|
||||
<div className="vmat-gender-group">
|
||||
{GENDER_OPTIONS.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
className={`vmat-gender-btn${value === opt.value ? " active" : ""} ${genderClass(opt.value)}`}
|
||||
onClick={() => onChange(opt.value)}
|
||||
>
|
||||
{opt.icon}
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default GenderSelector
|
||||
@@ -0,0 +1,90 @@
|
||||
import { useState, useRef, useCallback, useMemo } from "react"
|
||||
import type { TagItem } from "@/api/tags"
|
||||
|
||||
interface UseTagInputOptions {
|
||||
value: string[]
|
||||
onChange: (tagIds: string[]) => void
|
||||
tags: TagItem[]
|
||||
onCreateTag: (name: string) => Promise<TagItem>
|
||||
}
|
||||
|
||||
export function useTagInput({ value, onChange, tags, onCreateTag }: UseTagInputOptions) {
|
||||
const [inputVal, setInputVal] = useState("")
|
||||
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const findTagByName = useCallback(
|
||||
(name: string) => tags.find((t) => t.name.toLowerCase() === name.toLowerCase()),
|
||||
[tags],
|
||||
)
|
||||
|
||||
const addTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
if (value.includes(tagId)) return
|
||||
onChange([...value, tagId])
|
||||
setInputVal("")
|
||||
setShowSuggestions(false)
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
const addTagByName = useCallback(
|
||||
async (name: string) => {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return
|
||||
const existing = findTagByName(trimmed)
|
||||
if (existing) {
|
||||
addTagId(existing.id)
|
||||
} else {
|
||||
try {
|
||||
const created = await onCreateTag(trimmed)
|
||||
addTagId(created.id)
|
||||
} catch {
|
||||
/* 创建失败静默忽略 */
|
||||
}
|
||||
}
|
||||
},
|
||||
[findTagByName, addTagId, onCreateTag],
|
||||
)
|
||||
|
||||
const removeTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
onChange(value.filter((t) => t !== tagId))
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
const suggestions = useMemo(() => {
|
||||
if (!inputVal.trim()) return []
|
||||
const lower = inputVal.toLowerCase()
|
||||
return tags.filter((t) => t.name.toLowerCase().includes(lower) && !value.includes(t.id))
|
||||
}, [inputVal, tags, value])
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault()
|
||||
if (suggestions.length > 0) {
|
||||
addTagId(suggestions[0].id)
|
||||
} else {
|
||||
addTagByName(inputVal)
|
||||
}
|
||||
} else if (e.key === "Backspace" && !inputVal && value.length > 0) {
|
||||
removeTagId(value[value.length - 1])
|
||||
}
|
||||
}
|
||||
|
||||
const focus = () => inputRef.current?.focus()
|
||||
|
||||
return {
|
||||
inputVal,
|
||||
setInputVal,
|
||||
showSuggestions,
|
||||
setShowSuggestions,
|
||||
inputRef,
|
||||
suggestions,
|
||||
addTagId,
|
||||
removeTagId,
|
||||
handleKeyDown,
|
||||
focus,
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
import React from "react"
|
||||
import { CheckOutlined } from "@ant-design/icons"
|
||||
|
||||
interface BatchCheckboxProps {
|
||||
isSelected: boolean
|
||||
visible: boolean
|
||||
onToggle: () => void
|
||||
}
|
||||
|
||||
/** 批量选择 checkbox */
|
||||
const BatchCheckbox: React.FC<BatchCheckboxProps> = ({ isSelected, visible, onToggle }) => {
|
||||
if (!visible) return null
|
||||
return (
|
||||
<div
|
||||
className={`vmat-card-checkbox vmat-checkbox${isSelected ? " checked" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggle()
|
||||
}}
|
||||
>
|
||||
{isSelected && <CheckOutlined />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default BatchCheckbox
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import React from "react"
|
||||
import { EditOutlined, DeleteOutlined } from "@ant-design/icons"
|
||||
|
||||
interface CardActionsProps {
|
||||
onEdit: () => void
|
||||
onDelete: () => void
|
||||
}
|
||||
|
||||
/** 卡片操作按钮:编辑 / 删除 */
|
||||
const CardActions: React.FC<CardActionsProps> = ({ onEdit, onDelete }) => {
|
||||
return (
|
||||
<div className="vmat-card-actions">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-card-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onEdit()
|
||||
}}
|
||||
title="编辑"
|
||||
>
|
||||
<EditOutlined />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-card-action-btn vmat-card-action-btn--danger"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onDelete()
|
||||
}}
|
||||
title="删除"
|
||||
>
|
||||
<DeleteOutlined />
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardActions
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import React from "react"
|
||||
import { AudioOutlined } from "@ant-design/icons"
|
||||
import { type VoiceMaterial } from "../../types"
|
||||
import { genderClass, genderIcon, genderLabel } from "../../utils/format"
|
||||
|
||||
interface CardHeaderProps {
|
||||
material: VoiceMaterial
|
||||
}
|
||||
|
||||
/** 卡片头部:头像 + 名称 + 性别标签 */
|
||||
const CardHeader: React.FC<CardHeaderProps> = ({ material }) => {
|
||||
return (
|
||||
<div className="vmat-card-header">
|
||||
<div className="vmat-card-avatar">
|
||||
<AudioOutlined />
|
||||
</div>
|
||||
<div className="vmat-card-title-area">
|
||||
<h4 className="vmat-card-name" title={material.name}>
|
||||
{material.name}
|
||||
</h4>
|
||||
<span className={`vmat-card-gender ${genderClass(material.gender)}`}>
|
||||
{genderIcon(material.gender)}
|
||||
{genderLabel(material.gender)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardHeader
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import React from "react"
|
||||
import { formatDuration, formatFileSize, formatDate } from "../../utils/format"
|
||||
|
||||
interface CardMetaProps {
|
||||
duration: number
|
||||
fileSize: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
/** 元信息:时长 / 文件大小 / 创建日期 */
|
||||
const CardMeta: React.FC<CardMetaProps> = ({ duration, fileSize, createdAt }) => {
|
||||
return (
|
||||
<div className="vmat-card-meta">
|
||||
<span>{formatDuration(duration)}</span>
|
||||
<span>{formatFileSize(fileSize)}</span>
|
||||
<span>{formatDate(createdAt)}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardMeta
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
import React, { useRef } from "react"
|
||||
import {
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
SoundOutlined,
|
||||
MutedOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { formatDuration } from "../../utils/format"
|
||||
|
||||
interface CardPlayerProps {
|
||||
isPlaying: boolean
|
||||
currentTime: number
|
||||
duration: number
|
||||
volume: number
|
||||
fileUrl?: string
|
||||
onPlay: () => void
|
||||
onPause: () => void
|
||||
onSeek: (time: number) => void
|
||||
onVolumeChange: (e: React.ChangeEvent<HTMLInputElement>) => void
|
||||
onToggleMute: () => void
|
||||
}
|
||||
|
||||
/** 播放控制区:播放按钮 + 进度条 + 时间 + 音量 */
|
||||
const CardPlayer: React.FC<CardPlayerProps> = ({
|
||||
isPlaying,
|
||||
currentTime,
|
||||
duration,
|
||||
volume,
|
||||
fileUrl,
|
||||
onPlay,
|
||||
onPause,
|
||||
onSeek,
|
||||
onVolumeChange,
|
||||
onToggleMute,
|
||||
}) => {
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleProgressMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * duration)
|
||||
}
|
||||
doSeek(e.nativeEvent)
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
}
|
||||
|
||||
const progress = duration > 0 ? (currentTime / duration) * 100 : 0
|
||||
|
||||
return (
|
||||
<div className="vmat-card-player">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
disabled={!fileUrl}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div ref={progressRef} className="vmat-progress" onMouseDown={handleProgressMouseDown}>
|
||||
<div className="vmat-progress-bar" style={{ width: `${progress}%` }} />
|
||||
{isPlaying && <div className="vmat-progress-thumb" style={{ left: `${progress}%` }} />}
|
||||
</div>
|
||||
<span className="vmat-time">
|
||||
{isPlaying ? formatDuration(currentTime) : formatDuration(duration)}
|
||||
</span>
|
||||
{/* 音量控制 */}
|
||||
<div className="vmat-volume">
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-volume-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onToggleMute()
|
||||
}}
|
||||
title={volume === 0 ? "取消静音" : "静音"}
|
||||
>
|
||||
{volume === 0 ? <MutedOutlined /> : <SoundOutlined />}
|
||||
</button>
|
||||
<input
|
||||
type="range"
|
||||
className="vmat-volume-slider"
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.05}
|
||||
value={volume}
|
||||
onChange={(e) => {
|
||||
e.stopPropagation()
|
||||
onVolumeChange(e)
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardPlayer
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import React from "react"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { MAX_CARD_TAGS, TAG_VARIANTS } from "../../constants"
|
||||
|
||||
interface CardTagsProps {
|
||||
tagIds: string[]
|
||||
tagMap: Map<string, TagItem>
|
||||
onEdit: () => void
|
||||
}
|
||||
|
||||
/** 标签展示区 */
|
||||
const CardTags: React.FC<CardTagsProps> = ({ tagIds, tagMap, onEdit }) => {
|
||||
if (tagIds.length === 0) {
|
||||
return (
|
||||
<div className="vmat-card-tags">
|
||||
<span
|
||||
className="vmat-tag-empty"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onEdit()
|
||||
}}
|
||||
>
|
||||
添加标签
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="vmat-card-tags">
|
||||
{tagIds.slice(0, MAX_CARD_TAGS).map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{tagIds.length > MAX_CARD_TAGS && (
|
||||
<Tooltip
|
||||
title={tagIds
|
||||
.slice(MAX_CARD_TAGS)
|
||||
.map((id) => tagMap.get(id)?.name ?? id)
|
||||
.join("、")}
|
||||
>
|
||||
<Tag className="vmat-tag-overflow">+{tagIds.length - MAX_CARD_TAGS}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardTags
|
||||
@@ -0,0 +1,8 @@
|
||||
export { default } from "../VoiceMaterialCard"
|
||||
export * from "./types"
|
||||
export { default as CardHeader } from "./CardHeader"
|
||||
export { default as CardTags } from "./CardTags"
|
||||
export { default as CardMeta } from "./CardMeta"
|
||||
export { default as CardPlayer } from "./CardPlayer"
|
||||
export { default as CardActions } from "./CardActions"
|
||||
export { default as BatchCheckbox } from "./BatchCheckbox"
|
||||
@@ -0,0 +1,21 @@
|
||||
import React from "react"
|
||||
import { type VoiceMaterial } from "../../types"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
|
||||
export interface VoiceCardProps {
|
||||
material: VoiceMaterial
|
||||
isPlaying: boolean
|
||||
currentTime: number
|
||||
isSelected: boolean
|
||||
batchMode: boolean
|
||||
volume: number
|
||||
tagMap: Map<string, TagItem>
|
||||
onPlay: () => void
|
||||
onPause: () => void
|
||||
onSeek: (time: number) => void
|
||||
onEdit: () => void
|
||||
onDelete: () => void
|
||||
onToggleSelect: (id: string) => void
|
||||
onVolumeChange: (e: React.ChangeEvent<HTMLInputElement>) => void
|
||||
onToggleMute: () => void
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
import React from "react"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { MAX_ROW_TAGS, TAG_VARIANTS } from "../../constants"
|
||||
|
||||
interface TagDisplayProps {
|
||||
tagIds: string[]
|
||||
tagMap: Map<string, TagItem>
|
||||
onAddTag?: () => void
|
||||
}
|
||||
|
||||
const TagDisplay: React.FC<TagDisplayProps> = ({ tagIds, tagMap, onAddTag }) => {
|
||||
if (tagIds.length === 0) {
|
||||
return (
|
||||
<span className="vmat-tag-empty" onClick={onAddTag}>
|
||||
添加标签
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const visible = tagIds.slice(0, MAX_ROW_TAGS)
|
||||
const overflow = tagIds.slice(MAX_ROW_TAGS)
|
||||
|
||||
return (
|
||||
<>
|
||||
{visible.map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{overflow.length > 0 && (
|
||||
<Tooltip title={overflow.map((id) => tagMap.get(id)?.name ?? id).join("、")}>
|
||||
<Tag className="vmat-tag-overflow">+{overflow.length}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default TagDisplay
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import { useRef, useCallback } from "react"
|
||||
|
||||
interface UseRowProgressOptions {
|
||||
duration: number
|
||||
onSeek: (time: number) => void
|
||||
}
|
||||
|
||||
export function useRowProgress({ duration, onSeek }: UseRowProgressOptions) {
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleMouseDown = useCallback(
|
||||
(e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * duration)
|
||||
}
|
||||
|
||||
doSeek(e.nativeEvent)
|
||||
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
},
|
||||
[duration, onSeek],
|
||||
)
|
||||
|
||||
return { progressRef, handleMouseDown }
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import { useCallback } from "react"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { deleteAsset } from "@/api/assets"
|
||||
import { type VoiceMaterial } from "../../../types"
|
||||
|
||||
interface UseVoiceDeleteOptions {
|
||||
materials: VoiceMaterial[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 配音素材删除 Hook
|
||||
*/
|
||||
export function useVoiceDelete({ materials }: UseVoiceDeleteOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (assetId: string) => deleteAsset(assetId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
},
|
||||
})
|
||||
|
||||
const handleDelete = useCallback(
|
||||
(id: string, onBeforeDelete?: () => void) => {
|
||||
const material = materials.find((m) => m.id === id)
|
||||
if (!material) return
|
||||
if (onBeforeDelete) onBeforeDelete()
|
||||
deleteMutation.mutate(id)
|
||||
},
|
||||
[materials, deleteMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
isDeleting: deleteMutation.isPending,
|
||||
deleteMutation,
|
||||
handleDelete,
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
import { useCallback } from "react"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { updateAsset } from "@/api/assets"
|
||||
import { tagAsset, untagAsset } from "@/api/tags"
|
||||
import { type VoiceGender, type VoiceMaterial, buildMetadata } from "../../../types"
|
||||
|
||||
interface UseVoiceEditOptions {
|
||||
materials: VoiceMaterial[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 配音素材编辑 Hook
|
||||
* 封装编辑流程:更新基础信息 + 同步标签差异
|
||||
*/
|
||||
export function useVoiceEdit({ materials }: UseVoiceEditOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const editMutation = useMutation({
|
||||
mutationFn: async (data: {
|
||||
id: string
|
||||
name: string
|
||||
gender: VoiceGender
|
||||
description: string
|
||||
tagIds: string[]
|
||||
}) => {
|
||||
// 1. 更新基础信息
|
||||
await updateAsset(data.id, {
|
||||
name: data.name,
|
||||
metadata: buildMetadata({
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
}),
|
||||
})
|
||||
|
||||
// 2. 对比标签差异,调用 tag/untag API
|
||||
const currentAsset = materials.find((m) => m.id === data.id)
|
||||
const oldTagIds = currentAsset?.tagIds ?? []
|
||||
const newTagIds = data.tagIds
|
||||
|
||||
const toAdd = newTagIds.filter((id) => !oldTagIds.includes(id))
|
||||
const toRemove = oldTagIds.filter((id) => !newTagIds.includes(id))
|
||||
|
||||
if (toAdd.length > 0) {
|
||||
await tagAsset(data.id, toAdd)
|
||||
}
|
||||
for (const tagId of toRemove) {
|
||||
await untagAsset(data.id, tagId)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
queryClient.invalidateQueries({ queryKey: ["tags"] })
|
||||
},
|
||||
})
|
||||
|
||||
const handleEdit = useCallback(
|
||||
(
|
||||
editingMaterial: VoiceMaterial | null,
|
||||
data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File },
|
||||
) => {
|
||||
if (!editingMaterial) return
|
||||
editMutation.mutate({
|
||||
id: editingMaterial.id,
|
||||
name: data.name,
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
tagIds: data.tagIds,
|
||||
})
|
||||
},
|
||||
[editMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
isEditing: editMutation.isPending,
|
||||
editMutation,
|
||||
handleEdit,
|
||||
}
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
import { useState, useCallback } from "react"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import {
|
||||
createAsset,
|
||||
uploadAssetDirect,
|
||||
getAssetLibraries,
|
||||
type AssetLibraryItem,
|
||||
} from "@/api/assets"
|
||||
import { tagAsset } from "@/api/tags"
|
||||
import { type VoiceGender, type VoiceMaterial, buildMetadata } from "../../../types"
|
||||
import { getAudioDuration } from "../../../utils/audio"
|
||||
|
||||
interface UseVoiceUploadOptions {
|
||||
voiceLibrary?: { id: string; kind: string }
|
||||
createLibMutation: { mutateAsync: () => Promise<AssetLibraryItem>; isPending: boolean }
|
||||
}
|
||||
|
||||
/**
|
||||
* 配音素材上传 Hook
|
||||
* 封装上传流程:获取库 → 上传文件 → 获取时长 → 创建记录 → 打标签
|
||||
*/
|
||||
export function useVoiceUpload({ voiceLibrary, createLibMutation }: UseVoiceUploadOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
const [uploadProgress, setUploadProgress] = useState<number | null>(null)
|
||||
|
||||
const uploadMutation = useMutation({
|
||||
mutationFn: async (data: {
|
||||
file: File
|
||||
name: string
|
||||
gender: VoiceGender
|
||||
description: string
|
||||
tagIds: string[]
|
||||
}) => {
|
||||
setUploadProgress(0)
|
||||
try {
|
||||
// 1. 获取或等待 voice library
|
||||
let lib = voiceLibrary
|
||||
if (!lib) {
|
||||
if (createLibMutation.isPending) {
|
||||
await createLibMutation.mutateAsync()
|
||||
}
|
||||
const libs = await queryClient.fetchQuery({
|
||||
queryKey: ["asset-libraries"],
|
||||
queryFn: getAssetLibraries,
|
||||
})
|
||||
lib = libs.find((l: AssetLibraryItem) => l.kind === "voice")
|
||||
if (!lib) throw new Error("无法创建配音库")
|
||||
}
|
||||
|
||||
// 2. 上传文件(带进度)
|
||||
const { storage_key } = await uploadAssetDirect({
|
||||
file: data.file,
|
||||
library_id: lib.id,
|
||||
onProgress: (p) => setUploadProgress(p),
|
||||
})
|
||||
|
||||
// 3. 获取音频时长
|
||||
const duration = await getAudioDuration(data.file)
|
||||
|
||||
// 4. 创建素材记录
|
||||
const asset = await createAsset({
|
||||
library_id: lib.id,
|
||||
name: data.name,
|
||||
storage_key,
|
||||
mime_type: data.file.type || "audio/mpeg",
|
||||
metadata: buildMetadata({
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
duration,
|
||||
}),
|
||||
})
|
||||
|
||||
// 5. 打标签(标签走独立 API)
|
||||
if (data.tagIds.length > 0) {
|
||||
await tagAsset(asset.id, data.tagIds)
|
||||
}
|
||||
} finally {
|
||||
setUploadProgress(null)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
queryClient.invalidateQueries({ queryKey: ["tags"] })
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
message.error(err.message || "上传失败,请重试")
|
||||
},
|
||||
})
|
||||
|
||||
const handleUpload = useCallback(
|
||||
(data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File }) => {
|
||||
if (!data.file) return
|
||||
uploadMutation.mutate({
|
||||
file: data.file,
|
||||
name: data.name,
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
tagIds: data.tagIds,
|
||||
})
|
||||
},
|
||||
[uploadMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
uploadProgress,
|
||||
isUploading: uploadMutation.isPending,
|
||||
uploadMutation,
|
||||
handleUpload,
|
||||
}
|
||||
}
|
||||
Regular → Executable
+38
-174
@@ -1,17 +1,9 @@
|
||||
import { useState, useCallback } from "react"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import {
|
||||
createAsset,
|
||||
updateAsset,
|
||||
deleteAsset,
|
||||
uploadAssetDirect,
|
||||
getAssetLibraries,
|
||||
type AssetLibraryItem,
|
||||
} from "@/api/assets"
|
||||
import { tagAsset, untagAsset } from "@/api/tags"
|
||||
import { type VoiceGender, type VoiceMaterial, buildMetadata } from "../../types"
|
||||
import { getAudioDuration } from "../../utils/audio"
|
||||
import { useState } from "react"
|
||||
import { type VoiceMaterial } from "../../types"
|
||||
import { type AssetLibraryItem } from "@/api/assets"
|
||||
import { useVoiceUpload } from "./actions/useVoiceUpload"
|
||||
import { useVoiceEdit } from "./actions/useVoiceEdit"
|
||||
import { useVoiceDelete } from "./actions/useVoiceDelete"
|
||||
|
||||
interface UseVoiceMaterialActionsOptions {
|
||||
voiceLibrary?: { id: string; kind: string }
|
||||
@@ -21,184 +13,56 @@ interface UseVoiceMaterialActionsOptions {
|
||||
|
||||
/**
|
||||
* 配音素材操作 Hook
|
||||
* 封装上传、编辑、删除等变更操作及相关 UI 状态
|
||||
* 组合上传、编辑、删除三个子 Hook,统一管理弹窗状态
|
||||
*/
|
||||
export function useVoiceMaterialActions({
|
||||
voiceLibrary,
|
||||
materials,
|
||||
createLibMutation,
|
||||
}: UseVoiceMaterialActionsOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
// ── 弹窗状态 ──────────────────────────────────────────────
|
||||
// ── 弹窗状态 ──
|
||||
const [uploadOpen, setUploadOpen] = useState(false)
|
||||
const [editingMaterial, setEditingMaterial] = useState<VoiceMaterial | null>(null)
|
||||
|
||||
// ── 上传进度 ──────────────────────────────────────────────
|
||||
const [uploadProgress, setUploadProgress] = useState<number | null>(null)
|
||||
// ── 子领域 Hooks ──
|
||||
const { uploadProgress, uploadMutation } = useVoiceUpload({ voiceLibrary, createLibMutation })
|
||||
const { editMutation } = useVoiceEdit({ materials })
|
||||
const { handleDelete } = useVoiceDelete({ materials })
|
||||
|
||||
// ── 上传 mutation ─────────────────────────────────────────
|
||||
const uploadMutation = useMutation({
|
||||
mutationFn: async (data: {
|
||||
file: File
|
||||
name: string
|
||||
gender: VoiceGender
|
||||
description: string
|
||||
tagIds: string[]
|
||||
}) => {
|
||||
setUploadProgress(0)
|
||||
try {
|
||||
// 1. 获取或等待 voice library
|
||||
let lib = voiceLibrary
|
||||
if (!lib) {
|
||||
if (createLibMutation.isPending) {
|
||||
await createLibMutation.mutateAsync()
|
||||
}
|
||||
const libs = await queryClient.fetchQuery({
|
||||
queryKey: ["asset-libraries"],
|
||||
queryFn: getAssetLibraries,
|
||||
})
|
||||
lib = libs.find((l: AssetLibraryItem) => l.kind === "voice")
|
||||
if (!lib) throw new Error("无法创建配音库")
|
||||
}
|
||||
/* ── 操作 handlers(关联弹窗状态) ── */
|
||||
|
||||
// 2. 上传文件(带进度)
|
||||
const { storage_key } = await uploadAssetDirect({
|
||||
file: data.file,
|
||||
library_id: lib.id,
|
||||
onProgress: (p) => setUploadProgress(p),
|
||||
})
|
||||
|
||||
// 3. 获取音频时长
|
||||
const duration = await getAudioDuration(data.file)
|
||||
|
||||
// 4. 创建素材记录
|
||||
const asset = await createAsset({
|
||||
library_id: lib.id,
|
||||
name: data.name,
|
||||
storage_key,
|
||||
mime_type: data.file.type || "audio/mpeg",
|
||||
metadata: buildMetadata({
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
duration,
|
||||
}),
|
||||
})
|
||||
|
||||
// 5. 打标签(标签走独立 API)
|
||||
if (data.tagIds.length > 0) {
|
||||
await tagAsset(asset.id, data.tagIds)
|
||||
}
|
||||
} finally {
|
||||
setUploadProgress(null)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
queryClient.invalidateQueries({ queryKey: ["tags"] })
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
message.error(err.message || "上传失败,请重试")
|
||||
},
|
||||
})
|
||||
|
||||
// ── 编辑 mutation ─────────────────────────────────────────
|
||||
const editMutation = useMutation({
|
||||
mutationFn: async (data: {
|
||||
id: string
|
||||
name: string
|
||||
gender: VoiceGender
|
||||
description: string
|
||||
tagIds: string[]
|
||||
}) => {
|
||||
// 1. 更新基础信息
|
||||
await updateAsset(data.id, {
|
||||
name: data.name,
|
||||
metadata: buildMetadata({
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
}),
|
||||
})
|
||||
|
||||
// 2. 对比标签差异,调用 tag/untag API
|
||||
const currentAsset = materials.find((m) => m.id === data.id)
|
||||
const oldTagIds = currentAsset?.tagIds ?? []
|
||||
const newTagIds = data.tagIds
|
||||
|
||||
const toAdd = newTagIds.filter((id) => !oldTagIds.includes(id))
|
||||
const toRemove = oldTagIds.filter((id) => !newTagIds.includes(id))
|
||||
|
||||
if (toAdd.length > 0) {
|
||||
await tagAsset(data.id, toAdd)
|
||||
}
|
||||
for (const tagId of toRemove) {
|
||||
await untagAsset(data.id, tagId)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
queryClient.invalidateQueries({ queryKey: ["tags"] })
|
||||
},
|
||||
})
|
||||
|
||||
// ── 删除 mutation ─────────────────────────────────────────
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (assetId: string) => deleteAsset(assetId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["assets", "voice"] })
|
||||
},
|
||||
})
|
||||
|
||||
/* ── 数据操作 handlers ──────────────────────────────────── */
|
||||
|
||||
const handleUpload = useCallback(
|
||||
(data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File }) => {
|
||||
if (!data.file) return
|
||||
uploadMutation.mutate(
|
||||
{
|
||||
file: data.file,
|
||||
name: data.name,
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
tagIds: data.tagIds,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
setUploadOpen(false)
|
||||
},
|
||||
},
|
||||
)
|
||||
},
|
||||
[uploadMutation],
|
||||
)
|
||||
|
||||
const handleEdit = useCallback(
|
||||
(data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File }) => {
|
||||
if (!editingMaterial) return
|
||||
editMutation.mutate({
|
||||
id: editingMaterial.id,
|
||||
const handleUpload = (data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File }) => {
|
||||
if (!data.file) return
|
||||
uploadMutation.mutate(
|
||||
{
|
||||
file: data.file,
|
||||
name: data.name,
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
tagIds: data.tagIds,
|
||||
})
|
||||
setEditingMaterial(null)
|
||||
},
|
||||
[editingMaterial, editMutation],
|
||||
)
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
setUploadOpen(false)
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
const handleDelete = useCallback(
|
||||
(id: string, onBeforeDelete?: () => void) => {
|
||||
const material = materials.find((m) => m.id === id)
|
||||
if (!material) return
|
||||
if (onBeforeDelete) onBeforeDelete()
|
||||
deleteMutation.mutate(id)
|
||||
},
|
||||
[materials, deleteMutation],
|
||||
)
|
||||
const handleEdit = (data: Omit<VoiceMaterial, "id" | "createdAt"> & { file?: File }) => {
|
||||
if (!editingMaterial) return
|
||||
editMutation.mutate({
|
||||
id: editingMaterial.id,
|
||||
name: data.name,
|
||||
gender: data.gender,
|
||||
description: data.description,
|
||||
tagIds: data.tagIds,
|
||||
})
|
||||
setEditingMaterial(null)
|
||||
}
|
||||
|
||||
return {
|
||||
// 上传 & 编辑状态
|
||||
// 上传 & 编辑 loading 状态
|
||||
uploadProgress,
|
||||
isUploading: uploadMutation.isPending,
|
||||
isEditing: editMutation.isPending,
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
import React from "react"
|
||||
import {
|
||||
SoundOutlined,
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
DeleteOutlined,
|
||||
ReloadOutlined,
|
||||
CloseCircleOutlined,
|
||||
UserOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { DeleteOutlined, ReloadOutlined, CloseCircleOutlined } from "@ant-design/icons"
|
||||
import { Tooltip } from "antd"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
import { CLONE_STATUS_CONFIG } from "@/pages/voices/constants"
|
||||
import CardHeader from "./clone-voice-card/CardHeader"
|
||||
import CardFooter from "./clone-voice-card/CardFooter"
|
||||
|
||||
export interface CloneVoiceCardProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
@@ -28,26 +21,18 @@ export interface CloneVoiceCardProps {
|
||||
const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
voice,
|
||||
isPlaying,
|
||||
currentTime,
|
||||
onPlay,
|
||||
onPause,
|
||||
onUse,
|
||||
onDelete,
|
||||
onRetry,
|
||||
onShowDetail,
|
||||
...footerProps
|
||||
}) => {
|
||||
const statusCfg = CLONE_STATUS_CONFIG[voice.status]
|
||||
const isFailed = voice.status === "failed"
|
||||
const isProcessing = voice.status === "processing"
|
||||
const genderText =
|
||||
voice.gender === "male" ? "男声" : voice.gender === "female" ? "女声" : voice.gender
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`xx-clone-card${isPlaying ? " playing" : ""}${isFailed ? " failed" : ""}`}
|
||||
onClick={isFailed ? undefined : onShowDetail}
|
||||
>
|
||||
{/* 右上角操作按钮 */}
|
||||
<div className="xx-clone-card-actions">
|
||||
<Tooltip title="删除">
|
||||
<button
|
||||
@@ -77,38 +62,8 @@ const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 头部:头像 + 名称 + 状态 */}
|
||||
<div className="xx-clone-card-header">
|
||||
<div className={`xx-clone-avatar${isProcessing ? " xx-clone-avatar--processing" : ""}`}>
|
||||
<SoundOutlined />
|
||||
</div>
|
||||
<div className="xx-clone-header-info">
|
||||
<h4 className="xx-clone-name" title={voice.name}>
|
||||
{voice.name}
|
||||
</h4>
|
||||
<span className={`xx-clone-status ${statusCfg.className}`}>
|
||||
<span className="xx-clone-status-dot" />
|
||||
{statusCfg.label}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<CardHeader voice={voice} />
|
||||
|
||||
{/* 描述 */}
|
||||
{voice.description && <p className="xx-clone-desc">{voice.description}</p>}
|
||||
|
||||
{/* 元信息 */}
|
||||
<div className="xx-clone-meta">
|
||||
{(voice.gender || voice.language) && (
|
||||
<span className="xx-clone-meta-item">
|
||||
<UserOutlined />
|
||||
{genderText}
|
||||
{voice.language ? ` · ${voice.language}` : ""}
|
||||
</span>
|
||||
)}
|
||||
<span className="xx-clone-meta-item">{voice.createdAt}</span>
|
||||
</div>
|
||||
|
||||
{/* 错误信息 */}
|
||||
{isFailed && voice.errorMessage && (
|
||||
<div className="xx-clone-error">
|
||||
<CloseCircleOutlined />
|
||||
@@ -116,63 +71,7 @@ const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 底部操作区 */}
|
||||
<div className="xx-clone-footer">
|
||||
{voice.status === "ready" && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
title={isPlaying ? "暂停" : "试听"}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div className="xx-clone-progress">
|
||||
<div
|
||||
className="xx-clone-progress-bar"
|
||||
style={{
|
||||
width: isPlaying
|
||||
? `${Math.min((currentTime / Math.max(voice.duration, 1)) * 100, 100)}%`
|
||||
: "0%",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-use-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onUse()
|
||||
}}
|
||||
>
|
||||
使用
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{isProcessing && (
|
||||
<div className="xx-clone-processing-hint">
|
||||
<ReloadOutlined spin />
|
||||
克隆处理中,请稍候...
|
||||
</div>
|
||||
)}
|
||||
{isFailed && (
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-retry-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onRetry()
|
||||
}}
|
||||
>
|
||||
<ReloadOutlined />
|
||||
重试克隆
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<CardFooter voice={voice} isPlaying={isPlaying} onRetry={onRetry} {...footerProps} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,27 +1,12 @@
|
||||
import React from "react"
|
||||
import { RobotOutlined } from "@ant-design/icons"
|
||||
import { Modal, message } from "antd"
|
||||
import { type PresetVoiceDisplay } from "@/pages/voices/types"
|
||||
import { genderLabel } from "@/pages/voices/utils/format"
|
||||
|
||||
export type TtsStatus = "idle" | "synthesizing" | "done" | "error"
|
||||
|
||||
export interface TtsModalProps {
|
||||
open: boolean
|
||||
ttsText: string
|
||||
ttsVoiceId: string
|
||||
ttsSpeed: number
|
||||
ttsStatus: TtsStatus
|
||||
ttsAudioUrl: string | null
|
||||
ttsError: string | null
|
||||
presetVoices: PresetVoiceDisplay[]
|
||||
onClose: () => void
|
||||
onTextChange: (text: string) => void
|
||||
onVoiceChange: (voiceId: string) => void
|
||||
onSpeedChange: (speed: number) => void
|
||||
onSynthesize: () => void
|
||||
onSave: () => void
|
||||
}
|
||||
import { Modal } from "antd"
|
||||
import { type TtsModalProps, type TtsStatus } from "./tts-modal/types"
|
||||
import TextInputSection from "./tts-modal/TextInputSection"
|
||||
import VoiceSelector from "./tts-modal/VoiceSelector"
|
||||
import SpeedControl from "./tts-modal/SpeedControl"
|
||||
import SynthesizeButton from "./tts-modal/SynthesizeButton"
|
||||
import ErrorAlert from "./tts-modal/ErrorAlert"
|
||||
import ResultPanel from "./tts-modal/ResultPanel"
|
||||
|
||||
/** AI 配音弹窗 */
|
||||
const TtsModal: React.FC<TtsModalProps> = ({
|
||||
@@ -50,205 +35,13 @@ const TtsModal: React.FC<TtsModalProps> = ({
|
||||
padding: "8px 0",
|
||||
}}
|
||||
>
|
||||
{/* 文本输入 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
输入文本
|
||||
</div>
|
||||
<textarea
|
||||
value={ttsText}
|
||||
onChange={(e) => onTextChange(e.target.value)}
|
||||
placeholder="输入要配音的文本内容..."
|
||||
maxLength={2000}
|
||||
rows={4}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "10px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
resize: "vertical",
|
||||
fontFamily: "inherit",
|
||||
lineHeight: 1.6,
|
||||
}}
|
||||
/>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 11,
|
||||
color: "var(--text-tertiary)",
|
||||
textAlign: "right",
|
||||
marginTop: 4,
|
||||
}}
|
||||
>
|
||||
{ttsText.length}/2000
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 音色选择 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
选择音色
|
||||
</div>
|
||||
<select
|
||||
value={ttsVoiceId}
|
||||
onChange={(e) => onVoiceChange(e.target.value)}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "8px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
}}
|
||||
>
|
||||
<option value="">默认音色</option>
|
||||
{presetVoices.map((v) => (
|
||||
<option key={v.id} value={v.id}>
|
||||
{v.name} — {genderLabel(v.gender)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* 语速 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
语速:{ttsSpeed.toFixed(1)}x
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
min={0.5}
|
||||
max={2.0}
|
||||
step={0.1}
|
||||
value={ttsSpeed}
|
||||
onChange={(e) => onSpeedChange(parseFloat(e.target.value))}
|
||||
style={{ width: "100%", accentColor: "var(--primary-color)" }}
|
||||
/>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
fontSize: 11,
|
||||
color: "var(--text-tertiary)",
|
||||
}}
|
||||
>
|
||||
<span>0.5x</span>
|
||||
<span>1.0x</span>
|
||||
<span>2.0x</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 合成按钮 */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (!ttsText.trim()) {
|
||||
message.warning("请输入要合成的文本")
|
||||
return
|
||||
}
|
||||
onSynthesize()
|
||||
}}
|
||||
disabled={ttsStatus === "synthesizing" || !ttsText.trim()}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "10px 0",
|
||||
borderRadius: 8,
|
||||
border: "none",
|
||||
background:
|
||||
ttsStatus === "synthesizing" || !ttsText.trim()
|
||||
? "var(--text-tertiary)"
|
||||
: "var(--primary-color)",
|
||||
color: "#fff",
|
||||
fontSize: 14,
|
||||
fontWeight: 600,
|
||||
cursor: ttsStatus === "synthesizing" || !ttsText.trim() ? "not-allowed" : "pointer",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
gap: 8,
|
||||
}}
|
||||
>
|
||||
<RobotOutlined />
|
||||
{ttsStatus === "synthesizing" ? "合成中..." : "开始合成"}
|
||||
</button>
|
||||
|
||||
{/* 错误提示 */}
|
||||
{ttsError && (
|
||||
<div
|
||||
style={{
|
||||
padding: "10px 12px",
|
||||
background: "var(--error-soft, #fff2f0)",
|
||||
borderRadius: 8,
|
||||
color: "var(--error-color, #ff4d4f)",
|
||||
fontSize: 13,
|
||||
}}
|
||||
>
|
||||
{ttsError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 合成结果 */}
|
||||
<TextInputSection value={ttsText} onChange={onTextChange} />
|
||||
<VoiceSelector value={ttsVoiceId} onChange={onVoiceChange} presetVoices={presetVoices} />
|
||||
<SpeedControl speed={ttsSpeed} onChange={onSpeedChange} />
|
||||
<SynthesizeButton status={ttsStatus} text={ttsText} onClick={onSynthesize} />
|
||||
{ttsError && <ErrorAlert error={ttsError} />}
|
||||
{ttsStatus === "done" && ttsAudioUrl && (
|
||||
<div
|
||||
style={{
|
||||
padding: 12,
|
||||
background: "var(--bg-secondary)",
|
||||
borderRadius: 8,
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: 10,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
fontWeight: 500,
|
||||
color: "var(--success-color, #52c41a)",
|
||||
}}
|
||||
>
|
||||
✅ 合成完成
|
||||
</div>
|
||||
<audio controls src={ttsAudioUrl} style={{ width: "100%" }} />
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSave}
|
||||
style={{
|
||||
padding: "8px 0",
|
||||
borderRadius: 8,
|
||||
border: "1px solid var(--primary-color)",
|
||||
background: "var(--primary-soft)",
|
||||
color: "var(--primary-color)",
|
||||
fontSize: 13,
|
||||
fontWeight: 600,
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
保存到配音库
|
||||
</button>
|
||||
</div>
|
||||
<ResultPanel audioUrl={ttsAudioUrl} onSave={onSave} />
|
||||
)}
|
||||
</div>
|
||||
</Modal>
|
||||
@@ -256,3 +49,5 @@ const TtsModal: React.FC<TtsModalProps> = ({
|
||||
}
|
||||
|
||||
export default TtsModal
|
||||
|
||||
export type { TtsModalProps, TtsStatus }
|
||||
|
||||
@@ -1,24 +1,13 @@
|
||||
import React from "react"
|
||||
import { UploadOutlined, SoundOutlined } from "@ant-design/icons"
|
||||
import { Modal, Upload, message } from "antd"
|
||||
import { type VoiceGender } from "@/pages/voices/types"
|
||||
import { formatFileSize } from "@/pages/voices/utils/format"
|
||||
|
||||
export interface UploadVoiceModalProps {
|
||||
open: boolean
|
||||
uploadFile: File | null
|
||||
uploadName: string
|
||||
uploadGender: VoiceGender
|
||||
uploadDesc: string
|
||||
uploadProgress: number | null
|
||||
onClose: () => void
|
||||
onFileSelect: (file: File) => void
|
||||
onFileRemove: () => void
|
||||
onNameChange: (name: string) => void
|
||||
onGenderChange: (gender: VoiceGender) => void
|
||||
onDescChange: (desc: string) => void
|
||||
onUpload: () => void
|
||||
}
|
||||
import { Modal } from "antd"
|
||||
import {
|
||||
FileUploadZone,
|
||||
FileInfoCard,
|
||||
UploadProgress,
|
||||
FormFields,
|
||||
ActionButtons,
|
||||
} from "./upload-voice-modal"
|
||||
import type { UploadVoiceModalProps } from "./upload-voice-modal"
|
||||
|
||||
/** 上传音频弹窗 */
|
||||
const UploadVoiceModal: React.FC<UploadVoiceModalProps> = ({
|
||||
@@ -36,17 +25,20 @@ const UploadVoiceModal: React.FC<UploadVoiceModalProps> = ({
|
||||
onDescChange,
|
||||
onUpload,
|
||||
}) => {
|
||||
const uploading = uploadProgress !== null
|
||||
const canUpload = !!uploadFile && !!uploadName.trim()
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title="上传音频"
|
||||
open={open}
|
||||
onCancel={() => {
|
||||
if (uploadProgress !== null) return // 上传中不可关闭
|
||||
if (uploading) return // 上传中不可关闭
|
||||
onClose()
|
||||
}}
|
||||
footer={null}
|
||||
width={520}
|
||||
maskClosable={uploadProgress === null}
|
||||
maskClosable={!uploading}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
@@ -57,270 +49,36 @@ const UploadVoiceModal: React.FC<UploadVoiceModalProps> = ({
|
||||
}}
|
||||
>
|
||||
{/* 拖拽上传区 */}
|
||||
<Upload.Dragger
|
||||
accept="audio/*"
|
||||
maxCount={1}
|
||||
beforeUpload={(file) => {
|
||||
onFileSelect(file)
|
||||
return false
|
||||
}}
|
||||
onRemove={() => {
|
||||
onFileRemove()
|
||||
}}
|
||||
showUploadList={false}
|
||||
disabled={uploadProgress !== null}
|
||||
>
|
||||
<p
|
||||
style={{
|
||||
fontSize: 32,
|
||||
color: "var(--primary-color)",
|
||||
marginBottom: 8,
|
||||
}}
|
||||
>
|
||||
<UploadOutlined />
|
||||
</p>
|
||||
<p style={{ fontSize: 14, fontWeight: 500, margin: "0 0 4px" }}>
|
||||
点击或拖拽音频文件到此处
|
||||
</p>
|
||||
<p
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: "var(--text-secondary)",
|
||||
margin: 0,
|
||||
}}
|
||||
>
|
||||
支持 MP3、WAV、AAC、FLAC 等格式,最大 200MB
|
||||
</p>
|
||||
</Upload.Dragger>
|
||||
<FileUploadZone
|
||||
disabled={uploading}
|
||||
onFileSelect={onFileSelect}
|
||||
onFileRemove={onFileRemove}
|
||||
/>
|
||||
|
||||
{/* 已选文件信息 */}
|
||||
{uploadFile && (
|
||||
<div
|
||||
style={{
|
||||
padding: "10px 12px",
|
||||
background: "var(--bg-secondary)",
|
||||
borderRadius: 8,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 10,
|
||||
}}
|
||||
>
|
||||
<SoundOutlined style={{ fontSize: 18, color: "var(--primary-color)" }} />
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
fontWeight: 500,
|
||||
overflow: "hidden",
|
||||
textOverflow: "ellipsis",
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
>
|
||||
{uploadFile.name}
|
||||
</div>
|
||||
<div style={{ fontSize: 11, color: "var(--text-secondary)" }}>
|
||||
{formatFileSize(uploadFile.size)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{uploadFile && <FileInfoCard file={uploadFile} />}
|
||||
|
||||
{/* 上传进度 */}
|
||||
{uploadProgress !== null && (
|
||||
<div style={{ textAlign: "center", padding: "8px 0" }}>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 22,
|
||||
fontWeight: 700,
|
||||
color: "var(--primary-color)",
|
||||
}}
|
||||
>
|
||||
{uploadProgress}%
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: "var(--text-secondary)" }}>
|
||||
{uploadProgress < 100 ? "上传中..." : "处理中..."}
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
height: 4,
|
||||
background: "var(--bg-tertiary)",
|
||||
borderRadius: 2,
|
||||
marginTop: 8,
|
||||
overflow: "hidden",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
height: "100%",
|
||||
width: `${uploadProgress}%`,
|
||||
background: "var(--primary-color)",
|
||||
borderRadius: 2,
|
||||
transition: "width 0.3s ease",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{uploadProgress !== null && <UploadProgress progress={uploadProgress} />}
|
||||
|
||||
{/* 名称 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
素材名称
|
||||
</div>
|
||||
<input
|
||||
value={uploadName}
|
||||
onChange={(e) => onNameChange(e.target.value)}
|
||||
placeholder="输入素材名称"
|
||||
maxLength={100}
|
||||
disabled={uploadProgress !== null}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "8px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 性别选择 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
音色性别
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: 8 }}>
|
||||
{(["female", "male", "child"] as VoiceGender[]).map((g) => (
|
||||
<button
|
||||
key={g}
|
||||
type="button"
|
||||
onClick={() => onGenderChange(g)}
|
||||
disabled={uploadProgress !== null}
|
||||
style={{
|
||||
flex: 1,
|
||||
padding: "6px 0",
|
||||
borderRadius: 8,
|
||||
border: `1px solid ${uploadGender === g ? "var(--primary-color)" : "var(--border-color)"}`,
|
||||
background: uploadGender === g ? "var(--primary-soft)" : "transparent",
|
||||
color: uploadGender === g ? "var(--primary-color)" : "var(--text-secondary)",
|
||||
fontSize: 13,
|
||||
fontWeight: uploadGender === g ? 600 : 400,
|
||||
cursor: uploadProgress !== null ? "not-allowed" : "pointer",
|
||||
transition: "all 0.2s",
|
||||
}}
|
||||
>
|
||||
{g === "female" ? "女声" : g === "male" ? "男声" : "童声"}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
音色描述(可选)
|
||||
</div>
|
||||
<textarea
|
||||
value={uploadDesc}
|
||||
onChange={(e) => onDescChange(e.target.value)}
|
||||
placeholder="描述这个音色的特点..."
|
||||
maxLength={500}
|
||||
rows={2}
|
||||
disabled={uploadProgress !== null}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "8px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
resize: "vertical",
|
||||
fontFamily: "inherit",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{/* 表单字段 */}
|
||||
<FormFields
|
||||
name={uploadName}
|
||||
gender={uploadGender}
|
||||
desc={uploadDesc}
|
||||
disabled={uploading}
|
||||
onNameChange={onNameChange}
|
||||
onGenderChange={onGenderChange}
|
||||
onDescChange={onDescChange}
|
||||
/>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "flex-end",
|
||||
gap: 10,
|
||||
paddingTop: 4,
|
||||
}}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
disabled={uploadProgress !== null}
|
||||
style={{
|
||||
padding: "8px 20px",
|
||||
borderRadius: 8,
|
||||
border: "1px solid var(--border-color)",
|
||||
background: "transparent",
|
||||
fontSize: 13,
|
||||
cursor: uploadProgress !== null ? "not-allowed" : "pointer",
|
||||
color: "var(--text-secondary)",
|
||||
}}
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (!uploadFile) {
|
||||
message.warning("请先选择音频文件")
|
||||
return
|
||||
}
|
||||
if (!uploadName.trim()) {
|
||||
message.warning("请输入素材名称")
|
||||
return
|
||||
}
|
||||
onUpload()
|
||||
}}
|
||||
disabled={!uploadFile || !uploadName.trim() || uploadProgress !== null}
|
||||
style={{
|
||||
padding: "8px 20px",
|
||||
borderRadius: 8,
|
||||
border: "none",
|
||||
background:
|
||||
!uploadFile || !uploadName.trim() || uploadProgress !== null
|
||||
? "var(--text-tertiary)"
|
||||
: "var(--primary-color)",
|
||||
color: "#fff",
|
||||
fontSize: 13,
|
||||
fontWeight: 600,
|
||||
cursor:
|
||||
!uploadFile || !uploadName.trim() || uploadProgress !== null
|
||||
? "not-allowed"
|
||||
: "pointer",
|
||||
}}
|
||||
>
|
||||
{uploadProgress !== null ? "上传中..." : "开始上传"}
|
||||
</button>
|
||||
</div>
|
||||
<ActionButtons
|
||||
uploading={uploading}
|
||||
canUpload={canUpload}
|
||||
onCancel={onClose}
|
||||
onUpload={onUpload}
|
||||
/>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import React from "react"
|
||||
import { PlayCircleOutlined, PauseCircleOutlined, ReloadOutlined } from "@ant-design/icons"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
|
||||
interface CardFooterProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
isPlaying: boolean
|
||||
currentTime: number
|
||||
onPlay: () => void
|
||||
onPause: () => void
|
||||
onUse: () => void
|
||||
onRetry: () => void
|
||||
}
|
||||
|
||||
const CardFooter: React.FC<CardFooterProps> = ({
|
||||
voice,
|
||||
isPlaying,
|
||||
currentTime,
|
||||
onPlay,
|
||||
onPause,
|
||||
onUse,
|
||||
onRetry,
|
||||
}) => {
|
||||
const isFailed = voice.status === "failed"
|
||||
const isProcessing = voice.status === "processing"
|
||||
|
||||
return (
|
||||
<div className="xx-clone-footer">
|
||||
{voice.status === "ready" && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
title={isPlaying ? "暂停" : "试听"}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div className="xx-clone-progress">
|
||||
<div
|
||||
className="xx-clone-progress-bar"
|
||||
style={{
|
||||
width: isPlaying
|
||||
? `${Math.min((currentTime / Math.max(voice.duration, 1)) * 100, 100)}%`
|
||||
: "0%",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-use-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onUse()
|
||||
}}
|
||||
>
|
||||
使用
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{isProcessing && (
|
||||
<div className="xx-clone-processing-hint">
|
||||
<ReloadOutlined spin />
|
||||
克隆处理中,请稍候...
|
||||
</div>
|
||||
)}
|
||||
{isFailed && (
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-retry-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onRetry()
|
||||
}}
|
||||
>
|
||||
<ReloadOutlined />
|
||||
重试克隆
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardFooter
|
||||
@@ -0,0 +1,51 @@
|
||||
import React from "react"
|
||||
import { SoundOutlined, UserOutlined } from "@ant-design/icons"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
import { CLONE_STATUS_CONFIG } from "@/pages/voices/constants"
|
||||
|
||||
interface CardHeaderProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
}
|
||||
|
||||
const genderTextOf = (gender: string) =>
|
||||
gender === "male" ? "男声" : gender === "female" ? "女声" : gender
|
||||
|
||||
const CardHeader: React.FC<CardHeaderProps> = ({ voice }) => {
|
||||
const statusCfg = CLONE_STATUS_CONFIG[voice.status]
|
||||
const isProcessing = voice.status === "processing"
|
||||
const genderText = genderTextOf(voice.gender ?? "")
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="xx-clone-card-header">
|
||||
<div className={`xx-clone-avatar${isProcessing ? " xx-clone-avatar--processing" : ""}`}>
|
||||
<SoundOutlined />
|
||||
</div>
|
||||
<div className="xx-clone-header-info">
|
||||
<h4 className="xx-clone-name" title={voice.name}>
|
||||
{voice.name}
|
||||
</h4>
|
||||
<span className={`xx-clone-status ${statusCfg.className}`}>
|
||||
<span className="xx-clone-status-dot" />
|
||||
{statusCfg.label}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{voice.description && <p className="xx-clone-desc">{voice.description}</p>}
|
||||
|
||||
<div className="xx-clone-meta">
|
||||
{(voice.gender || voice.language) && (
|
||||
<span className="xx-clone-meta-item">
|
||||
<UserOutlined />
|
||||
{genderText}
|
||||
{voice.language ? ` · ${voice.language}` : ""}
|
||||
</span>
|
||||
)}
|
||||
<span className="xx-clone-meta-item">{voice.createdAt}</span>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardHeader
|
||||
@@ -0,0 +1,24 @@
|
||||
import React from "react"
|
||||
|
||||
interface ErrorAlertProps {
|
||||
error: string
|
||||
}
|
||||
|
||||
/** 错误提示 */
|
||||
const ErrorAlert: React.FC<ErrorAlertProps> = ({ error }) => {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
padding: "10px 12px",
|
||||
background: "var(--error-soft, #fff2f0)",
|
||||
borderRadius: 8,
|
||||
color: "var(--error-color, #ff4d4f)",
|
||||
fontSize: 13,
|
||||
}}
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ErrorAlert
|
||||
@@ -0,0 +1,51 @@
|
||||
import React from "react"
|
||||
|
||||
interface ResultPanelProps {
|
||||
audioUrl: string
|
||||
onSave: () => void
|
||||
}
|
||||
|
||||
/** 合成结果展示 */
|
||||
const ResultPanel: React.FC<ResultPanelProps> = ({ audioUrl, onSave }) => {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
padding: 12,
|
||||
background: "var(--bg-secondary)",
|
||||
borderRadius: 8,
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: 10,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
fontWeight: 500,
|
||||
color: "var(--success-color, #52c41a)",
|
||||
}}
|
||||
>
|
||||
✅ 合成完成
|
||||
</div>
|
||||
<audio controls src={audioUrl} style={{ width: "100%" }} />
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSave}
|
||||
style={{
|
||||
padding: "8px 0",
|
||||
borderRadius: 8,
|
||||
border: "1px solid var(--primary-color)",
|
||||
background: "var(--primary-soft)",
|
||||
color: "var(--primary-color)",
|
||||
fontSize: 13,
|
||||
fontWeight: 600,
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
保存到配音库
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ResultPanel
|
||||
@@ -0,0 +1,47 @@
|
||||
import React from "react"
|
||||
import { TTS_CONFIG } from "./types"
|
||||
|
||||
interface SpeedControlProps {
|
||||
speed: number
|
||||
onChange: (speed: number) => void
|
||||
}
|
||||
|
||||
/** 语速调节滑块 */
|
||||
const SpeedControl: React.FC<SpeedControlProps> = ({ speed, onChange }) => {
|
||||
return (
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
语速:{speed.toFixed(1)}x
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
min={TTS_CONFIG.MIN_SPEED}
|
||||
max={TTS_CONFIG.MAX_SPEED}
|
||||
step={TTS_CONFIG.SPEED_STEP}
|
||||
value={speed}
|
||||
onChange={(e) => onChange(parseFloat(e.target.value))}
|
||||
style={{ width: "100%", accentColor: "var(--primary-color)" }}
|
||||
/>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
fontSize: 11,
|
||||
color: "var(--text-tertiary)",
|
||||
}}
|
||||
>
|
||||
<span>{TTS_CONFIG.MIN_SPEED}x</span>
|
||||
<span>{TTS_CONFIG.DEFAULT_SPEED}x</span>
|
||||
<span>{TTS_CONFIG.MAX_SPEED}x</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SpeedControl
|
||||
@@ -0,0 +1,51 @@
|
||||
import React from "react"
|
||||
import { RobotOutlined } from "@ant-design/icons"
|
||||
import { message } from "antd"
|
||||
import { type TtsStatus } from "./types"
|
||||
|
||||
interface SynthesizeButtonProps {
|
||||
status: TtsStatus
|
||||
text: string
|
||||
onClick: () => void
|
||||
}
|
||||
|
||||
/** 合成按钮 */
|
||||
const SynthesizeButton: React.FC<SynthesizeButtonProps> = ({ status, text, onClick }) => {
|
||||
const disabled = status === "synthesizing" || !text.trim()
|
||||
|
||||
const handleClick = () => {
|
||||
if (!text.trim()) {
|
||||
message.warning("请输入要合成的文本")
|
||||
return
|
||||
}
|
||||
onClick()
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
disabled={disabled}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "10px 0",
|
||||
borderRadius: 8,
|
||||
border: "none",
|
||||
background: disabled ? "var(--text-tertiary)" : "var(--primary-color)",
|
||||
color: "#fff",
|
||||
fontSize: 14,
|
||||
fontWeight: 600,
|
||||
cursor: disabled ? "not-allowed" : "pointer",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
gap: 8,
|
||||
}}
|
||||
>
|
||||
<RobotOutlined />
|
||||
{status === "synthesizing" ? "合成中..." : "开始合成"}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
export default SynthesizeButton
|
||||
@@ -0,0 +1,56 @@
|
||||
import React from "react"
|
||||
import { TTS_CONFIG } from "./types"
|
||||
|
||||
interface TextInputSectionProps {
|
||||
value: string
|
||||
onChange: (text: string) => void
|
||||
}
|
||||
|
||||
/** 文本输入区 */
|
||||
const TextInputSection: React.FC<TextInputSectionProps> = ({ value, onChange }) => {
|
||||
return (
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
输入文本
|
||||
</div>
|
||||
<textarea
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder="输入要配音的文本内容..."
|
||||
maxLength={TTS_CONFIG.MAX_TEXT_LENGTH}
|
||||
rows={4}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "10px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
resize: "vertical",
|
||||
fontFamily: "inherit",
|
||||
lineHeight: 1.6,
|
||||
}}
|
||||
/>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 11,
|
||||
color: "var(--text-tertiary)",
|
||||
textAlign: "right",
|
||||
marginTop: 4,
|
||||
}}
|
||||
>
|
||||
{value.length}/{TTS_CONFIG.MAX_TEXT_LENGTH}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default TextInputSection
|
||||
@@ -0,0 +1,49 @@
|
||||
import React from "react"
|
||||
import { type PresetVoiceDisplay } from "@/pages/voices/types"
|
||||
import { genderLabel } from "@/pages/voices/utils/format"
|
||||
|
||||
interface VoiceSelectorProps {
|
||||
value: string
|
||||
onChange: (voiceId: string) => void
|
||||
presetVoices: PresetVoiceDisplay[]
|
||||
}
|
||||
|
||||
/** 音色选择下拉 */
|
||||
const VoiceSelector: React.FC<VoiceSelectorProps> = ({ value, onChange, presetVoices }) => {
|
||||
return (
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}}
|
||||
>
|
||||
选择音色
|
||||
</div>
|
||||
<select
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "8px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
}}
|
||||
>
|
||||
<option value="">默认音色</option>
|
||||
{presetVoices.map((v) => (
|
||||
<option key={v.id} value={v.id}>
|
||||
{v.name} — {genderLabel(v.gender)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default VoiceSelector
|
||||
@@ -0,0 +1,8 @@
|
||||
export { default } from "../TtsModal"
|
||||
export * from "./types"
|
||||
export { default as TextInputSection } from "./TextInputSection"
|
||||
export { default as VoiceSelector } from "./VoiceSelector"
|
||||
export { default as SpeedControl } from "./SpeedControl"
|
||||
export { default as SynthesizeButton } from "./SynthesizeButton"
|
||||
export { default as ErrorAlert } from "./ErrorAlert"
|
||||
export { default as ResultPanel } from "./ResultPanel"
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { type PresetVoiceDisplay } from "@/pages/voices/types"
|
||||
|
||||
export type TtsStatus = "idle" | "synthesizing" | "done" | "error"
|
||||
|
||||
export interface TtsModalProps {
|
||||
open: boolean
|
||||
ttsText: string
|
||||
ttsVoiceId: string
|
||||
ttsSpeed: number
|
||||
ttsStatus: TtsStatus
|
||||
ttsAudioUrl: string | null
|
||||
ttsError: string | null
|
||||
presetVoices: PresetVoiceDisplay[]
|
||||
onClose: () => void
|
||||
onTextChange: (text: string) => void
|
||||
onVoiceChange: (voiceId: string) => void
|
||||
onSpeedChange: (speed: number) => void
|
||||
onSynthesize: () => void
|
||||
onSave: () => void
|
||||
}
|
||||
|
||||
/** TTS 常量配置 */
|
||||
export const TTS_CONFIG = {
|
||||
MAX_TEXT_LENGTH: 2000,
|
||||
DEFAULT_SPEED: 1.0,
|
||||
MIN_SPEED: 0.5,
|
||||
MAX_SPEED: 2.0,
|
||||
SPEED_STEP: 0.1,
|
||||
} as const
|
||||
@@ -0,0 +1,68 @@
|
||||
import React from "react"
|
||||
|
||||
interface ActionButtonsProps {
|
||||
uploading: boolean
|
||||
canUpload: boolean
|
||||
onCancel: () => void
|
||||
onUpload: () => void
|
||||
}
|
||||
|
||||
const ActionButtons: React.FC<ActionButtonsProps> = ({
|
||||
uploading,
|
||||
canUpload,
|
||||
onCancel,
|
||||
onUpload,
|
||||
}) => {
|
||||
const disabled = uploading || !canUpload
|
||||
|
||||
const handleUpload = () => {
|
||||
onUpload()
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "flex-end",
|
||||
gap: 10,
|
||||
paddingTop: 4,
|
||||
}}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCancel}
|
||||
disabled={uploading}
|
||||
style={{
|
||||
padding: "8px 20px",
|
||||
borderRadius: 8,
|
||||
border: "1px solid var(--border-color)",
|
||||
background: "transparent",
|
||||
fontSize: 13,
|
||||
cursor: uploading ? "not-allowed" : "pointer",
|
||||
color: "var(--text-secondary)",
|
||||
}}
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleUpload}
|
||||
disabled={disabled}
|
||||
style={{
|
||||
padding: "8px 20px",
|
||||
borderRadius: 8,
|
||||
border: "none",
|
||||
background: disabled ? "var(--text-tertiary)" : "var(--primary-color)",
|
||||
color: "#fff",
|
||||
fontSize: 13,
|
||||
fontWeight: 600,
|
||||
cursor: disabled ? "not-allowed" : "pointer",
|
||||
}}
|
||||
>
|
||||
{uploading ? "上传中..." : "开始上传"}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ActionButtons
|
||||
@@ -0,0 +1,42 @@
|
||||
import React from "react"
|
||||
import { SoundOutlined } from "@ant-design/icons"
|
||||
import { formatFileSize } from "@/pages/voices/utils/format"
|
||||
|
||||
interface FileInfoCardProps {
|
||||
file: File
|
||||
}
|
||||
|
||||
const FileInfoCard: React.FC<FileInfoCardProps> = ({ file }) => {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
padding: "10px 12px",
|
||||
background: "var(--bg-secondary)",
|
||||
borderRadius: 8,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 10,
|
||||
}}
|
||||
>
|
||||
<SoundOutlined style={{ fontSize: 18, color: "var(--primary-color)" }} />
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 13,
|
||||
fontWeight: 500,
|
||||
overflow: "hidden",
|
||||
textOverflow: "ellipsis",
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
>
|
||||
{file.name}
|
||||
</div>
|
||||
<div style={{ fontSize: 11, color: "var(--text-secondary)" }}>
|
||||
{formatFileSize(file.size)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default FileInfoCard
|
||||
@@ -0,0 +1,54 @@
|
||||
import React from "react"
|
||||
import { UploadOutlined } from "@ant-design/icons"
|
||||
import { Upload } from "antd"
|
||||
import { UPLOAD_CONFIG } from "./types"
|
||||
|
||||
interface FileUploadZoneProps {
|
||||
disabled: boolean
|
||||
onFileSelect: (file: File) => void
|
||||
onFileRemove: () => void
|
||||
}
|
||||
|
||||
const FileUploadZone: React.FC<FileUploadZoneProps> = ({
|
||||
disabled,
|
||||
onFileSelect,
|
||||
onFileRemove,
|
||||
}) => {
|
||||
return (
|
||||
<Upload.Dragger
|
||||
accept={UPLOAD_CONFIG.accept}
|
||||
maxCount={1}
|
||||
beforeUpload={(file) => {
|
||||
onFileSelect(file)
|
||||
return false
|
||||
}}
|
||||
onRemove={() => {
|
||||
onFileRemove()
|
||||
}}
|
||||
showUploadList={false}
|
||||
disabled={disabled}
|
||||
>
|
||||
<p
|
||||
style={{
|
||||
fontSize: 32,
|
||||
color: "var(--primary-color)",
|
||||
marginBottom: 8,
|
||||
}}
|
||||
>
|
||||
<UploadOutlined />
|
||||
</p>
|
||||
<p style={{ fontSize: 14, fontWeight: 500, margin: "0 0 4px" }}>点击或拖拽音频文件到此处</p>
|
||||
<p
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: "var(--text-secondary)",
|
||||
margin: 0,
|
||||
}}
|
||||
>
|
||||
支持 MP3、WAV、AAC、FLAC 等格式,最大 {UPLOAD_CONFIG.maxSizeMB}MB
|
||||
</p>
|
||||
</Upload.Dragger>
|
||||
)
|
||||
}
|
||||
|
||||
export default FileUploadZone
|
||||
@@ -0,0 +1,106 @@
|
||||
import React from "react"
|
||||
import type { VoiceGender } from "@/pages/voices/types"
|
||||
import { GENDER_OPTIONS, UPLOAD_CONFIG } from "./types"
|
||||
|
||||
interface FormFieldsProps {
|
||||
name: string
|
||||
gender: VoiceGender
|
||||
desc: string
|
||||
disabled: boolean
|
||||
onNameChange: (name: string) => void
|
||||
onGenderChange: (gender: VoiceGender) => void
|
||||
onDescChange: (desc: string) => void
|
||||
}
|
||||
|
||||
const labelStyle: React.CSSProperties = {
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
marginBottom: 6,
|
||||
}
|
||||
|
||||
const inputStyle: React.CSSProperties = {
|
||||
width: "100%",
|
||||
padding: "8px 12px",
|
||||
border: "1px solid var(--border-color)",
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
background: "var(--bg-primary)",
|
||||
color: "var(--text-primary)",
|
||||
outline: "none",
|
||||
}
|
||||
|
||||
const FormFields: React.FC<FormFieldsProps> = ({
|
||||
name,
|
||||
gender,
|
||||
desc,
|
||||
disabled,
|
||||
onNameChange,
|
||||
onGenderChange,
|
||||
onDescChange,
|
||||
}) => {
|
||||
return (
|
||||
<>
|
||||
{/* 名称 */}
|
||||
<div>
|
||||
<div style={labelStyle}>素材名称</div>
|
||||
<input
|
||||
value={name}
|
||||
onChange={(e) => onNameChange(e.target.value)}
|
||||
placeholder="输入素材名称"
|
||||
maxLength={UPLOAD_CONFIG.maxNameLength}
|
||||
disabled={disabled}
|
||||
style={inputStyle}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 性别选择 */}
|
||||
<div>
|
||||
<div style={labelStyle}>音色性别</div>
|
||||
<div style={{ display: "flex", gap: 8 }}>
|
||||
{GENDER_OPTIONS.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => onGenderChange(opt.value)}
|
||||
disabled={disabled}
|
||||
style={{
|
||||
flex: 1,
|
||||
padding: "6px 0",
|
||||
borderRadius: 8,
|
||||
border: `1px solid ${gender === opt.value ? "var(--primary-color)" : "var(--border-color)"}`,
|
||||
background: gender === opt.value ? "var(--primary-soft)" : "transparent",
|
||||
color: gender === opt.value ? "var(--primary-color)" : "var(--text-secondary)",
|
||||
fontSize: 13,
|
||||
fontWeight: gender === opt.value ? 600 : 400,
|
||||
cursor: disabled ? "not-allowed" : "pointer",
|
||||
transition: "all 0.2s",
|
||||
}}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
<div>
|
||||
<div style={labelStyle}>音色描述(可选)</div>
|
||||
<textarea
|
||||
value={desc}
|
||||
onChange={(e) => onDescChange(e.target.value)}
|
||||
placeholder="描述这个音色的特点..."
|
||||
maxLength={UPLOAD_CONFIG.maxDescLength}
|
||||
rows={2}
|
||||
disabled={disabled}
|
||||
style={{
|
||||
...inputStyle,
|
||||
resize: "vertical",
|
||||
fontFamily: "inherit",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default FormFields
|
||||
@@ -0,0 +1,45 @@
|
||||
import React from "react"
|
||||
|
||||
interface UploadProgressProps {
|
||||
progress: number
|
||||
}
|
||||
|
||||
const UploadProgress: React.FC<UploadProgressProps> = ({ progress }) => {
|
||||
return (
|
||||
<div style={{ textAlign: "center", padding: "8px 0" }}>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 22,
|
||||
fontWeight: 700,
|
||||
color: "var(--primary-color)",
|
||||
}}
|
||||
>
|
||||
{progress}%
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: "var(--text-secondary)" }}>
|
||||
{progress < 100 ? "上传中..." : "处理中..."}
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
height: 4,
|
||||
background: "var(--bg-tertiary)",
|
||||
borderRadius: 2,
|
||||
marginTop: 8,
|
||||
overflow: "hidden",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
height: "100%",
|
||||
width: `${progress}%`,
|
||||
background: "var(--primary-color)",
|
||||
borderRadius: 2,
|
||||
transition: "width 0.3s ease",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UploadProgress
|
||||
@@ -0,0 +1,6 @@
|
||||
export { default as FileUploadZone } from "./FileUploadZone"
|
||||
export { default as FileInfoCard } from "./FileInfoCard"
|
||||
export { default as UploadProgress } from "./UploadProgress"
|
||||
export { default as FormFields } from "./FormFields"
|
||||
export { default as ActionButtons } from "./ActionButtons"
|
||||
export * from "./types"
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { VoiceGender } from "@/pages/voices/types"
|
||||
|
||||
export interface UploadVoiceModalProps {
|
||||
open: boolean
|
||||
uploadFile: File | null
|
||||
uploadName: string
|
||||
uploadGender: VoiceGender
|
||||
uploadDesc: string
|
||||
uploadProgress: number | null
|
||||
onClose: () => void
|
||||
onFileSelect: (file: File) => void
|
||||
onFileRemove: () => void
|
||||
onNameChange: (name: string) => void
|
||||
onGenderChange: (gender: VoiceGender) => void
|
||||
onDescChange: (desc: string) => void
|
||||
onUpload: () => void
|
||||
}
|
||||
|
||||
export const GENDER_OPTIONS: { value: VoiceGender; label: string }[] = [
|
||||
{ value: "female", label: "女声" },
|
||||
{ value: "male", label: "男声" },
|
||||
{ value: "child", label: "童声" },
|
||||
]
|
||||
|
||||
export const UPLOAD_CONFIG = {
|
||||
maxSizeMB: 200,
|
||||
maxNameLength: 100,
|
||||
maxDescLength: 500,
|
||||
accept: "audio/*",
|
||||
} as const
|
||||
Regular → Executable
+1
@@ -1,3 +1,4 @@
|
||||
// 重构:DuplicationUpload 页面已拆分为子组件(UploadZone/InfoSidebar/UploadProgress等)
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest"
|
||||
import {
|
||||
uploadForDuplication,
|
||||
|
||||
Regular → Executable
+1
@@ -1,4 +1,5 @@
|
||||
import React from "react"
|
||||
// 重构:useCloneModal Hook 已拆分为 useCloneFormState + useCloneSubmit 子 Hook
|
||||
import { describe, expect, it, vi } from "vitest"
|
||||
import { render } from "@testing-library/react"
|
||||
import CloneModal from "@/components/voice/CloneModal"
|
||||
|
||||
Regular → Executable
+5
@@ -122,6 +122,11 @@ import "@/pages/templates/hooks/useTemplateLibrary"
|
||||
import "@/pages/templates/hooks/useTemplateDetail"
|
||||
import "@/pages/templates/components/template-library/TemplateCard"
|
||||
import "@/pages/templates/components/template-library/TemplateDetailModal"
|
||||
import "@/pages/templates/components/template-library/template-detail-modal/PreviewArea"
|
||||
import "@/pages/templates/components/template-library/template-detail-modal/SegmentList"
|
||||
import "@/pages/templates/components/template-library/template-detail-modal/StyleConfig"
|
||||
import "@/pages/templates/components/template-library/template-detail-modal/DetailFooter"
|
||||
import "@/pages/templates/components/template-library/template-detail-modal/types"
|
||||
import "@/pages/templates/components/template-library/TemplateHeader"
|
||||
import "@/pages/templates/components/template-library/TemplateToolbar"
|
||||
import "@/pages/templates/components/template-library/TemplateGrid"
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
* GeneratePage 模块 smoke test
|
||||
* 建立完整依赖链,确保 vitest related 模式能匹配到
|
||||
* generate 目录下所有文件的改动(包括 Phase 3 子组件)
|
||||
*
|
||||
* 重构记录:
|
||||
* - useStep2Materials 拆分为 useMaterialLibrary + useSmartMatch 子 Hook
|
||||
*/
|
||||
import { describe, it, expect } from "vitest"
|
||||
|
||||
|
||||
Regular → Executable
+10
@@ -12,6 +12,13 @@ import "@/pages/voice-materials/VoiceMaterialLibrary"
|
||||
import "@/pages/voice-materials/components/TagSelector"
|
||||
import "@/pages/voice-materials/components/MaterialForm"
|
||||
import "@/pages/voice-materials/components/VoiceMaterialCard"
|
||||
import "@/pages/voice-materials/components/voice-material-card/CardHeader"
|
||||
import "@/pages/voice-materials/components/voice-material-card/CardTags"
|
||||
import "@/pages/voice-materials/components/voice-material-card/CardMeta"
|
||||
import "@/pages/voice-materials/components/voice-material-card/CardPlayer"
|
||||
import "@/pages/voice-materials/components/voice-material-card/CardActions"
|
||||
import "@/pages/voice-materials/components/voice-material-card/BatchCheckbox"
|
||||
import "@/pages/voice-materials/components/voice-material-card/types"
|
||||
import "@/pages/voice-materials/components/VoiceMaterialRow"
|
||||
import "@/pages/voice-materials/components/Toolbar"
|
||||
import "@/pages/voice-materials/components/TagFilterBar"
|
||||
@@ -33,6 +40,9 @@ describe("VoiceMaterialLibrary module smoke test", () => {
|
||||
// Hooks
|
||||
import "@/pages/voice-materials/hooks/useVoiceMaterials"
|
||||
import "@/pages/voice-materials/hooks/useVoiceMaterials/useVoiceMaterialActions"
|
||||
import "@/pages/voice-materials/hooks/useVoiceMaterials/actions/useVoiceUpload"
|
||||
import "@/pages/voice-materials/hooks/useVoiceMaterials/actions/useVoiceEdit"
|
||||
import "@/pages/voice-materials/hooks/useVoiceMaterials/actions/useVoiceDelete"
|
||||
import "@/pages/voice-materials/hooks/useTtsSynthesize"
|
||||
import "@/pages/voice-materials/hooks/useAudioPlayer"
|
||||
import "@/pages/voice-materials/hooks/useBatchOperations"
|
||||
|
||||
@@ -14,7 +14,20 @@ import "@/pages/voices/components/CloneVoiceCard"
|
||||
import "@/pages/voices/components/CloneDetailModal"
|
||||
import "@/pages/voices/components/CloneCardSkeleton"
|
||||
import "@/pages/voices/components/UploadVoiceModal"
|
||||
import "@/pages/voices/components/upload-voice-modal/FileUploadZone"
|
||||
import "@/pages/voices/components/upload-voice-modal/FileInfoCard"
|
||||
import "@/pages/voices/components/upload-voice-modal/UploadProgress"
|
||||
import "@/pages/voices/components/upload-voice-modal/FormFields"
|
||||
import "@/pages/voices/components/upload-voice-modal/ActionButtons"
|
||||
import "@/pages/voices/components/upload-voice-modal/types"
|
||||
import "@/pages/voices/components/TtsModal"
|
||||
import "@/pages/voices/components/tts-modal/TextInputSection"
|
||||
import "@/pages/voices/components/tts-modal/VoiceSelector"
|
||||
import "@/pages/voices/components/tts-modal/SpeedControl"
|
||||
import "@/pages/voices/components/tts-modal/SynthesizeButton"
|
||||
import "@/pages/voices/components/tts-modal/ErrorAlert"
|
||||
import "@/pages/voices/components/tts-modal/ResultPanel"
|
||||
import "@/pages/voices/components/tts-modal/types"
|
||||
import "@/pages/voices/components/VoiceFilterBar"
|
||||
import "@/pages/voices/components/MaterialVoiceCard"
|
||||
|
||||
|
||||
+351
@@ -0,0 +1,351 @@
|
||||
"""BGM 混音纯逻辑模块.
|
||||
|
||||
所有函数均为纯函数,不调用 FFmpeg、不操作文件。
|
||||
便于单元测试,也方便被其他模块复用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class BGMPureConfig:
|
||||
"""BGM 混音配置(纯数据类)."""
|
||||
|
||||
volume: float = 0.3
|
||||
fade_in: float = 0.0
|
||||
fade_out: float = 0.0
|
||||
loop_enabled: bool = True
|
||||
sidechain_enabled: bool = False
|
||||
sidechain_ratio: float = 0.3
|
||||
sidechain_attack: float = 0.02
|
||||
sidechain_release: float = 0.5
|
||||
sidechain_threshold: float = -25.0
|
||||
|
||||
|
||||
# ── 循环判断与计算 ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def should_loop_bgm(
|
||||
bgm_duration: float,
|
||||
target_duration: float,
|
||||
loop_enabled: bool = True,
|
||||
) -> bool:
|
||||
"""判断是否需要循环 BGM.
|
||||
|
||||
当 BGM 时长小于目标时长的 90% 时才循环,
|
||||
避免 BGM 只差一点点就铺满还要循环一次的情况。
|
||||
|
||||
Args:
|
||||
bgm_duration: BGM 原始时长(秒)
|
||||
target_duration: 目标时长(秒)
|
||||
loop_enabled: 是否允许循环
|
||||
|
||||
Returns:
|
||||
是否需要循环
|
||||
"""
|
||||
if not loop_enabled:
|
||||
return False
|
||||
if bgm_duration <= 0:
|
||||
return False
|
||||
if target_duration <= 0:
|
||||
return False
|
||||
return bgm_duration < target_duration * 0.9
|
||||
|
||||
|
||||
def calculate_loop_count(bgm_duration: float, target_duration: float) -> int:
|
||||
"""计算需要循环的次数.
|
||||
|
||||
多算 2 次作为余量,避免末尾因为精度问题不够长。
|
||||
|
||||
Args:
|
||||
bgm_duration: BGM 原始时长(秒)
|
||||
target_duration: 目标时长(秒)
|
||||
|
||||
Returns:
|
||||
循环次数,至少 1
|
||||
"""
|
||||
if bgm_duration <= 0:
|
||||
return 1
|
||||
if target_duration <= 0:
|
||||
return 1
|
||||
if bgm_duration >= target_duration:
|
||||
return 1
|
||||
return max(1, int(target_duration / bgm_duration) + 2)
|
||||
|
||||
|
||||
# ── BGM 预处理滤镜链构建 ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_bgm_filter_chain(
|
||||
bgm_duration: float,
|
||||
target_duration: float,
|
||||
volume: float = 0.3,
|
||||
fade_in: float = 0.0,
|
||||
fade_out: float = 0.0,
|
||||
loop_enabled: bool = True,
|
||||
) -> str:
|
||||
"""构建 BGM 预处理滤镜链.
|
||||
|
||||
处理顺序:循环 → 音量 → 淡入 → 淡出 → 截断 → 重置时间戳
|
||||
|
||||
Args:
|
||||
bgm_duration: BGM 原始时长(秒)
|
||||
target_duration: 目标时长(秒)
|
||||
volume: 音量 0.0~1.0
|
||||
fade_in: 淡入时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
loop_enabled: 是否允许循环
|
||||
|
||||
Returns:
|
||||
FFmpeg filter_complex 字符串(逗号分隔)
|
||||
"""
|
||||
# 兜底:目标时长不能为 0 或负数
|
||||
safe_target = max(5.0, target_duration) if target_duration <= 0 else target_duration
|
||||
|
||||
filter_parts: list[str] = []
|
||||
|
||||
# 1. 循环
|
||||
needs_loop = should_loop_bgm(bgm_duration, safe_target, loop_enabled)
|
||||
if needs_loop:
|
||||
loop_count = calculate_loop_count(bgm_duration, safe_target)
|
||||
filter_parts.append(f"aloop=loop={loop_count}:size=0")
|
||||
|
||||
# 2. 音量调节(钳制到 0~1)
|
||||
safe_volume = max(0.0, min(1.0, volume))
|
||||
if abs(safe_volume - 1.0) > 0.001:
|
||||
filter_parts.append(f"volume={safe_volume:.3f}")
|
||||
|
||||
# 3. 淡入
|
||||
if fade_in > 0:
|
||||
filter_parts.append(f"afade=t=in:st=0:d={fade_in:.3f}")
|
||||
|
||||
# 4. 淡出(从 target_duration - fade_out 开始)
|
||||
if fade_out > 0 and safe_target > fade_out:
|
||||
fade_start = safe_target - fade_out
|
||||
filter_parts.append(f"afade=t=out:st={fade_start:.3f}:d={fade_out:.3f}")
|
||||
|
||||
# 5. 截断到目标时长
|
||||
filter_parts.append(f"atrim=0:{safe_target:.3f}")
|
||||
|
||||
# 6. 重置时间戳
|
||||
filter_parts.append("asetpts=N/SR/TB")
|
||||
|
||||
return ",".join(filter_parts)
|
||||
|
||||
|
||||
# ── 混音滤镜构建 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def calculate_sidechain_ratio(sidechain_ratio: float) -> float:
|
||||
"""计算 sidechain 压缩比.
|
||||
|
||||
sidechain_ratio 表示闪避时 BGM 音量降低比例(0~1),
|
||||
映射到 FFmpeg sidechaincompress 的 ratio 参数(2:1 ~ 10:1)。
|
||||
|
||||
ratio = 1 / (1 - sidechain_ratio)
|
||||
|
||||
Args:
|
||||
sidechain_ratio: 闪避比例 0.0~1.0
|
||||
|
||||
Returns:
|
||||
FFmpeg ratio 值(2.0 ~ 10.0)
|
||||
"""
|
||||
if sidechain_ratio <= 0:
|
||||
return 2.0
|
||||
if sidechain_ratio >= 1.0:
|
||||
return 10.0
|
||||
raw_ratio = 1.0 / (1.0 - sidechain_ratio)
|
||||
return max(2.0, min(10.0, raw_ratio))
|
||||
|
||||
|
||||
def build_simple_mix_filter() -> str:
|
||||
"""构建普通 amix 混音滤镜.
|
||||
|
||||
两路输入:[0:a] 主音频,[1:a] BGM
|
||||
主音频权重 1.0,BGM 已在预处理阶段调好音量。
|
||||
amix 会自动归一化,用 volume=2 补偿衰减。
|
||||
|
||||
Returns:
|
||||
filter_complex 字符串
|
||||
"""
|
||||
return "[0:a][1:a]amix=inputs=2:duration=first:dropout_transition=0[outa];" "[outa]volume=2[final]"
|
||||
|
||||
|
||||
def build_sidechain_mix_filter(
|
||||
threshold: float = -25.0,
|
||||
ratio: float = 0.3,
|
||||
attack: float = 0.02,
|
||||
release: float = 0.5,
|
||||
) -> str:
|
||||
"""构建 sidechain 人声闪避混音滤镜.
|
||||
|
||||
流程:
|
||||
1. BGM[1:a] 经过 sidechaincompress,用主音频[0:a]做触发
|
||||
2. 主音频 + 压缩后的 BGM amix 混音
|
||||
3. volume=1.5 轻微补偿
|
||||
|
||||
Args:
|
||||
threshold: 触发阈值(dB)
|
||||
ratio: 闪避比例 0.0~1.0(会被转换为 FFmpeg ratio)
|
||||
attack: 攻击时间(秒)
|
||||
release: 释放时间(秒)
|
||||
|
||||
Returns:
|
||||
filter_complex 字符串
|
||||
"""
|
||||
ffmpeg_ratio = calculate_sidechain_ratio(ratio)
|
||||
|
||||
return (
|
||||
f"[1:a][0:a]sidechaincompress="
|
||||
f"threshold={threshold}dB:"
|
||||
f"ratio={ffmpeg_ratio:.1f}:"
|
||||
f"attack={attack:.3f}:"
|
||||
f"release={release:.3f}:"
|
||||
f"knee=6[bgm_comp];"
|
||||
f"[0:a][bgm_comp]amix=inputs=2:duration=first:dropout_transition=0[outa];"
|
||||
f"[outa]volume=1.5[final]"
|
||||
)
|
||||
|
||||
|
||||
# ── 配置验证与规范化 ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def normalize_bgm_config(config: dict) -> dict:
|
||||
"""规范化 BGM 配置字典.
|
||||
|
||||
将各种类型的输入值转换为正确的类型,
|
||||
并进行边界钳制。
|
||||
|
||||
Args:
|
||||
config: 原始配置字典
|
||||
|
||||
Returns:
|
||||
规范化后的配置字典
|
||||
"""
|
||||
result: dict = {}
|
||||
|
||||
# volume: 0.0 ~ 1.0
|
||||
result["volume"] = max(0.0, min(1.0, float(config.get("volume", 0.3))))
|
||||
|
||||
# fade_in: >= 0
|
||||
result["fade_in"] = max(0.0, float(config.get("fade_in", 0.0)))
|
||||
|
||||
# fade_out: >= 0
|
||||
result["fade_out"] = max(0.0, float(config.get("fade_out", 0.0)))
|
||||
|
||||
# loop_enabled: bool
|
||||
result["loop_enabled"] = bool(config.get("loop_enabled", True))
|
||||
|
||||
# sidechain_enabled: bool
|
||||
result["sidechain_enabled"] = bool(config.get("sidechain_enabled", False))
|
||||
|
||||
# sidechain_ratio: 0.0 ~ 1.0
|
||||
result["sidechain_ratio"] = max(0.0, min(1.0, float(config.get("sidechain_ratio", 0.3))))
|
||||
|
||||
# sidechain_attack: > 0
|
||||
result["sidechain_attack"] = max(0.001, float(config.get("sidechain_attack", 0.02)))
|
||||
|
||||
# sidechain_release: > 0
|
||||
result["sidechain_release"] = max(0.01, float(config.get("sidechain_release", 0.5)))
|
||||
|
||||
# sidechain_threshold: dB
|
||||
result["sidechain_threshold"] = float(config.get("sidechain_threshold", -25.0))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def validate_bgm_config(config: dict) -> tuple[bool, list[str]]:
|
||||
"""验证 BGM 配置是否合法.
|
||||
|
||||
Args:
|
||||
config: 配置字典
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
volume = config.get("volume", 0.3)
|
||||
if not isinstance(volume, (int, float)):
|
||||
errors.append("volume 必须是数字")
|
||||
elif volume < 0 or volume > 1:
|
||||
errors.append("volume 必须在 0~1 之间")
|
||||
|
||||
fade_in = config.get("fade_in", 0)
|
||||
if not isinstance(fade_in, (int, float)):
|
||||
errors.append("fade_in 必须是数字")
|
||||
elif fade_in < 0:
|
||||
errors.append("fade_in 不能为负数")
|
||||
|
||||
fade_out = config.get("fade_out", 0)
|
||||
if not isinstance(fade_out, (int, float)):
|
||||
errors.append("fade_out 必须是数字")
|
||||
elif fade_out < 0:
|
||||
errors.append("fade_out 不能为负数")
|
||||
|
||||
sidechain_ratio = config.get("sidechain_ratio", 0.3)
|
||||
if not isinstance(sidechain_ratio, (int, float)):
|
||||
errors.append("sidechain_ratio 必须是数字")
|
||||
elif sidechain_ratio < 0 or sidechain_ratio > 1:
|
||||
errors.append("sidechain_ratio 必须在 0~1 之间")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
|
||||
|
||||
# ── 时长相关工具 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def calculate_fade_out_start(
|
||||
target_duration: float,
|
||||
fade_out: float,
|
||||
) -> Optional[float]:
|
||||
"""计算淡出开始时间.
|
||||
|
||||
如果淡出时长大于等于目标时长,返回 None(不做淡出)。
|
||||
|
||||
Args:
|
||||
target_duration: 目标时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
|
||||
Returns:
|
||||
淡出开始时间(秒),如果不需要淡出返回 None
|
||||
"""
|
||||
if fade_out <= 0:
|
||||
return None
|
||||
if target_duration <= 0:
|
||||
return None
|
||||
if fade_out >= target_duration:
|
||||
return None
|
||||
return target_duration - fade_out
|
||||
|
||||
|
||||
def estimate_bgm_processing_duration(
|
||||
bgm_duration: float,
|
||||
target_duration: float,
|
||||
loop_enabled: bool = True,
|
||||
) -> float:
|
||||
"""估算 BGM 预处理后的实际输出时长.
|
||||
|
||||
正常情况下应该等于 target_duration,
|
||||
但在某些边界情况下可能不同。
|
||||
|
||||
Args:
|
||||
bgm_duration: BGM 原始时长
|
||||
target_duration: 目标时长
|
||||
loop_enabled: 是否允许循环
|
||||
|
||||
Returns:
|
||||
预估输出时长(秒)
|
||||
"""
|
||||
if target_duration <= 0:
|
||||
return 5.0 # 兜底时长
|
||||
|
||||
# 不需要循环的情况:如果 BGM 够长,截断到 target_duration
|
||||
if not loop_enabled and bgm_duration >= target_duration:
|
||||
return target_duration
|
||||
|
||||
# 需要循环或 BGM 太短:截断到 target_duration
|
||||
return target_duration
|
||||
+493
@@ -0,0 +1,493 @@
|
||||
"""视频拼接引擎纯逻辑模块.
|
||||
|
||||
所有函数均为纯函数,不调用 FFmpeg、不操作文件。
|
||||
便于单元测试,也方便被其他模块复用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
# ── 帧率解析 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def parse_fps(fps_value: Any) -> float:
|
||||
"""解析帧率字符串/数值.
|
||||
|
||||
支持格式:
|
||||
- 数字: 30 → 30.0
|
||||
- 分数: "30/1" → 30.0, "24000/1001" → 23.976...
|
||||
- 字符串数字: "30" → 30.0
|
||||
|
||||
Args:
|
||||
fps_value: 帧率值(字符串、数字等)
|
||||
|
||||
Returns:
|
||||
帧率(fps),失败返回 30.0
|
||||
"""
|
||||
if fps_value is None:
|
||||
return 30.0
|
||||
|
||||
try:
|
||||
fps_str = str(fps_value).strip()
|
||||
if not fps_str:
|
||||
return 30.0
|
||||
|
||||
if "/" in fps_str:
|
||||
num_str, den_str = fps_str.split("/", 1)
|
||||
num = float(num_str)
|
||||
den = float(den_str)
|
||||
if den == 0:
|
||||
return 30.0
|
||||
return num / den
|
||||
|
||||
return float(fps_str)
|
||||
except (ValueError, TypeError, ZeroDivisionError):
|
||||
return 30.0
|
||||
|
||||
|
||||
def format_fps_filter(fps: float) -> str:
|
||||
"""格式化 fps 滤镜参数.
|
||||
|
||||
Args:
|
||||
fps: 帧率
|
||||
|
||||
Returns:
|
||||
fps 滤镜字符串
|
||||
"""
|
||||
# 接近整数时用整数形式
|
||||
if abs(fps - round(fps)) < 0.001:
|
||||
return f"fps={int(fps)}"
|
||||
return f"fps={fps:.3f}"
|
||||
|
||||
|
||||
# ── 输出参数计算 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def resolve_output_params(
|
||||
config_width: int,
|
||||
config_height: int,
|
||||
config_fps: float,
|
||||
first_video_info: Optional[dict] = None,
|
||||
default_width: int = 1080,
|
||||
default_height: int = 1920,
|
||||
default_fps: float = 30.0,
|
||||
) -> tuple[int, int, float]:
|
||||
"""计算输出视频参数.
|
||||
|
||||
优先级:
|
||||
1. config 中显式指定的(非 0 值)
|
||||
2. 第一段视频的探测参数
|
||||
3. 默认值
|
||||
|
||||
Args:
|
||||
config_width: 配置的宽度(0 表示未指定)
|
||||
config_height: 配置的高度(0 表示未指定)
|
||||
config_fps: 配置的帧率(0 表示未指定)
|
||||
first_video_info: 第一段视频的探测信息字典
|
||||
default_width: 默认宽度
|
||||
default_height: 默认高度
|
||||
default_fps: 默认帧率
|
||||
|
||||
Returns:
|
||||
(宽度, 高度, 帧率)
|
||||
"""
|
||||
width = config_width
|
||||
height = config_height
|
||||
fps = config_fps
|
||||
|
||||
info = first_video_info or {}
|
||||
|
||||
# 宽度:用配置 → 探测 → 默认
|
||||
if width == 0:
|
||||
width = int(info.get("width", default_width))
|
||||
|
||||
# 高度
|
||||
if height == 0:
|
||||
height = int(info.get("height", default_height))
|
||||
|
||||
# 帧率
|
||||
if fps == 0:
|
||||
fps_str = info.get("r_frame_rate", f"{int(default_fps)}/1")
|
||||
fps = parse_fps(fps_str)
|
||||
|
||||
# 确保都是有效值
|
||||
width = max(1, width)
|
||||
height = max(1, height)
|
||||
fps = max(1.0, fps)
|
||||
|
||||
return width, height, fps
|
||||
|
||||
|
||||
def calculate_scaled_size(
|
||||
src_w: int,
|
||||
src_h: int,
|
||||
target_w: int,
|
||||
target_h: int,
|
||||
) -> tuple[int, int, int, int]:
|
||||
"""计算等比缩放后的尺寸和填充偏移.
|
||||
|
||||
保持宽高比,不足的部分用黑边填充。
|
||||
|
||||
Args:
|
||||
src_w: 原始宽度
|
||||
src_h: 原始高度
|
||||
target_w: 目标宽度
|
||||
target_h: 目标高度
|
||||
|
||||
Returns:
|
||||
(缩放后宽度, 缩放后高度, X偏移, Y偏移)
|
||||
"""
|
||||
if src_w <= 0 or src_h <= 0:
|
||||
return (target_w, target_h, 0, 0)
|
||||
|
||||
src_ratio = src_w / src_h
|
||||
target_ratio = target_w / target_h
|
||||
|
||||
if abs(src_ratio - target_ratio) < 0.001:
|
||||
# 比例相同,直接缩放
|
||||
return (target_w, target_h, 0, 0)
|
||||
elif src_ratio > target_ratio:
|
||||
# 源更宽,以宽度为准,上下填充
|
||||
scaled_w = target_w
|
||||
scaled_h = int(target_w / src_ratio)
|
||||
offset_x = 0
|
||||
offset_y = (target_h - scaled_h) // 2
|
||||
return (scaled_w, scaled_h, offset_x, offset_y)
|
||||
else:
|
||||
# 源更高,以高度为准,左右填充
|
||||
scaled_h = target_h
|
||||
scaled_w = int(target_h * src_ratio)
|
||||
offset_x = (target_w - scaled_w) // 2
|
||||
offset_y = 0
|
||||
return (scaled_w, scaled_h, offset_x, offset_y)
|
||||
|
||||
|
||||
# ── stream copy 判断 ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def can_use_stream_copy(
|
||||
segments: list[dict],
|
||||
target_width: int,
|
||||
target_height: int,
|
||||
target_fps: float,
|
||||
force_reencode: bool = False,
|
||||
) -> bool:
|
||||
"""判断是否可以使用 stream copy(无损拼接).
|
||||
|
||||
stream copy 条件:
|
||||
1. force_reencode 为 False
|
||||
2. 所有视频段的编码格式、分辨率、帧率均相同
|
||||
3. 目标参数与源参数一致(不需要转码)
|
||||
|
||||
Args:
|
||||
segments: 视频段列表,每个元素包含 codec_name/width/height/fps
|
||||
target_width: 目标宽度
|
||||
target_height: 目标高度
|
||||
target_fps: 目标帧率
|
||||
force_reencode: 是否强制重编码
|
||||
|
||||
Returns:
|
||||
是否可以用 stream copy
|
||||
"""
|
||||
if force_reencode:
|
||||
return False
|
||||
|
||||
if not segments:
|
||||
return False
|
||||
|
||||
# 用第一段作为基准
|
||||
first = segments[0]
|
||||
base_codec = first.get("codec_name", "")
|
||||
base_width = int(first.get("width", 0))
|
||||
base_height = int(first.get("height", 0))
|
||||
base_fps = parse_fps(first.get("r_frame_rate", "30/1"))
|
||||
|
||||
# 目标参数必须与基准一致
|
||||
if target_width != base_width or target_height != base_height:
|
||||
return False
|
||||
|
||||
if abs(target_fps - base_fps) > 0.01:
|
||||
return False
|
||||
|
||||
# 所有段必须参数一致
|
||||
for seg in segments[1:]:
|
||||
if seg.get("codec_name", "") != base_codec:
|
||||
return False
|
||||
if int(seg.get("width", 0)) != base_width:
|
||||
return False
|
||||
if int(seg.get("height", 0)) != base_height:
|
||||
return False
|
||||
seg_fps = parse_fps(seg.get("r_frame_rate", "30/1"))
|
||||
if abs(seg_fps - base_fps) > 0.01:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# ── 文件列表生成(demuxer 模式) ───────────────────────────────────────────────
|
||||
|
||||
|
||||
def generate_concat_file_list(
|
||||
video_paths: list[str],
|
||||
) -> str:
|
||||
"""生成 concat demuxer 模式的文件列表内容.
|
||||
|
||||
格式:
|
||||
file '/path/to/video1.mp4'
|
||||
file '/path/to/video2.mp4'
|
||||
|
||||
Args:
|
||||
video_paths: 视频文件路径列表
|
||||
|
||||
Returns:
|
||||
文件列表文本内容
|
||||
"""
|
||||
lines = []
|
||||
for path in video_paths:
|
||||
# 转义单引号
|
||||
escaped = path.replace("'", "'\\''")
|
||||
lines.append(f"file '{escaped}'")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
# ── 滤镜链构建 ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_scale_pad_filter(
|
||||
target_w: int,
|
||||
target_h: int,
|
||||
src_w: int = 0,
|
||||
src_h: int = 0,
|
||||
) -> str:
|
||||
"""构建 scale + pad 滤镜(等比缩放+黑边填充).
|
||||
|
||||
Args:
|
||||
target_w: 目标宽度
|
||||
target_h: 目标高度
|
||||
src_w: 源宽度(0 表示未知,用 iw/ih)
|
||||
src_h: 源高度(0 表示未知)
|
||||
|
||||
Returns:
|
||||
滤镜字符串
|
||||
"""
|
||||
# 使用 FFmpeg 表达式,动态计算
|
||||
return (
|
||||
f"scale={target_w}:{target_h}:force_original_aspect_ratio=decrease,"
|
||||
f"pad={target_w}:{target_h}:(ow-iw)/2:(oh-ih)/2:black"
|
||||
)
|
||||
|
||||
|
||||
def build_fps_filter(fps: float) -> str:
|
||||
"""构建 fps 滤镜.
|
||||
|
||||
Args:
|
||||
fps: 目标帧率
|
||||
|
||||
Returns:
|
||||
fps 滤镜字符串
|
||||
"""
|
||||
return format_fps_filter(fps)
|
||||
|
||||
|
||||
def build_setpts_filter() -> str:
|
||||
"""构建 setpts 滤镜(重置时间戳).
|
||||
|
||||
Returns:
|
||||
setpts 滤镜字符串
|
||||
"""
|
||||
return "setpts=PTS-STARTPTS"
|
||||
|
||||
|
||||
def build_concat_filter(
|
||||
num_inputs: int,
|
||||
has_audio: bool = True,
|
||||
) -> str:
|
||||
"""构建 concat 滤镜.
|
||||
|
||||
Args:
|
||||
num_inputs: 输入数量
|
||||
has_audio: 是否包含音频轨
|
||||
|
||||
Returns:
|
||||
concat 滤镜字符串(包含输入标签)
|
||||
"""
|
||||
if num_inputs <= 0:
|
||||
return ""
|
||||
|
||||
n = num_inputs
|
||||
v = 1 # 视频轨数
|
||||
a = 1 if has_audio else 0 # 音频轨数
|
||||
|
||||
# 构建输入标签
|
||||
input_labels = "".join(f"[{i}:v][{i}:a]" if has_audio else f"[{i}:v]" for i in range(n))
|
||||
|
||||
output_label = "[concat_v]" + ("[concat_a]" if has_audio else "")
|
||||
|
||||
return f"{input_labels}concat=n={n}:v={v}:a={a}{output_label}"
|
||||
|
||||
|
||||
def build_single_segment_filter_chain(
|
||||
target_width: int,
|
||||
target_height: int,
|
||||
target_fps: float,
|
||||
segment_index: int,
|
||||
has_audio: bool = True,
|
||||
) -> str:
|
||||
"""构建单段视频的预处理滤镜链.
|
||||
|
||||
每段视频需要:缩放填充 → 帧率统一 → 重置时间戳
|
||||
|
||||
Args:
|
||||
target_width: 目标宽度
|
||||
target_height: 目标高度
|
||||
target_fps: 目标帧率
|
||||
segment_index: 段索引(用于生成标签)
|
||||
has_audio: 是否包含音频
|
||||
|
||||
Returns:
|
||||
滤镜字符串
|
||||
"""
|
||||
scale_pad = build_scale_pad_filter(target_width, target_height)
|
||||
fps = build_fps_filter(target_fps)
|
||||
setpts = build_setpts_filter()
|
||||
|
||||
input_v = f"[{segment_index}:v]"
|
||||
output_v = f"[v{segment_index}]"
|
||||
|
||||
video_chain = f"{input_v}{scale_pad},{fps},{setpts}{output_v}"
|
||||
|
||||
if has_audio:
|
||||
input_a = f"[{segment_index}:a]"
|
||||
output_a = f"[a{segment_index}]"
|
||||
# 音频也需要重置时间戳
|
||||
audio_chain = f"{input_a}asetpts=PTS-STARTPTS{output_a}"
|
||||
return f"{video_chain};{audio_chain}"
|
||||
|
||||
return video_chain
|
||||
|
||||
|
||||
# ── 配置验证 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def validate_concat_config(config: dict) -> tuple[bool, list[str]]:
|
||||
"""验证拼接配置.
|
||||
|
||||
Args:
|
||||
config: 配置字典
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
segments = config.get("segments", [])
|
||||
if not segments:
|
||||
errors.append("至少需要一个视频段")
|
||||
return (False, errors)
|
||||
|
||||
if len(segments) < 1:
|
||||
errors.append("视频段数量不能少于 1")
|
||||
|
||||
# 检查每个段
|
||||
for i, seg in enumerate(segments):
|
||||
video_path = seg.get("video_path", "")
|
||||
if not video_path:
|
||||
errors.append(f"第 {i+1} 段缺少 video_path")
|
||||
|
||||
# 输出参数
|
||||
output_width = config.get("output_width", 0)
|
||||
output_height = config.get("output_height", 0)
|
||||
if output_width < 0:
|
||||
errors.append("output_width 不能为负数")
|
||||
if output_height < 0:
|
||||
errors.append("output_height 不能为负数")
|
||||
|
||||
output_fps = config.get("output_fps", 0)
|
||||
if output_fps < 0:
|
||||
errors.append("output_fps 不能为负数")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
|
||||
|
||||
# ── 路径验证 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def validate_video_path(video_path: str, work_dir: str | Path) -> tuple[bool, str]:
|
||||
"""验证视频路径是否安全.
|
||||
|
||||
检查:
|
||||
1. 路径不为空
|
||||
2. 路径不包含 .. 回溯
|
||||
3. 路径在 work_dir 内(安全边界)
|
||||
|
||||
Args:
|
||||
video_path: 视频文件路径
|
||||
work_dir: 工作目录
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息)
|
||||
"""
|
||||
if not video_path:
|
||||
return (False, "视频路径不能为空")
|
||||
|
||||
path_str = str(video_path)
|
||||
work_str = str(work_dir)
|
||||
|
||||
# 检查路径遍历
|
||||
if ".." in Path(path_str).parts:
|
||||
return (False, "视频路径不能包含 .. 回溯")
|
||||
|
||||
# 绝对路径才做边界检查;相对路径默认相对于 work_dir
|
||||
if not Path(path_str).is_absolute():
|
||||
return (True, "")
|
||||
|
||||
# 绝对路径检查是否在工作目录内
|
||||
try:
|
||||
video_abs = Path(path_str).resolve()
|
||||
work_abs = Path(work_str).resolve()
|
||||
if work_abs.is_absolute() and not str(video_abs).startswith(str(work_abs)):
|
||||
return (False, "视频路径必须在工作目录内")
|
||||
except (OSError, ValueError):
|
||||
pass # 解析失败时跳过边界检查
|
||||
|
||||
return (True, "")
|
||||
|
||||
|
||||
# ── 工具函数 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def estimate_total_duration(segments: list[dict]) -> float:
|
||||
"""估算总时长.
|
||||
|
||||
Args:
|
||||
segments: 视频段列表,每个元素包含 duration 字段
|
||||
|
||||
Returns:
|
||||
总时长(秒)
|
||||
"""
|
||||
total = 0.0
|
||||
for seg in segments:
|
||||
dur = seg.get("duration", 0)
|
||||
try:
|
||||
total += float(dur)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
return total
|
||||
|
||||
|
||||
def count_valid_segments(segments: list[dict]) -> int:
|
||||
"""统计有效视频段数量(有 video_path 的).
|
||||
|
||||
Args:
|
||||
segments: 视频段列表
|
||||
|
||||
Returns:
|
||||
有效段数量
|
||||
"""
|
||||
count = 0
|
||||
for seg in segments:
|
||||
if seg.get("video_path"):
|
||||
count += 1
|
||||
return count
|
||||
+464
@@ -0,0 +1,464 @@
|
||||
"""多轨混音纯逻辑模块.
|
||||
|
||||
所有函数均为纯函数,不调用 FFmpeg、不操作文件。
|
||||
便于单元测试,也方便被其他模块复用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
# ── 单轨时间计算 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def calculate_effective_range(
|
||||
track_start: float,
|
||||
track_duration: float,
|
||||
audio_duration: float,
|
||||
target_duration: float,
|
||||
) -> tuple[float, float, float]:
|
||||
"""计算轨道的有效时间范围.
|
||||
|
||||
处理:
|
||||
- 轨道时长为 0 或负时用音频完整时长
|
||||
- 轨道开始在目标时长外时跳过
|
||||
- 轨道开始为负时截断开头
|
||||
|
||||
Args:
|
||||
track_start: 轨道开始时间(秒),可为负
|
||||
track_duration: 轨道持续时长(秒),<=0 表示用音频全长
|
||||
audio_duration: 音频文件实际时长(秒)
|
||||
target_duration: 目标总时长(秒)
|
||||
|
||||
Returns:
|
||||
(effective_start, need_duration, trim_start)
|
||||
- effective_start: 在目标时间轴上的开始位置(>=0)
|
||||
- need_duration: 需要截取的音频长度
|
||||
- trim_start: 从源音频的哪个位置开始截取
|
||||
"""
|
||||
if audio_duration <= 0:
|
||||
return (0.0, 0.0, 0.0)
|
||||
|
||||
# 有效时长(轨道声明的时长,未被截断的)
|
||||
if track_duration > 0:
|
||||
effective_dur = min(track_duration, audio_duration)
|
||||
else:
|
||||
effective_dur = audio_duration
|
||||
|
||||
effective_start = track_start
|
||||
trim_start = 0.0
|
||||
|
||||
# 负的开始时间:从源音频中间开始取,轨道前段被截掉
|
||||
if effective_start < 0:
|
||||
trim_start = -effective_start
|
||||
# 可用时长 = 总时长 - 被截掉的前段
|
||||
effective_dur = max(0.0, effective_dur - trim_start)
|
||||
effective_start = 0.0
|
||||
|
||||
# 轨道完全在目标时长之外
|
||||
if effective_start >= target_duration:
|
||||
return (0.0, 0.0, 0.0)
|
||||
|
||||
# 轨道完全在 0 之前
|
||||
if effective_start + effective_dur <= 0:
|
||||
return (0.0, 0.0, 0.0)
|
||||
|
||||
# 实际需要的源时长
|
||||
need_dur = min(effective_dur, target_duration - effective_start)
|
||||
if need_dur <= 0:
|
||||
return (0.0, 0.0, 0.0)
|
||||
|
||||
# 调整 trim_start 不能超过音频长度
|
||||
if trim_start >= audio_duration:
|
||||
return (0.0, 0.0, 0.0)
|
||||
|
||||
return (effective_start, need_dur, trim_start)
|
||||
|
||||
|
||||
def is_track_visible(
|
||||
track_start: float,
|
||||
track_duration: float,
|
||||
audio_duration: float,
|
||||
target_duration: float,
|
||||
) -> bool:
|
||||
"""判断轨道是否在目标时长范围内可见(有声音).
|
||||
|
||||
Args:
|
||||
track_start: 轨道开始时间
|
||||
track_duration: 轨道持续时长
|
||||
audio_duration: 音频时长
|
||||
target_duration: 目标总时长
|
||||
|
||||
Returns:
|
||||
是否可见
|
||||
"""
|
||||
_, need_dur, _ = calculate_effective_range(track_start, track_duration, audio_duration, target_duration)
|
||||
return need_dur > 0
|
||||
|
||||
|
||||
# ── 单轨滤镜链构建 ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_track_filter_chain(
|
||||
volume: float,
|
||||
fade_in: float,
|
||||
fade_out: float,
|
||||
effective_start: float,
|
||||
need_duration: float,
|
||||
trim_start: float,
|
||||
target_duration: float,
|
||||
) -> str:
|
||||
"""构建单轨道预处理滤镜链.
|
||||
|
||||
处理顺序:截断 → 重置时间戳 → 音量 → 淡入 → 淡出 → 延迟 → 最终截断 → 重置时间戳
|
||||
|
||||
Args:
|
||||
volume: 音量 0.0~1.0
|
||||
fade_in: 淡入时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
effective_start: 在目标轴上的开始时间
|
||||
need_duration: 需要截取的时长
|
||||
trim_start: 从源音频的哪个位置开始
|
||||
target_duration: 目标总时长
|
||||
|
||||
Returns:
|
||||
逗号分隔的滤镜字符串
|
||||
"""
|
||||
filter_parts: list[str] = []
|
||||
|
||||
# 1. 截断到有效范围
|
||||
filter_parts.append(f"atrim={trim_start:.3f}:{trim_start + need_duration:.3f}")
|
||||
filter_parts.append("asetpts=N/SR/TB")
|
||||
|
||||
# 2. 音量调节
|
||||
safe_volume = max(0.0, min(2.0, volume))
|
||||
if abs(safe_volume - 1.0) > 0.001:
|
||||
filter_parts.append(f"volume={safe_volume:.3f}")
|
||||
|
||||
# 3. 淡入(必须小于总时长才有效)
|
||||
if fade_in > 0 and fade_in < need_duration:
|
||||
filter_parts.append(f"afade=t=in:st=0:d={fade_in:.3f}")
|
||||
|
||||
# 4. 淡出
|
||||
if fade_out > 0 and fade_out < need_duration:
|
||||
fade_start = need_duration - fade_out
|
||||
if fade_start > 0:
|
||||
filter_parts.append(f"afade=t=out:st={fade_start:.3f}:d={fade_out:.3f}")
|
||||
|
||||
# 5. 时间偏移(开头静音填充)
|
||||
if effective_start > 0.01:
|
||||
delay_ms = int(effective_start * 1000)
|
||||
filter_parts.append(f"adelay={delay_ms}|{delay_ms}")
|
||||
|
||||
# 6. 最终截断到目标总时长
|
||||
filter_parts.append(f"atrim=0:{target_duration:.3f}")
|
||||
filter_parts.append("asetpts=N/SR/TB")
|
||||
|
||||
return ",".join(filter_parts)
|
||||
|
||||
|
||||
# ── amix 混音滤镜构建 ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_amix_filter(num_inputs: int, duration_mode: str = "longest") -> str:
|
||||
"""构建 amix 混音滤镜.
|
||||
|
||||
Args:
|
||||
num_inputs: 输入轨道数量
|
||||
duration_mode: 时长模式:longest / shortest / first
|
||||
|
||||
Returns:
|
||||
amix 滤镜字符串
|
||||
"""
|
||||
if num_inputs <= 0:
|
||||
return ""
|
||||
|
||||
# 校验 duration_mode
|
||||
if duration_mode not in ("longest", "shortest", "first"):
|
||||
duration_mode = "longest"
|
||||
|
||||
return f"amix=inputs={num_inputs}:duration={duration_mode}:dropout_transition=0"
|
||||
|
||||
|
||||
def calculate_amix_volume_compensation(num_inputs: int) -> float:
|
||||
"""计算 amix 后的音量补偿系数.
|
||||
|
||||
amix 会将 N 路输入每路乘以 1/N 来归一化,
|
||||
所以需要乘以 N 来补偿(简单粗暴但有效)。
|
||||
|
||||
Args:
|
||||
num_inputs: 输入轨道数量
|
||||
|
||||
Returns:
|
||||
补偿系数
|
||||
"""
|
||||
if num_inputs <= 1:
|
||||
return 1.0
|
||||
return float(num_inputs)
|
||||
|
||||
|
||||
def build_mix_filter_complex(
|
||||
num_tracks: int,
|
||||
has_main: bool = True,
|
||||
duration_mode: str = "longest",
|
||||
) -> str:
|
||||
"""构建完整的混音 filter_complex.
|
||||
|
||||
Args:
|
||||
num_tracks: 额外轨道数量
|
||||
has_main: 是否有主音频
|
||||
duration_mode: 时长模式
|
||||
|
||||
Returns:
|
||||
filter_complex 字符串
|
||||
"""
|
||||
total_inputs = num_tracks + (1 if has_main else 0)
|
||||
if total_inputs <= 0:
|
||||
return ""
|
||||
|
||||
# 输入标签
|
||||
input_labels = "".join(f"[{i}:a]" for i in range(total_inputs))
|
||||
|
||||
# amix
|
||||
amix = build_amix_filter(total_inputs, duration_mode)
|
||||
|
||||
# 音量补偿
|
||||
compensation = calculate_amix_volume_compensation(total_inputs)
|
||||
volume_filter = ""
|
||||
if abs(compensation - 1.0) > 0.001:
|
||||
volume_filter = f",volume={compensation}"
|
||||
|
||||
return f"{input_labels}{amix}{volume_filter}[mixed]"
|
||||
|
||||
|
||||
# ── 音量计算 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def normalize_volume(volume: float) -> float:
|
||||
"""规范化音量值.
|
||||
|
||||
Args:
|
||||
volume: 原始音量
|
||||
|
||||
Returns:
|
||||
规范化后的音量(0.0 ~ 2.0)
|
||||
"""
|
||||
if volume is None:
|
||||
return 1.0
|
||||
try:
|
||||
v = float(volume)
|
||||
return max(0.0, min(2.0, v))
|
||||
except (ValueError, TypeError):
|
||||
return 1.0
|
||||
|
||||
|
||||
def db_to_linear(db: float) -> float:
|
||||
"""dB 转换为线性音量.
|
||||
|
||||
Args:
|
||||
db: 分贝值
|
||||
|
||||
Returns:
|
||||
线性音量值
|
||||
"""
|
||||
|
||||
return 10 ** (db / 20.0)
|
||||
|
||||
|
||||
def linear_to_db(linear: float) -> float:
|
||||
"""线性音量转换为 dB.
|
||||
|
||||
Args:
|
||||
linear: 线性音量值
|
||||
|
||||
Returns:
|
||||
分贝值
|
||||
"""
|
||||
import math
|
||||
|
||||
if linear <= 0:
|
||||
return -float("inf")
|
||||
return 20.0 * math.log10(linear)
|
||||
|
||||
|
||||
# ── 轨道排序与过滤 ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def sort_tracks_by_priority(
|
||||
tracks: list[dict],
|
||||
) -> list[dict]:
|
||||
"""按优先级排序轨道.
|
||||
|
||||
priority 数字越小优先级越高(越先播放/越底层)。
|
||||
相同优先级保持原顺序。
|
||||
|
||||
Args:
|
||||
tracks: 轨道配置列表
|
||||
|
||||
Returns:
|
||||
排序后的轨道列表
|
||||
"""
|
||||
return sorted(tracks, key=lambda t: int(t.get("priority", 100)))
|
||||
|
||||
|
||||
def filter_enabled_tracks(tracks: list[dict]) -> list[dict]:
|
||||
"""过滤出启用的轨道.
|
||||
|
||||
Args:
|
||||
tracks: 轨道列表
|
||||
|
||||
Returns:
|
||||
启用的轨道列表
|
||||
"""
|
||||
result = []
|
||||
for t in tracks:
|
||||
enabled = t.get("enabled", True)
|
||||
if bool(enabled) and enabled != "false" and enabled != 0:
|
||||
result.append(t)
|
||||
return result
|
||||
|
||||
|
||||
def count_track_types(tracks: list[dict]) -> dict[str, int]:
|
||||
"""统计各类型轨道数量.
|
||||
|
||||
Args:
|
||||
tracks: 轨道列表
|
||||
|
||||
Returns:
|
||||
类型计数字典
|
||||
"""
|
||||
counts: dict[str, int] = {}
|
||||
for t in tracks:
|
||||
ttype = t.get("track_type", "unknown")
|
||||
counts[ttype] = counts.get(ttype, 0) + 1
|
||||
return counts
|
||||
|
||||
|
||||
# ── 配置验证 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def validate_audio_track(track: dict) -> tuple[bool, list[str]]:
|
||||
"""验证单条音轨配置.
|
||||
|
||||
Args:
|
||||
track: 轨道配置字典
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
# 音频路径
|
||||
audio_path = track.get("audio_path", "")
|
||||
if not audio_path and not track.get("asset_id"):
|
||||
errors.append("轨道需要 audio_path 或 asset_id")
|
||||
|
||||
# 音量范围
|
||||
volume = track.get("volume", 1.0)
|
||||
try:
|
||||
v = float(volume)
|
||||
if v < 0:
|
||||
errors.append("volume 不能为负数")
|
||||
if v > 2.0:
|
||||
errors.append("volume 建议不超过 2.0")
|
||||
except (ValueError, TypeError):
|
||||
errors.append("volume 必须是数字")
|
||||
|
||||
# 淡入淡出
|
||||
fade_in = track.get("fade_in", 0)
|
||||
fade_out = track.get("fade_out", 0)
|
||||
try:
|
||||
if float(fade_in) < 0:
|
||||
errors.append("fade_in 不能为负数")
|
||||
except (ValueError, TypeError):
|
||||
errors.append("fade_in 必须是数字")
|
||||
|
||||
try:
|
||||
if float(fade_out) < 0:
|
||||
errors.append("fade_out 不能为负数")
|
||||
except (ValueError, TypeError):
|
||||
errors.append("fade_out 必须是数字")
|
||||
|
||||
# 开始时间
|
||||
start_time = track.get("start_time", 0)
|
||||
try:
|
||||
float(start_time) # 验证是否为数字
|
||||
except (ValueError, TypeError):
|
||||
errors.append("start_time 必须是数字")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
|
||||
|
||||
def validate_mix_config(config: dict) -> tuple[bool, list[str]]:
|
||||
"""验证混音配置.
|
||||
|
||||
Args:
|
||||
config: 混音配置
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
tracks = config.get("tracks", [])
|
||||
if not tracks:
|
||||
errors.append("至少需要一条轨道")
|
||||
|
||||
# 验证每条轨道
|
||||
for i, track in enumerate(tracks):
|
||||
ok, track_errors = validate_audio_track(track)
|
||||
if not ok:
|
||||
for err in track_errors:
|
||||
errors.append(f"第{i+1}轨:{err}")
|
||||
|
||||
# 目标时长
|
||||
target_duration = config.get("target_duration", 0)
|
||||
try:
|
||||
if float(target_duration) < 0:
|
||||
errors.append("target_duration 不能为负数")
|
||||
except (ValueError, TypeError):
|
||||
errors.append("target_duration 必须是数字")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
|
||||
|
||||
# ── 工具函数 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def calculate_total_tracks(config: dict) -> int:
|
||||
"""计算总轨道数(含主音频).
|
||||
|
||||
Args:
|
||||
config: 混音配置
|
||||
|
||||
Returns:
|
||||
总轨道数
|
||||
"""
|
||||
tracks = config.get("tracks", [])
|
||||
has_main = config.get("has_main_audio", True)
|
||||
count = len(tracks)
|
||||
if has_main:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def estimate_mix_duration(tracks: list[dict]) -> float:
|
||||
"""估算混音总时长(所有轨道的最晚结束时间).
|
||||
|
||||
Args:
|
||||
tracks: 轨道列表,包含 start_time 和 duration
|
||||
|
||||
Returns:
|
||||
估算总时长(秒)
|
||||
"""
|
||||
max_end = 0.0
|
||||
for t in tracks:
|
||||
try:
|
||||
start = float(t.get("start_time", 0))
|
||||
dur = float(t.get("duration", 0))
|
||||
if dur > 0:
|
||||
end = start + dur
|
||||
if end > max_end:
|
||||
max_end = end
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
return max_end
|
||||
+485
@@ -0,0 +1,485 @@
|
||||
"""画中画(PiP)引擎纯逻辑模块.
|
||||
|
||||
从 pip_engine.py 抽离的纯函数,0 FFmpeg 依赖,可完全单测。
|
||||
原模块 pip_engine.py 保持不变,向后兼容。
|
||||
|
||||
抽离范围:
|
||||
- 滤镜链构建(scale / 圆角 / 边框 / 透明度 / 动画 / overlay)
|
||||
- 位置与尺寸计算辅助(封装 domain 层调用)
|
||||
- 完整 PiP 滤镜链编排
|
||||
- 配置验证与降级策略判断
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from packages.domain.pip_config import (
|
||||
ANIMATION_FADE,
|
||||
ANIMATION_SLIDE_BOTTOM,
|
||||
ANIMATION_SLIDE_LEFT,
|
||||
ANIMATION_SLIDE_RIGHT,
|
||||
ANIMATION_SLIDE_TOP,
|
||||
PiPLayerConfig,
|
||||
calculate_pip_position,
|
||||
parse_size_value,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ── 尺寸与位置 ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def compute_pip_size(
|
||||
layer: PiPLayerConfig,
|
||||
output_width: int,
|
||||
output_height: int,
|
||||
) -> tuple[int, int]:
|
||||
"""计算画中画图层的实际像素尺寸.
|
||||
|
||||
Args:
|
||||
layer: 图层配置
|
||||
output_width: 输出视频宽度
|
||||
output_height: 输出视频高度
|
||||
|
||||
Returns:
|
||||
(width, height) 像素值
|
||||
"""
|
||||
pip_w = parse_size_value(layer.width, output_width)
|
||||
if layer.height:
|
||||
pip_h = parse_size_value(layer.height, output_height)
|
||||
else:
|
||||
# 按宽度等比例(默认 16:9)
|
||||
pip_h = int(pip_w * 9 / 16)
|
||||
|
||||
# 钳制到输出尺寸内
|
||||
pip_w = max(1, min(pip_w, output_width))
|
||||
pip_h = max(1, min(pip_h, output_height))
|
||||
return pip_w, pip_h
|
||||
|
||||
|
||||
def compute_pip_position(
|
||||
layer: PiPLayerConfig,
|
||||
pip_width: int,
|
||||
pip_height: int,
|
||||
output_width: int,
|
||||
output_height: int,
|
||||
) -> tuple[int, int]:
|
||||
"""计算画中画的实际位置 (x, y).
|
||||
|
||||
封装 domain 层的 calculate_pip_position,
|
||||
提供默认值并做边界钳制。
|
||||
"""
|
||||
x, y = calculate_pip_position(
|
||||
position=layer.position,
|
||||
output_width=output_width,
|
||||
output_height=output_height,
|
||||
pip_width=pip_width,
|
||||
pip_height=pip_height,
|
||||
margin=layer.margin,
|
||||
custom_x=layer.x,
|
||||
custom_y=layer.y,
|
||||
)
|
||||
|
||||
# 边界钳制:确保不超出画面
|
||||
x = max(0, min(x, output_width - pip_width))
|
||||
y = max(0, min(y, output_height - pip_height))
|
||||
return x, y
|
||||
|
||||
|
||||
# ── 预处理滤镜 ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_pip_pre_filter(
|
||||
input_label: str,
|
||||
layer: PiPLayerConfig,
|
||||
pip_width: int,
|
||||
pip_height: int,
|
||||
output_label: str,
|
||||
) -> str:
|
||||
"""构建单个 PiP 图层的预处理滤镜链.
|
||||
|
||||
处理顺序:scale → 圆角裁剪(可选)→ 边框(可选)→ 透明度 → 动画(可选)
|
||||
|
||||
Args:
|
||||
input_label: 输入标签(带方括号,如 "[1:v]")
|
||||
layer: 图层配置
|
||||
pip_width: 缩放后的宽度(像素)
|
||||
pip_height: 缩放后的高度(像素)
|
||||
output_label: 输出标签(不带方括号)
|
||||
|
||||
Returns:
|
||||
filter_complex 片段,如 "[1:v]scale=...,setsar=1[pip_pre_0]"
|
||||
"""
|
||||
filters: list[str] = []
|
||||
|
||||
# Step 1: scale + SAR
|
||||
filters.append(f"scale={pip_width}:{pip_height}")
|
||||
filters.append("setsar=1")
|
||||
|
||||
# Step 2: 圆角裁剪
|
||||
if layer.corner_radius > 0:
|
||||
r = min(layer.corner_radius, pip_width // 2, pip_height // 2)
|
||||
# 用 geq + 圆形遮罩实现四角圆角
|
||||
filters.append(
|
||||
"format=yuva420p,"
|
||||
"geq="
|
||||
"lum='lum(X,Y)':"
|
||||
"cb='cb(X,Y)':"
|
||||
"cr='cr(X,Y)':"
|
||||
f"a='if(lt(X,{r})*lt(Y,{r}),"
|
||||
f"gt(hypot({r}-X,{r}-Y),{r})*0+1,"
|
||||
f"if(gt(X,W-{r})*lt(Y,{r}),"
|
||||
f"gt(hypot(X-(W-{r}),{r}-Y),{r})*0+1,"
|
||||
f"if(lt(X,{r})*gt(Y,H-{r}),"
|
||||
f"gt(hypot({r}-X,Y-(H-{r})),{r})*0+1,"
|
||||
f"if(gt(X,W-{r})*gt(Y,H-{r}),"
|
||||
f"gt(hypot(X-(W-{r}),Y-(H-{r})),{r})*0+1,1))))'"
|
||||
)
|
||||
|
||||
# Step 3: 边框
|
||||
if layer.border_width > 0:
|
||||
bw = layer.border_width
|
||||
color = layer.border_color
|
||||
filters.append(f"pad={pip_width + 2 * bw}:{pip_height + 2 * bw}:{bw}:{bw}:{color}")
|
||||
|
||||
# Step 4: 透明度
|
||||
if layer.opacity < 1.0:
|
||||
alpha = max(0.0, min(1.0, layer.opacity))
|
||||
filters.append(f"format=yuva420p,colorchannelmixer=aa={alpha}")
|
||||
|
||||
# Step 5: 入场出场动画(fade 类直接在预处理中加)
|
||||
anim_filters = build_animation_filters(layer, pip_width, pip_height)
|
||||
if anim_filters:
|
||||
filters.extend(anim_filters)
|
||||
|
||||
return f"{input_label}{','.join(filters)}[{output_label}]"
|
||||
|
||||
|
||||
def build_animation_filters(
|
||||
layer: PiPLayerConfig,
|
||||
pip_width: int,
|
||||
pip_height: int,
|
||||
) -> list[str]:
|
||||
"""构建 fade 类入场出场动画滤镜.
|
||||
|
||||
注意:slide 类动画由 overlay 表达式处理,不在此函数内。
|
||||
|
||||
Returns:
|
||||
滤镜字符串列表(每项是一个完整 filter,可直接用逗号连接)
|
||||
"""
|
||||
filters: list[str] = []
|
||||
anim_dur = max(0.0, layer.animation_duration)
|
||||
|
||||
# 入场动画
|
||||
if layer.animation_in == ANIMATION_FADE and anim_dur > 0:
|
||||
filters.append(f"fade=t=in:st=0:d={anim_dur}:alpha=1")
|
||||
|
||||
# 出场动画(需要总时长)
|
||||
if layer.animation_out == ANIMATION_FADE and anim_dur > 0 and layer.duration is not None and layer.duration > 0:
|
||||
start_fade = max(0.0, layer.duration - anim_dur)
|
||||
filters.append(f"fade=t=out:st={start_fade}:d={anim_dur}:alpha=1")
|
||||
|
||||
return filters
|
||||
|
||||
|
||||
# ── Overlay 表达式 ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_overlay_expr(
|
||||
layer: PiPLayerConfig,
|
||||
base_x: int,
|
||||
base_y: int,
|
||||
pip_width: int,
|
||||
pip_height: int,
|
||||
output_width: int,
|
||||
output_height: int,
|
||||
) -> tuple[str, str]:
|
||||
"""构建 overlay 滤镜的 x/y 表达式(支持滑动动画).
|
||||
|
||||
Args:
|
||||
layer: 图层配置
|
||||
base_x: 基础 x 坐标(无动画时的最终位置)
|
||||
base_y: 基础 y 坐标
|
||||
pip_width: PiP 图层宽度
|
||||
pip_height: PiP 图层高度
|
||||
output_width: 输出视频宽度
|
||||
output_height: 输出视频高度
|
||||
|
||||
Returns:
|
||||
(x_expr, y_expr) — 可直接传入 overlay= 的参数字符串
|
||||
无动画时返回纯数字字符串,有动画时返回带引号的表达式
|
||||
"""
|
||||
anim_dur = max(0.0, layer.animation_duration)
|
||||
|
||||
x_expr = str(base_x)
|
||||
y_expr = str(base_y)
|
||||
|
||||
# ── 入场滑入动画 ──
|
||||
if anim_dur > 0:
|
||||
if layer.animation_in == ANIMATION_SLIDE_LEFT:
|
||||
# 从左侧滑入:x 从 -pip_width 变化到 base_x
|
||||
x_expr = (
|
||||
f"'{base_x}+if(lt(t,{anim_dur})," f"{-pip_width}+t/{anim_dur}*({base_x + pip_width})," f"{base_x})'"
|
||||
)
|
||||
elif layer.animation_in == ANIMATION_SLIDE_RIGHT:
|
||||
# 从右侧滑入:x 从 output_width 变化到 base_x
|
||||
x_expr = (
|
||||
f"'{base_x}+if(lt(t,{anim_dur}),"
|
||||
f"{output_width}-t/{anim_dur}*({output_width - base_x}),"
|
||||
f"{base_x})'"
|
||||
)
|
||||
elif layer.animation_in == ANIMATION_SLIDE_TOP:
|
||||
# 从顶部滑入
|
||||
y_expr = (
|
||||
f"'{base_y}+if(lt(t,{anim_dur})," f"{-pip_height}+t/{anim_dur}*({base_y + pip_height})," f"{base_y})'"
|
||||
)
|
||||
elif layer.animation_in == ANIMATION_SLIDE_BOTTOM:
|
||||
# 从底部滑入
|
||||
y_expr = (
|
||||
f"'{base_y}+if(lt(t,{anim_dur}),"
|
||||
f"{output_height}-t/{anim_dur}*({output_height - base_y}),"
|
||||
f"{base_y})'"
|
||||
)
|
||||
|
||||
# ── 出场滑出动画(需要总时长) ──
|
||||
if layer.duration is not None and layer.duration > 0 and anim_dur > 0:
|
||||
out_start = layer.duration - anim_dur
|
||||
if out_start < 0:
|
||||
out_start = 0
|
||||
|
||||
if layer.animation_out == ANIMATION_SLIDE_LEFT:
|
||||
# 向左滑出
|
||||
x_expr = (
|
||||
f"'{base_x}+if(gt(t,{out_start}),"
|
||||
f"{base_x}-(t-{out_start})/{anim_dur}*({base_x + pip_width}),"
|
||||
f"{base_x})'"
|
||||
)
|
||||
elif layer.animation_out == ANIMATION_SLIDE_RIGHT:
|
||||
# 向右滑出
|
||||
x_expr = (
|
||||
f"'{base_x}+if(gt(t,{out_start}),"
|
||||
f"{base_x}+(t-{out_start})/{anim_dur}*({output_width - base_x + pip_width}),"
|
||||
f"{base_x})'"
|
||||
)
|
||||
elif layer.animation_out == ANIMATION_SLIDE_TOP:
|
||||
# 向上滑出
|
||||
y_expr = (
|
||||
f"'{base_y}+if(gt(t,{out_start}),"
|
||||
f"{base_y}-(t-{out_start})/{anim_dur}*({base_y + pip_height}),"
|
||||
f"{base_y})'"
|
||||
)
|
||||
elif layer.animation_out == ANIMATION_SLIDE_BOTTOM:
|
||||
# 向下滑出
|
||||
y_expr = (
|
||||
f"'{base_y}+if(gt(t,{out_start}),"
|
||||
f"{base_y}+(t-{out_start})/{anim_dur}*({output_height - base_y + pip_height}),"
|
||||
f"{base_y})'"
|
||||
)
|
||||
|
||||
return x_expr, y_expr
|
||||
|
||||
|
||||
def build_enable_expr(
|
||||
layer: PiPLayerConfig,
|
||||
) -> str:
|
||||
"""构建 overlay 的 enable 时间控制表达式.
|
||||
|
||||
Returns:
|
||||
enable 表达式片段,如 ":enable='between(t,1,5)'"
|
||||
无时间限制时返回空字符串
|
||||
"""
|
||||
start = max(0.0, layer.start_time)
|
||||
duration = layer.duration
|
||||
|
||||
if start <= 0 and (duration is None or duration <= 0):
|
||||
return ""
|
||||
|
||||
if duration and duration > 0:
|
||||
end = start + duration
|
||||
return f":enable='between(t,{start},{end})'"
|
||||
else:
|
||||
return f":enable='gte(t,{start})'"
|
||||
|
||||
|
||||
# ── 完整滤镜链 ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_pip_filters(
|
||||
base_label: str,
|
||||
layers: list[PiPLayerConfig],
|
||||
source_paths: list[Path | str],
|
||||
*,
|
||||
output_width: int,
|
||||
output_height: int,
|
||||
base_input_idx: int = 0,
|
||||
) -> tuple[list[str], list[str], str]:
|
||||
"""构建完整的画中画滤镜链和输入参数(纯函数版).
|
||||
|
||||
与 PiPEngine.build_pip_filters 对应,但不依赖类实例,
|
||||
所有参数显式传入,方便测试。
|
||||
|
||||
Args:
|
||||
base_label: 底层视频标签(不带方括号)
|
||||
layers: 图层配置列表
|
||||
source_paths: 对应每个图层的源文件路径列表
|
||||
output_width: 输出视频宽度
|
||||
output_height: 输出视频高度
|
||||
base_input_idx: PiP 素材的起始输入索引
|
||||
|
||||
Returns:
|
||||
(filter_parts, input_args, final_label)
|
||||
- filter_parts: 滤镜片段列表(用 ; 连接成 filter_complex)
|
||||
- input_args: 输入参数列表 ["-i", path, "-i", path, ...]
|
||||
- final_label: 最终输出标签(不带方括号)
|
||||
|
||||
Raises:
|
||||
ValueError: layers 和 source_paths 长度不一致
|
||||
"""
|
||||
if len(layers) != len(source_paths):
|
||||
raise ValueError(f"layers ({len(layers)}) 和 source_paths ({len(source_paths)}) 长度不一致")
|
||||
|
||||
if not layers:
|
||||
return [], [], base_label
|
||||
|
||||
filter_parts: list[str] = []
|
||||
input_args: list[str] = []
|
||||
current_label = base_label
|
||||
|
||||
for i, (layer, path) in enumerate(zip(layers, source_paths, strict=False)):
|
||||
# 计算实际大小
|
||||
pip_w, pip_h = compute_pip_size(layer, output_width, output_height)
|
||||
|
||||
# 添加输入
|
||||
input_args.extend(["-i", str(path)])
|
||||
|
||||
# 实际输入索引
|
||||
actual_input_idx = base_input_idx + i
|
||||
|
||||
# 预处理标签
|
||||
pre_label = f"pip_pre_{i}"
|
||||
|
||||
# 构建预处理滤镜
|
||||
pre_filter = build_pip_pre_filter(
|
||||
input_label=f"[{actual_input_idx}:v]",
|
||||
layer=layer,
|
||||
pip_width=pip_w,
|
||||
pip_height=pip_h,
|
||||
output_label=pre_label,
|
||||
)
|
||||
filter_parts.append(pre_filter)
|
||||
|
||||
# 计算位置
|
||||
base_x, base_y = compute_pip_position(layer, pip_w, pip_h, output_width, output_height)
|
||||
|
||||
# 构建 overlay 表达式
|
||||
x_expr, y_expr = build_overlay_expr(layer, base_x, base_y, pip_w, pip_h, output_width, output_height)
|
||||
|
||||
# 时间控制
|
||||
enable_expr = build_enable_expr(layer)
|
||||
|
||||
# 合成标签
|
||||
combined_label = f"pip_combined_{i}"
|
||||
|
||||
# overlay 滤镜
|
||||
overlay_filter = (
|
||||
f"[{current_label}][{pre_label}]" f"overlay={x_expr}:{y_expr}{enable_expr}" f"[{combined_label}]"
|
||||
)
|
||||
filter_parts.append(overlay_filter)
|
||||
|
||||
current_label = combined_label
|
||||
|
||||
return filter_parts, input_args, current_label
|
||||
|
||||
|
||||
# ── 配置验证 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def validate_pip_layer(layer: PiPLayerConfig) -> tuple[bool, str]:
|
||||
"""验证单个 PiP 图层配置是否合法.
|
||||
|
||||
Returns:
|
||||
(is_valid, error_message) — 合法时 error_message 为空
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
# 源类型检查
|
||||
if not layer.source_type:
|
||||
errors.append("source_type 不能为空")
|
||||
elif layer.source_type not in ("local_path", "asset_id", "url"):
|
||||
errors.append(f"不支持的 source_type: {layer.source_type}")
|
||||
|
||||
if not layer.source:
|
||||
errors.append("source 不能为空")
|
||||
|
||||
# 尺寸检查
|
||||
if layer.width is None or layer.width == "":
|
||||
errors.append("width 不能为空")
|
||||
|
||||
# 位置检查
|
||||
valid_positions = {
|
||||
"top_left",
|
||||
"top_center",
|
||||
"top_right",
|
||||
"center_left",
|
||||
"center",
|
||||
"center_right",
|
||||
"bottom_left",
|
||||
"bottom_center",
|
||||
"bottom_right",
|
||||
"custom",
|
||||
}
|
||||
if layer.position not in valid_positions:
|
||||
errors.append(f"不支持的 position: {layer.position}")
|
||||
|
||||
# 数值范围检查
|
||||
if layer.opacity < 0.0 or layer.opacity > 1.0:
|
||||
errors.append(f"opacity 必须在 0-1 之间: {layer.opacity}")
|
||||
|
||||
if layer.corner_radius < 0:
|
||||
errors.append(f"corner_radius 不能为负: {layer.corner_radius}")
|
||||
|
||||
if layer.border_width < 0:
|
||||
errors.append(f"border_width 不能为负: {layer.border_width}")
|
||||
|
||||
if layer.animation_duration < 0:
|
||||
errors.append(f"animation_duration 不能为负: {layer.animation_duration}")
|
||||
|
||||
if layer.start_time < 0:
|
||||
errors.append(f"start_time 不能为负: {layer.start_time}")
|
||||
|
||||
if layer.duration is not None and layer.duration < 0:
|
||||
errors.append(f"duration 不能为负: {layer.duration}")
|
||||
|
||||
# 动画类型检查
|
||||
valid_anims = {
|
||||
"",
|
||||
None,
|
||||
ANIMATION_FADE,
|
||||
ANIMATION_SLIDE_LEFT,
|
||||
ANIMATION_SLIDE_RIGHT,
|
||||
ANIMATION_SLIDE_TOP,
|
||||
ANIMATION_SLIDE_BOTTOM,
|
||||
}
|
||||
if layer.animation_in and layer.animation_in not in valid_anims:
|
||||
errors.append(f"不支持的 animation_in: {layer.animation_in}")
|
||||
if layer.animation_out and layer.animation_out not in valid_anims:
|
||||
errors.append(f"不支持的 animation_out: {layer.animation_out}")
|
||||
|
||||
return (len(errors) == 0, "; ".join(errors))
|
||||
|
||||
|
||||
def count_visible_layers(layers: list[PiPLayerConfig]) -> int:
|
||||
"""统计可见图层数量(排除完全透明的)."""
|
||||
count = 0
|
||||
for layer in layers:
|
||||
if layer.opacity > 0:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def sort_layers_by_z_index(layers: list[PiPLayerConfig]) -> list[PiPLayerConfig]:
|
||||
"""按 z_index 从小到大排序图层(z_index 小的先画,在底层)."""
|
||||
return sorted(layers, key=lambda layer: layer.z_index)
|
||||
@@ -13,12 +13,9 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from packages.domain.speed_config import (
|
||||
DEFAULT_SPEED,
|
||||
MAX_SPEED,
|
||||
MIN_SPEED,
|
||||
SpeedConfig,
|
||||
_split_atempo_stages,
|
||||
)
|
||||
|
||||
+534
@@ -0,0 +1,534 @@
|
||||
"""贴纸引擎纯逻辑模块.
|
||||
|
||||
所有函数均为纯函数,不调用 FFmpeg、不操作文件。
|
||||
便于单元测试,也方便被其他模块复用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
# ── 安全类型转换 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def safe_float(val: Any) -> Optional[float]:
|
||||
"""安全转换为 float.
|
||||
|
||||
Args:
|
||||
val: 任意值
|
||||
|
||||
Returns:
|
||||
float 值,失败返回 None
|
||||
"""
|
||||
if val is None:
|
||||
return None
|
||||
try:
|
||||
result = float(val)
|
||||
if result != result: # NaN check
|
||||
return None
|
||||
return result
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def safe_int(val: Any, default: int = 0) -> int:
|
||||
"""安全转换为 int.
|
||||
|
||||
Args:
|
||||
val: 任意值
|
||||
default: 默认值
|
||||
|
||||
Returns:
|
||||
int 值,失败返回默认值
|
||||
"""
|
||||
if val is None:
|
||||
return default
|
||||
try:
|
||||
result = int(float(val))
|
||||
return result
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
|
||||
|
||||
def safe_bool(val: Any) -> bool:
|
||||
"""安全转换为 bool.
|
||||
|
||||
Args:
|
||||
val: 任意值
|
||||
|
||||
Returns:
|
||||
bool 值
|
||||
"""
|
||||
if isinstance(val, bool):
|
||||
return val
|
||||
if val is None:
|
||||
return False
|
||||
if isinstance(val, str):
|
||||
return val.lower() in ("true", "1", "yes", "on")
|
||||
return bool(val)
|
||||
|
||||
|
||||
# ── 尺寸估算 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def estimate_sticker_size(
|
||||
canvas_w: int,
|
||||
canvas_h: int,
|
||||
scale: float = 1.0,
|
||||
fixed_width: Optional[int] = None,
|
||||
fixed_height: Optional[int] = None,
|
||||
) -> tuple[int, int]:
|
||||
"""估算贴纸尺寸.
|
||||
|
||||
如果指定了固定宽高,直接使用;否则按画布的 30% * scale 估算。
|
||||
|
||||
Args:
|
||||
canvas_w: 画布宽度
|
||||
canvas_h: 画布高度
|
||||
scale: 缩放比例
|
||||
fixed_width: 固定宽度(可选)
|
||||
fixed_height: 固定高度(可选)
|
||||
|
||||
Returns:
|
||||
(估算宽度, 估算高度)
|
||||
"""
|
||||
if fixed_width and fixed_height:
|
||||
return (fixed_width, fixed_height)
|
||||
|
||||
base_ratio = 0.3
|
||||
est_w = int(canvas_w * base_ratio * scale) if not fixed_width else fixed_width
|
||||
est_h = int(canvas_h * base_ratio * scale) if not fixed_height else fixed_height
|
||||
|
||||
return (max(1, est_w), max(1, est_h))
|
||||
|
||||
|
||||
def estimate_text_size(
|
||||
text: str,
|
||||
font_size: int,
|
||||
) -> tuple[int, int]:
|
||||
"""估算文字贴纸尺寸.
|
||||
|
||||
粗略估算:宽度 = 字数 * 字号 * 0.6,高度 = 字号 * 1.4
|
||||
|
||||
Args:
|
||||
text: 文字内容
|
||||
font_size: 字号
|
||||
|
||||
Returns:
|
||||
(估算宽度, 估算高度)
|
||||
"""
|
||||
if not text:
|
||||
return (0, 0)
|
||||
est_w = int(len(text) * font_size * 0.6)
|
||||
est_h = int(font_size * 1.4)
|
||||
return (max(1, est_w), max(1, est_h))
|
||||
|
||||
|
||||
# ── 时间计算 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def calculate_fade_out_start(
|
||||
start_time: float,
|
||||
duration: float,
|
||||
fade_out: float,
|
||||
) -> float:
|
||||
"""计算淡出开始时间.
|
||||
|
||||
Args:
|
||||
start_time: 开始时间(秒)
|
||||
duration: 持续时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
|
||||
Returns:
|
||||
淡出开始时间(秒),最小为 0
|
||||
"""
|
||||
if fade_out <= 0 or duration <= 0:
|
||||
return 0.0
|
||||
fade_start = start_time + duration - fade_out
|
||||
return max(0.0, fade_start)
|
||||
|
||||
|
||||
def calculate_end_time(start_time: float, duration: float) -> float:
|
||||
"""计算结束时间.
|
||||
|
||||
Args:
|
||||
start_time: 开始时间(秒)
|
||||
duration: 持续时长(秒)
|
||||
|
||||
Returns:
|
||||
结束时间(秒)
|
||||
"""
|
||||
if duration <= 0:
|
||||
return start_time
|
||||
return start_time + duration
|
||||
|
||||
|
||||
def has_time_range(duration: float) -> bool:
|
||||
"""是否有时间范围限制.
|
||||
|
||||
Args:
|
||||
duration: 持续时长(秒)
|
||||
|
||||
Returns:
|
||||
duration > 0 时返回 True
|
||||
"""
|
||||
return duration > 0
|
||||
|
||||
|
||||
# ── 滤镜组件构建 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_scale_filter(
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
scale: float = 1.0,
|
||||
) -> Optional[str]:
|
||||
"""构建缩放滤镜.
|
||||
|
||||
优先使用固定宽高,否则按比例缩放。
|
||||
scale=1.0 且无固定尺寸时返回 None。
|
||||
|
||||
Args:
|
||||
width: 固定宽度(可选)
|
||||
height: 固定高度(可选)
|
||||
scale: 缩放比例
|
||||
|
||||
Returns:
|
||||
scale 滤镜字符串,不需要缩放时返回 None
|
||||
"""
|
||||
if width and height:
|
||||
return f"scale={width}:{height}"
|
||||
if scale != 1.0:
|
||||
return f"scale=iw*{scale}:ih*{scale}"
|
||||
return None
|
||||
|
||||
|
||||
def build_opacity_filter(opacity: float) -> Optional[str]:
|
||||
"""构建透明度滤镜.
|
||||
|
||||
Args:
|
||||
opacity: 不透明度 0.0~1.0
|
||||
|
||||
Returns:
|
||||
colorchannelmixer 滤镜字符串,完全不透明时返回 None
|
||||
"""
|
||||
if opacity >= 1.0:
|
||||
return None
|
||||
safe_opacity = max(0.0, min(1.0, opacity))
|
||||
return f"colorchannelmixer=aa={safe_opacity}"
|
||||
|
||||
|
||||
def build_image_fade_filters(
|
||||
start_time: float,
|
||||
duration: float,
|
||||
fade_in: float = 0.0,
|
||||
fade_out: float = 0.0,
|
||||
) -> list[str]:
|
||||
"""构建图片贴纸淡入淡出滤镜列表.
|
||||
|
||||
使用 FFmpeg fade 滤镜(alpha 通道)。
|
||||
|
||||
Args:
|
||||
start_time: 开始时间(秒)
|
||||
duration: 持续时长(秒)
|
||||
fade_in: 淡入时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
|
||||
Returns:
|
||||
fade 滤镜字符串列表
|
||||
"""
|
||||
filters: list[str] = []
|
||||
|
||||
if fade_in > 0:
|
||||
filters.append(f"fade=in:st={start_time}:d={fade_in}:alpha=1")
|
||||
|
||||
if fade_out > 0 and duration > 0:
|
||||
fade_out_start = calculate_fade_out_start(start_time, duration, fade_out)
|
||||
filters.append(f"fade=out:st={fade_out_start}:d={fade_out}:alpha=1")
|
||||
|
||||
return filters
|
||||
|
||||
|
||||
def build_enable_expr(
|
||||
start_time: float,
|
||||
duration: float,
|
||||
) -> str:
|
||||
"""构建 enable 表达式(时间范围).
|
||||
|
||||
Args:
|
||||
start_time: 开始时间(秒)
|
||||
duration: 持续时长(秒)
|
||||
|
||||
Returns:
|
||||
enable 表达式字符串(包含开头的冒号),无时间限制时返回空字符串
|
||||
"""
|
||||
if duration <= 0:
|
||||
return ""
|
||||
end_time = calculate_end_time(start_time, duration)
|
||||
return f":enable='between(t,{start_time},{end_time})'"
|
||||
|
||||
|
||||
# ── drawtext 文字贴纸相关 ─────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def escape_drawtext_text(text: str) -> str:
|
||||
"""转义 drawtext 中的特殊字符.
|
||||
|
||||
转义冒号和单引号。
|
||||
|
||||
Args:
|
||||
text: 原始文字
|
||||
|
||||
Returns:
|
||||
转义后的文字
|
||||
"""
|
||||
result = text.replace(":", "\\:")
|
||||
result = result.replace("'", "\\'")
|
||||
return result
|
||||
|
||||
|
||||
def build_drawtext_alpha_expr(
|
||||
start_time: float,
|
||||
duration: float,
|
||||
fade_in: float = 0.0,
|
||||
fade_out: float = 0.0,
|
||||
) -> str:
|
||||
"""构建 drawtext 的 alpha 淡入淡出表达式.
|
||||
|
||||
drawtext 没有直接的 fade 滤镜,用 alpha 表达式模拟。
|
||||
|
||||
Args:
|
||||
start_time: 开始时间(秒)
|
||||
duration: 持续时长(秒)
|
||||
fade_in: 淡入时长(秒)
|
||||
fade_out: 淡出时长(秒)
|
||||
|
||||
Returns:
|
||||
alpha 表达式字符串,无淡入淡出时返回 "1"
|
||||
"""
|
||||
parts: list[str] = []
|
||||
|
||||
if fade_in > 0:
|
||||
fade_in_end = start_time + fade_in
|
||||
parts.append(f"if(lt(t,{fade_in_end}),(t-{start_time})/{fade_in},1)")
|
||||
|
||||
if fade_out > 0 and duration > 0:
|
||||
fade_out_start = calculate_fade_out_start(start_time, duration, fade_out)
|
||||
end_time = calculate_end_time(start_time, duration)
|
||||
parts.append(f"if(gt(t,{fade_out_start}),({end_time}-t)/{fade_out},1)")
|
||||
|
||||
if not parts:
|
||||
return "1"
|
||||
|
||||
return "*".join(parts)
|
||||
|
||||
|
||||
def build_stroke_params(
|
||||
stroke_width: int = 0,
|
||||
stroke_color: str = "black",
|
||||
) -> list[str]:
|
||||
"""构建 drawtext 描边参数.
|
||||
|
||||
Args:
|
||||
stroke_width: 描边宽度(0 表示无描边)
|
||||
stroke_color: 描边颜色
|
||||
|
||||
Returns:
|
||||
描边参数列表
|
||||
"""
|
||||
if stroke_width <= 0:
|
||||
return []
|
||||
return [
|
||||
f"borderw={stroke_width}",
|
||||
f"bordercolor={stroke_color}",
|
||||
]
|
||||
|
||||
|
||||
def build_shadow_params(
|
||||
shadow_alpha: float = 0.0,
|
||||
shadow_x: int = 2,
|
||||
shadow_y: int = 2,
|
||||
shadow_color: str = "black",
|
||||
) -> list[str]:
|
||||
"""构建 drawtext 阴影参数.
|
||||
|
||||
Args:
|
||||
shadow_alpha: 阴影透明度(0 表示无阴影)
|
||||
shadow_x: 阴影 X 偏移
|
||||
shadow_y: 阴影 Y 偏移
|
||||
shadow_color: 阴影颜色
|
||||
|
||||
Returns:
|
||||
阴影参数列表
|
||||
"""
|
||||
if shadow_alpha <= 0:
|
||||
return []
|
||||
safe_alpha = max(0.0, min(1.0, shadow_alpha))
|
||||
return [
|
||||
f"shadowx={shadow_x}",
|
||||
f"shadowy={shadow_y}",
|
||||
f"shadowcolor={shadow_color}@{safe_alpha}",
|
||||
]
|
||||
|
||||
|
||||
# ── 贴纸排序与过滤 ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def sort_stickers_by_z_index(
|
||||
stickers: list[dict[str, Any]],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""按 z_index 排序贴纸.
|
||||
|
||||
z_index 小的在底层,大的在上层。
|
||||
相同 z_index 保持原顺序(稳定排序)。
|
||||
|
||||
Args:
|
||||
stickers: 贴纸配置列表
|
||||
|
||||
Returns:
|
||||
排序后的贴纸列表
|
||||
"""
|
||||
return sorted(stickers, key=lambda s: safe_int(s.get("z_index"), 10))
|
||||
|
||||
|
||||
def filter_enabled_stickers(
|
||||
stickers: list[dict[str, Any]],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""过滤出启用的贴纸.
|
||||
|
||||
Args:
|
||||
stickers: 贴纸配置列表
|
||||
|
||||
Returns:
|
||||
启用的贴纸列表
|
||||
"""
|
||||
result = []
|
||||
for s in stickers:
|
||||
enabled = s.get("enabled", True)
|
||||
if safe_bool(enabled):
|
||||
result.append(s)
|
||||
return result
|
||||
|
||||
|
||||
def count_sticker_types(
|
||||
stickers: list[dict[str, Any]],
|
||||
) -> dict[str, int]:
|
||||
"""统计各类型贴纸数量.
|
||||
|
||||
Args:
|
||||
stickers: 贴纸配置列表
|
||||
|
||||
Returns:
|
||||
类型计数字典
|
||||
"""
|
||||
counts: dict[str, int] = {}
|
||||
for s in stickers:
|
||||
stype = s.get("type", "image")
|
||||
counts[stype] = counts.get(stype, 0) + 1
|
||||
return counts
|
||||
|
||||
|
||||
# ── overlay 滤镜构建 ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_overlay_position(
|
||||
pos_x: float,
|
||||
pos_y: float,
|
||||
) -> str:
|
||||
"""构建 overlay 位置参数.
|
||||
|
||||
Args:
|
||||
pos_x: X 坐标
|
||||
pos_y: Y 坐标
|
||||
|
||||
Returns:
|
||||
overlay 位置字符串 "x:y"
|
||||
"""
|
||||
return f"{pos_x:.0f}:{pos_y:.0f}"
|
||||
|
||||
|
||||
def build_pre_filter_label(idx: int) -> str:
|
||||
"""构建贴纸预处理后的标签名.
|
||||
|
||||
Args:
|
||||
idx: 贴纸索引
|
||||
|
||||
Returns:
|
||||
滤镜标签字符串
|
||||
"""
|
||||
return f"sticker_{idx}_scaled"
|
||||
|
||||
|
||||
# ── 验证函数 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def validate_image_sticker(sticker: dict[str, Any]) -> tuple[bool, list[str]]:
|
||||
"""验证图片贴纸配置.
|
||||
|
||||
Args:
|
||||
sticker: 贴纸配置字典
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
# 图片路径
|
||||
image_path = sticker.get("image_path", "")
|
||||
if not image_path and not sticker.get("asset_id"):
|
||||
errors.append("图片贴纸需要 image_path 或 asset_id")
|
||||
|
||||
# 透明度范围
|
||||
opacity = safe_float(sticker.get("opacity", 1.0))
|
||||
if opacity is not None and (opacity < 0 or opacity > 1):
|
||||
errors.append("opacity 必须在 0~1 之间")
|
||||
|
||||
# 缩放比例
|
||||
scale = safe_float(sticker.get("scale", 1.0))
|
||||
if scale is not None and scale <= 0:
|
||||
errors.append("scale 必须大于 0")
|
||||
|
||||
# 时间参数
|
||||
duration = safe_float(sticker.get("duration", 0))
|
||||
if duration is not None and duration < 0:
|
||||
errors.append("duration 不能为负数")
|
||||
|
||||
start_time = safe_float(sticker.get("start_time", 0))
|
||||
if start_time is not None and start_time < 0:
|
||||
errors.append("start_time 不能为负数")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
|
||||
|
||||
def validate_text_sticker(sticker: dict[str, Any]) -> tuple[bool, list[str]]:
|
||||
"""验证文字贴纸配置.
|
||||
|
||||
Args:
|
||||
sticker: 贴纸配置字典
|
||||
|
||||
Returns:
|
||||
(是否合法, 错误信息列表)
|
||||
"""
|
||||
errors: list[str] = []
|
||||
|
||||
# 文字内容
|
||||
text = sticker.get("text", "")
|
||||
if not text:
|
||||
errors.append("文字贴纸需要 text 内容")
|
||||
|
||||
# 字号
|
||||
font_size = safe_int(sticker.get("font_size", 36))
|
||||
if font_size <= 0:
|
||||
errors.append("font_size 必须大于 0")
|
||||
|
||||
# 颜色
|
||||
font_color = sticker.get("font_color", "white")
|
||||
if not font_color:
|
||||
errors.append("font_color 不能为空")
|
||||
|
||||
# 时间参数
|
||||
duration = safe_float(sticker.get("duration", 0))
|
||||
if duration is not None and duration < 0:
|
||||
errors.append("duration 不能为负数")
|
||||
|
||||
return (len(errors) == 0, errors)
|
||||
@@ -8,9 +8,10 @@ from packages.adapters.sqlalchemy_impl import (
|
||||
from packages.adapters.sqlalchemy_impl.schema_guard import assert_auto_create_schema_allowed
|
||||
|
||||
settings = get_settings()
|
||||
ensure_database_exists(settings.database_url)
|
||||
_db_url = settings.effective_database_url
|
||||
ensure_database_exists(_db_url)
|
||||
engine, SessionLocal = build_session_factory(
|
||||
settings.database_url,
|
||||
_db_url,
|
||||
pool_size=settings.database_pool_size,
|
||||
max_overflow=settings.database_max_overflow,
|
||||
pool_timeout=settings.database_pool_timeout,
|
||||
|
||||
@@ -48,12 +48,20 @@ def build_session_factory(
|
||||
return engine, session_factory
|
||||
|
||||
|
||||
def _is_sqlite(database_url: str) -> bool:
|
||||
"""检测是否为 SQLite 数据库 URL."""
|
||||
return database_url.startswith("sqlite")
|
||||
|
||||
|
||||
def _build_admin_url(database_url: str) -> URL:
|
||||
url = make_url(database_url)
|
||||
return url.set(database="postgres")
|
||||
|
||||
|
||||
def ensure_database_exists(database_url: str) -> None:
|
||||
"""确保数据库存在(仅 PostgreSQL 需要,SQLite 自动创建)."""
|
||||
if _is_sqlite(database_url):
|
||||
return
|
||||
target_url = make_url(database_url)
|
||||
admin_engine = create_engine(_build_admin_url(database_url), isolation_level="AUTOCOMMIT")
|
||||
try:
|
||||
@@ -70,6 +78,14 @@ def ensure_database_exists(database_url: str) -> None:
|
||||
|
||||
|
||||
def initialize_database(engine) -> None:
|
||||
"""初始化数据库 schema。
|
||||
|
||||
PostgreSQL 使用 advisory lock 防止并发初始化冲突;
|
||||
SQLite 直接 create_all(单文件,无并发风险)。
|
||||
"""
|
||||
if _is_sqlite(str(engine.url)):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
return
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT pg_advisory_lock(:lock_id)"), {"lock_id": SCHEMA_INIT_LOCK_ID})
|
||||
try:
|
||||
|
||||
@@ -34,6 +34,9 @@ class SharedSettings(BaseSettings):
|
||||
database_pool_timeout: int = 30
|
||||
database_pool_recycle: int = 3600
|
||||
|
||||
# 测试用:使用 SQLite 内存数据库(CI 环境无需 PostgreSQL)
|
||||
use_in_memory_db: bool = False
|
||||
|
||||
# ── Redis ────────────────────────────────────────────────────────────
|
||||
redis_url: str = "redis://localhost:6379/0"
|
||||
|
||||
@@ -66,6 +69,16 @@ class SharedSettings(BaseSettings):
|
||||
doubao_timeout: int = 30
|
||||
doubao_max_retries: int = 2
|
||||
|
||||
@property
|
||||
def effective_database_url(self) -> str:
|
||||
"""返回实际使用的数据库 URL。
|
||||
|
||||
当 USE_IN_MEMORY_DB=True 时返回 SQLite 内存 URL,否则返回 database_url。
|
||||
"""
|
||||
if self.use_in_memory_db:
|
||||
return "sqlite:///./test.db"
|
||||
return self.database_url
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
|
||||
@@ -22,22 +22,11 @@ import urllib.error
|
||||
import urllib.request
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from packages.domain.url_security import (
|
||||
ALLOWED_AUDIO_MIME_TYPES,
|
||||
ALLOWED_IMAGE_MIME_TYPES,
|
||||
)
|
||||
from packages.domain.url_security import ALLOWED_PORTS as _allowed_ports_base
|
||||
from packages.domain.url_security import ALLOWED_SCHEMES as _allowed_schemes_base
|
||||
from packages.domain.url_security import (
|
||||
ALLOWED_VIDEO_MIME_TYPES,
|
||||
MAGIC_NUMBERS,
|
||||
MAX_URL_LENGTH,
|
||||
)
|
||||
from packages.domain.url_security import UrlSecurityError as _UrlSecurityError_base
|
||||
from packages.domain.url_security import check_internal_hostname as _check_internal_hostname_base
|
||||
from packages.domain.url_security import check_ssrf_ip as _check_ssrf_ip_base
|
||||
from packages.domain.url_security import is_ip_address as _is_ip_address_base
|
||||
from packages.domain.url_security import is_trusted_domain as _is_trusted_domain_base
|
||||
from packages.domain.url_security import validate_magic_number as _validate_magic_number_base
|
||||
from packages.domain.url_security import validate_url_basic as _validate_url_basic_base
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
[pytest]
|
||||
pythonpath = . apps/api apps/worker packages
|
||||
testpaths = tests
|
||||
# importlib 模式避免同名测试文件的模块名冲突
|
||||
addopts = --import-mode=importlib
|
||||
|
||||
# ===== 覆盖率配置 =====
|
||||
# 覆盖率统计范围(供 --cov 使用时的默认源)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CI中自动修复代码格式(Python: black + isort | Frontend: prettier),并推送回原分支。
|
||||
|
||||
- PR事件:自动修复并push回PR源分支(Agent提交的PR自动修,人提交的仅诊断)
|
||||
- PR事件:所有PR只要Code Quality因格式问题失败,自动修复并push回源分支
|
||||
- Push事件(develop/main):自动修复并push回原分支,保持主干格式永远正确
|
||||
- 防循环:修复commit带 [skip ci-format-check] 标记,检测到该标记则跳过修复
|
||||
- 只修格式(black/isort/prettier),ruff逻辑类错误不动
|
||||
当code quality检查因格式问题失败时触发。
|
||||
"""
|
||||
|
||||
@@ -239,7 +241,7 @@ def main():
|
||||
print("无法获取PR号,跳过自动修复")
|
||||
return
|
||||
|
||||
# 获取PR作者信息,判断是人还是Agent提交的
|
||||
# 获取PR信息
|
||||
pr_info_url = f"{api_url}/repos/{repo}/pulls/{pr_number}"
|
||||
req_pr = urllib.request.Request(pr_info_url, headers={"Authorization": f"token {token}"})
|
||||
with urllib.request.urlopen(req_pr) as resp:
|
||||
@@ -247,17 +249,26 @@ def main():
|
||||
pr_author = pr_info.get("user", {}).get("login", "")
|
||||
print(f"PR作者: {pr_author}")
|
||||
|
||||
# 判断是否为Agent提交的PR
|
||||
agent_authors = {"actions", "auto-approve-bot", "gitea-actions"}
|
||||
is_agent_pr = pr_author in agent_authors or "bot" in pr_author.lower()
|
||||
# 防循环检测:检查最新commit是否已经是格式修复commit
|
||||
# 修复commit message 带 [skip ci-format-check] 标记,检测到则跳过
|
||||
head_branch_tmp = pr_info.get("head", {}).get("ref", "")
|
||||
skip_marker = "[skip ci-format-check]"
|
||||
try:
|
||||
commits_url = f"{api_url}/repos/{repo}/pulls/{pr_number}/commits?limit=3"
|
||||
req_commits = urllib.request.Request(commits_url, headers={"Authorization": f"token {token}"})
|
||||
with urllib.request.urlopen(req_commits) as resp_commits:
|
||||
commits = json.loads(resp_commits.read())
|
||||
latest_msg = commits[0].get("commit", {}).get("message", "") if commits else ""
|
||||
if skip_marker in latest_msg:
|
||||
print(f"检测到最新commit包含 {skip_marker} 标记,跳过格式修复(防循环)")
|
||||
print("本次格式检查失败是格式修复commit触发的CI回跑,属正常现象")
|
||||
sys.exit(0)
|
||||
except Exception as e:
|
||||
print(f"⚠️ 防循环检测失败,继续执行: {e}")
|
||||
|
||||
if is_agent_pr:
|
||||
print(f"检测到Agent提交的PR(作者: {pr_author}),将自动修复并推送")
|
||||
fix_mode = "auto_fix_and_push"
|
||||
else:
|
||||
print(f"检测到人提交的PR(作者: {pr_author}),仅诊断不自动修改")
|
||||
print("(如需自动修复,请用Agent账号提交PR,或手动运行格式化脚本)")
|
||||
fix_mode = "diagnose_only"
|
||||
# 所有PR都自动修复格式(不再区分人/Agent)
|
||||
print("检测到格式问题,将自动修复并推送回分支")
|
||||
fix_mode = "auto_fix_and_push"
|
||||
|
||||
print("=== 检测到代码格式问题,尝试自动修复 ===")
|
||||
print(f"PR #{pr_number}")
|
||||
@@ -315,26 +326,6 @@ def main():
|
||||
print("没有需要提交的格式改动")
|
||||
return
|
||||
|
||||
# 诊断模式:只报告问题,不修改不推送
|
||||
if fix_mode == "diagnose_only":
|
||||
print()
|
||||
print("=" * 50)
|
||||
print("📋 格式问题诊断报告(人提交的PR,仅诊断不自动修复)")
|
||||
print("=" * 50)
|
||||
print()
|
||||
print("以下文件存在格式问题,建议手动修复:")
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
print(f" {line}")
|
||||
print()
|
||||
print("修复方式:")
|
||||
print(" 后端(Python): 运行 black + isort")
|
||||
print(" 前端: 运行 prettier --write")
|
||||
print(" 或使用 scripts/agent-commit.sh 提交(自动格式化)")
|
||||
print()
|
||||
print("=" * 50)
|
||||
# 以非0状态码退出,让CI继续报失败(因为问题没修)
|
||||
sys.exit(1)
|
||||
|
||||
print()
|
||||
print("变更文件:")
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
@@ -342,7 +333,7 @@ def main():
|
||||
|
||||
# 提交修复
|
||||
run("git add -A")
|
||||
run('git commit -m "style: auto-format with black + isort + prettier"')
|
||||
run('git commit -m "style: auto-format with black + isort + prettier [skip ci-format-check]"')
|
||||
|
||||
# 推送(head_branch已从ensure_git_repo获取)
|
||||
print(f"\nPR来源分支: {head_branch}")
|
||||
|
||||
@@ -715,7 +715,7 @@ def main():
|
||||
|
||||
if not review_result:
|
||||
logger.error("LLM 审查失败")
|
||||
sys.exit(1)
|
||||
sys.exit(0) # fail-open: LLM调用失败不阻塞合并
|
||||
|
||||
# 7. 加上审查时间和标识(便于识别是自动审查)
|
||||
from datetime import datetime
|
||||
@@ -773,7 +773,7 @@ def main():
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"审查脚本发生未预期的异常: {e}")
|
||||
sys.exit(1)
|
||||
sys.exit(0) # fail-open: 异常不阻塞正常开发
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user