refactor: 全面重构代码并加白话注释,移动全局并行,去掉去向/重复记录

- 重构后端与前端,全程大白话注释(不懂程序也能看懂)
- 移动阶段改为全局并行(MoveAll,工人数=CPU核心数)
- 去掉记录每个文件搬去哪了(删除去向回填,解决写库卡顿)
- 去掉重复文件标记与统计:重复判定改为 MD5+路径都相同才算,重复则原地忽略不移动;MD5 相同但路径不同仍照常搬
- 媒体库去掉归类目录列,看板去掉重复/归类分布统计
This commit is contained in:
2025-05-11 21:05:35 +08:00
parent f915a9b1ac
commit 8979c744a8
26 changed files with 1044 additions and 683 deletions
+32 -14
View File
@@ -1,4 +1,10 @@
// Package probe 负责媒体信息探测:图片尺寸读取、ffprobe 视频/音频解析与内嵌解码器释放。
// Package probe 负责给媒体文件"体检":
// - 图片:读出它的宽高(尺寸),特殊格式(WebP/BMP/PSD)有专门的读法
// - 视频/音频:调 ffprobe 程序读出编码、时长、宽高等信息
// - 明暗分析:图片是偏暗还是偏亮(给图片分类用)
//
// 大白话理解:这一步负责搞清楚"这个文件到底是什么样的",好让后面决定把它归到哪。
// 体检结果统一放进 ProbeResult 这一个结构里,图片、视频、音频都往里填。
package probe
import (
@@ -10,44 +16,51 @@ import (
"time"
)
// ProbeResult 媒体探测结果(图片/视频/音频通用)
// ProbeResult 一份"体检报告",图片/视频/音频通用。
type ProbeResult struct {
HasVideo bool
HasAudio bool
Width int
Height int
Duration float64
VideoCodec string
AudioCodec string
Format string
RawJSON string
HasVideo bool // 有没有视频流
HasAudio bool // 有没有音频流
Width int // 宽(像素)
Height int // 高(像素)
Duration float64 // 时长(秒)
VideoCodec string // 视频编码(如 h264)
AudioCodec string // 音频编码(如 aac)
Format string // 容器格式(如 mp4)
RawJSON string // ffprobe 读出的原始信息,存下来留底
}
// streamInfo 是 ffprobe 输出里"一条流"的 JSON 结构(视频流/音频流都会列出来)。
type streamInfo struct {
Index int `json:"index"`
CodecType string `json:"codec_type"`
CodecType string `json:"codec_type"` // video / audio
CodecName string `json:"codec_name"`
Width int `json:"width"`
Height int `json:"height"`
}
// formatInfo 是 ffprobe 输出里"容器格式"部分的 JSON 结构。
type formatInfo struct {
Duration string `json:"duration"`
Format string `json:"format_name"`
}
// probeOutput 是 ffprobe 完整输出的 JSON 结构。
type probeOutput struct {
Streams []streamInfo `json:"streams"`
Format formatInfo `json:"format"`
}
// ffprobeTimeout ffprobe 探测超时时间,超时视为探测失败(避免损坏文件卡死程序)
// ffprobeTimeout 调 ffprobe 的超时时间。超过就当探测失败,
// 免得遇到损坏文件时卡住整个程序。
const ffprobeTimeout = 30 * time.Second
// ProbeMediaFile 用 ffprobe 探测媒体文件,一次调用同时获取视频/音频流与时长
// ProbeMediaFile 调 ffprobe 探测一个媒体文件,一次调用就把
// 视频流/音频流/时长/格式全部拿到。
func ProbeMediaFile(ffprobePath, filePath string) (*ProbeResult, error) {
// 带超时的执行环境:文件坏了也不至于永远卡住
ctx, cancel := context.WithTimeout(context.Background(), ffprobeTimeout)
defer cancel()
// 用 ffprobe 读信息,指定输出 JSON 格式
cmd := exec.CommandContext(ctx, ffprobePath, "-v", "error",
"-show_entries", "stream=index,codec_type,codec_name,width,height:format=duration,format_name",
"-of", "json", filePath)
@@ -55,6 +68,7 @@ func ProbeMediaFile(ffprobePath, filePath string) (*ProbeResult, error) {
if err != nil {
return nil, fmt.Errorf("ffprobe failed: %w", err)
}
// 把 JSON 输出翻译成结构体
var out probeOutput
if err := json.Unmarshal(output, &out); err != nil {
return nil, fmt.Errorf("parse ffprobe output: %w", err)
@@ -63,9 +77,13 @@ func ProbeMediaFile(ffprobePath, filePath string) (*ProbeResult, error) {
Format: out.Format.Format,
RawJSON: string(output),
}
// 时长(秒)
if d, err := strconv.ParseFloat(out.Format.Duration, 64); err == nil {
res.Duration = d
}
// 把每条流的信息填进报告:
// 视频流 → 记下"有视频",补上编码和宽高;
// 音频流 → 记下"有音频",补上编码。
for _, s := range out.Streams {
switch s.CodecType {
case "video":