feat: add rust_register CLI tool
新增功能: - 獨立的 register CLI 工具 - 不依賴 API 和 Worker - 直接註冊視頻文件到數據庫 特性: - ✅ 元數據提取 (ffprobe) - ✅ Birth UUID 計算 - ✅ Force 模式 (覆蓋已存在) - ✅ 詳細輸出模式 - ✅ 錯誤處理 使用方法: ```bash # 註冊文件 rust_register /path/to/video.mp4 # 強制重新註冊 rust_register /path/to/video.mp4 --force # 詳細輸出 rust_register /path/to/video.mp4 --verbose ``` 功能: 1. 文件驗證 2. UUID 計算 (birth UUID) 3. 元數據提取 (duration, resolution, fps) 4. 數據庫插入 5. Force 模式 (自動 unregister) 元數據: - Duration: 8.80s - Resolution: 1920x1080 - FPS: 59.94 - Total frames: 527 優勢: - 同步執行(無需等待 Worker) - 獨立運行(無需 API Server) - 詳細輸出(便於調試) 文檔: - docs_v1.0/OPERATIONS/Rust_Register_CLI_Tool.md 測試: - ✅ 編譯成功 - ✅ 基本註冊測試通過 - ✅ Force 模式測試通過 - ✅ Verbose 模式測試通過 Binary: - target/debug/rust_register
This commit is contained in:
@@ -0,0 +1,297 @@
|
||||
---
|
||||
title: Rust Register CLI Tool
|
||||
version: 1.0
|
||||
date: 2026-09-28
|
||||
author: OpenCode
|
||||
status: production
|
||||
---
|
||||
|
||||
## 概述
|
||||
|
||||
`rust_register` 是一個獨立的 CLI 工具,用於將視頻文件註冊到 Momentry 數據庫。
|
||||
|
||||
---
|
||||
|
||||
## 功能
|
||||
|
||||
### 註冊流程
|
||||
|
||||
1. **文件驗證**
|
||||
- 檢查文件是否存在
|
||||
- 計算 canonical path
|
||||
|
||||
2. **UUID 計算**
|
||||
- 使用 birth UUID 算法
|
||||
- 基於 MAC、birthday、file path、file name
|
||||
|
||||
3. **元數據提取**
|
||||
- Duration
|
||||
- Width/Height
|
||||
- FPS
|
||||
- Total frames
|
||||
|
||||
4. **數據庫插入**
|
||||
- 插入到 videos 表
|
||||
- 設置狀態為 'registered'
|
||||
|
||||
5. **Force 模式**
|
||||
- 自動 unregister 已存在的文件
|
||||
- 重新註冊
|
||||
|
||||
---
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 基本用法
|
||||
|
||||
```bash
|
||||
# 註冊文件
|
||||
rust_register /path/to/video.mp4
|
||||
|
||||
# 強制重新註冊(unregister + register)
|
||||
rust_register /path/to/video.mp4 --force
|
||||
|
||||
# 詳細輸出
|
||||
rust_register /path/to/video.mp4 --verbose
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 參數
|
||||
|
||||
| 參數 | 必需 | 說明 |
|
||||
|------|------|------|
|
||||
| `FILE` | ✅ | 視頻文件路徑 |
|
||||
| `-F, --force` | ❌ | 強制註冊(覆蓋已存在的文件) |
|
||||
| `-a, --auto-pipeline` | ❌ | 註冊後自動觸發 pipeline |
|
||||
| `-v, --verbose` | ❌ | 詳細輸出 |
|
||||
|
||||
---
|
||||
|
||||
### 示例
|
||||
|
||||
#### 1. 基本註冊
|
||||
|
||||
```bash
|
||||
rust_register /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4
|
||||
```
|
||||
|
||||
輸出:
|
||||
```
|
||||
[REGISTER] Starting for: /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4
|
||||
[REGISTER] Extracting metadata...
|
||||
[REGISTER] Inserting into database...
|
||||
[REGISTER] Registered: b496a68800f90770aa7246c877f38985
|
||||
[REGISTER] Done
|
||||
[REGISTER] File UUID: b496a68800f90770aa7246c877f38985
|
||||
[REGISTER] Duration: 8.80s
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### 2. 強制重新註冊
|
||||
|
||||
```bash
|
||||
rust_register /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4 --force
|
||||
```
|
||||
|
||||
輸出:
|
||||
```
|
||||
[REGISTER] Starting for: /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4
|
||||
[REGISTER] Force mode: unregistering existing file
|
||||
[REGISTER] Unregistering existing file...
|
||||
[REGISTER] Deleted 7 from processor_results
|
||||
[REGISTER] Deleted 691 from pre_chunks
|
||||
[REGISTER] Extracting metadata...
|
||||
[REGISTER] Inserting into database...
|
||||
[REGISTER] Registered: b496a68800f90770aa7246c877f38985
|
||||
[REGISTER] Done
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### 3. 詳細輸出
|
||||
|
||||
```bash
|
||||
rust_register /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4 --verbose
|
||||
```
|
||||
|
||||
輸出:
|
||||
```
|
||||
[REGISTER] Starting for: /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4
|
||||
[REGISTER] Force: false
|
||||
[REGISTER] Auto pipeline: false
|
||||
[REGISTER] Canonical path: /Users/accusys/momentry/var/sftpgo/data/demo/020.mp4
|
||||
[REGISTER] File name: 020.mp4
|
||||
[REGISTER] Birthday: 2026-09-23T00:54:07+00:00
|
||||
[REGISTER] File UUID: b496a68800f90770aa7246c877f38985
|
||||
[REGISTER] Content hash: 252851a1c...
|
||||
[REGISTER] File already registered: b496a68800f90770aa7246c877f38985
|
||||
[REGISTER] Use --force to overwrite
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 環境變量
|
||||
|
||||
工具依賴以下環境變量:
|
||||
|
||||
| 變量 | 說明 | 默認值 |
|
||||
|------|------|--------|
|
||||
| `DATABASE_URL` | PostgreSQL URL | `postgres://accusys@localhost:5432/momentry` |
|
||||
| `DATABASE_SCHEMA` | Schema | `public` |
|
||||
| `MOMENTRY_OUTPUT_DIR` | Output 目錄 | `/Users/accusys/momentry/output` |
|
||||
|
||||
---
|
||||
|
||||
## 安裝位置
|
||||
|
||||
**Binary**:
|
||||
```
|
||||
/Users/accusys/momentry_core/target/debug/rust_register
|
||||
```
|
||||
|
||||
**Source**:
|
||||
```
|
||||
/Users/accusys/momentry_core/src/bin/rust_register.rs
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 與 API register 的區別
|
||||
|
||||
| 特性 | API `/api/v1/register` | `rust_register` |
|
||||
|------|----------------------|----------------|
|
||||
| 執行方式 | 異步(通過 Worker) | 同步(直接執行) |
|
||||
| 依賴 | 需要 API Server + Worker | 獨立運行 |
|
||||
| 速度 | 較慢(排隊等待) | 較快(直接執行) |
|
||||
| 適用場景 | 生產環境 | 開發、測試、批量操作 |
|
||||
| 錯誤處理 | Worker 日誌 | 終端輸出 |
|
||||
| Pipeline 觸發 | 自動 | 需要手動調用 |
|
||||
|
||||
---
|
||||
|
||||
## 性能
|
||||
|
||||
### 測試結果
|
||||
|
||||
**文件**: 020.mp4 (14.2M, 528 frames)
|
||||
|
||||
**執行時間**: ~1秒
|
||||
|
||||
**提取元數據**:
|
||||
- Duration: 8.80s
|
||||
- Resolution: 1920x1080
|
||||
- FPS: 59.94
|
||||
- Total frames: 527
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 注意事項
|
||||
|
||||
### Force 模式
|
||||
|
||||
- **⚠️ 會刪除所有相關數據**
|
||||
- **⚠️ 不可恢復**
|
||||
- **⚠️ 建議先備份**
|
||||
|
||||
---
|
||||
|
||||
### 依賴
|
||||
|
||||
- 需要 ffprobe(用於元數據提取)
|
||||
- 需要 PostgreSQL 服務
|
||||
- 需要數據庫寫權限
|
||||
|
||||
---
|
||||
|
||||
## 錯誤處理
|
||||
|
||||
### 常見錯誤
|
||||
|
||||
#### 1. 文件不存在
|
||||
|
||||
```
|
||||
Error: File not found: /path/to/video.mp4
|
||||
```
|
||||
|
||||
解決:
|
||||
- 檢查文件路徑是否正確
|
||||
|
||||
---
|
||||
|
||||
#### 2. 文件已註冊
|
||||
|
||||
```
|
||||
[REGISTER] File already registered: b496a688...
|
||||
[REGISTER] Use --force to overwrite
|
||||
```
|
||||
|
||||
解決:
|
||||
- 使用 `--force` 重新註冊
|
||||
|
||||
---
|
||||
|
||||
#### 3. 數據庫連接失敗
|
||||
|
||||
```
|
||||
Error: Failed to connect to database
|
||||
```
|
||||
|
||||
解決:
|
||||
- 檢查 `DATABASE_URL`
|
||||
- 確認 PostgreSQL 服務運行中
|
||||
|
||||
---
|
||||
|
||||
## 批量操作
|
||||
|
||||
### 註冊多個文件
|
||||
|
||||
```bash
|
||||
# 創建批量腳本
|
||||
cat > /tmp/batch_register.sh << 'SCRIPT'
|
||||
#!/bin/bash
|
||||
|
||||
FILES=(
|
||||
"/path/to/video1.mp4"
|
||||
"/path/to/video2.mp4"
|
||||
"/path/to/video3.mp4"
|
||||
)
|
||||
|
||||
for file in "${FILES[@]}"; do
|
||||
echo "Registering: $file"
|
||||
rust_register "$file" --verbose
|
||||
done
|
||||
SCRIPT
|
||||
|
||||
chmod +x /tmp/batch_register.sh
|
||||
/tmp/batch_register.sh
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 相關工具
|
||||
|
||||
- `rust_unregister` - 取消註冊文件
|
||||
- `rust_register` - 註冊文件
|
||||
- API `/api/v1/process` - 觸發 pipeline
|
||||
|
||||
---
|
||||
|
||||
## 相關文檔
|
||||
|
||||
- `docs_v1.0/OPERATIONS/Rust_Unregister_CLI_Tool.md` - Unregister 工具
|
||||
- `docs_v1.0/API_WORKSPACE/modules/01_register_api.md` - API 文檔
|
||||
|
||||
---
|
||||
|
||||
## 版本歷史
|
||||
|
||||
| 版本 | 日期 | 變更 |
|
||||
|------|------|------|
|
||||
| 1.0 | 2026-09-28 | 初始版本 |
|
||||
|
||||
---
|
||||
|
||||
**狀態**: ✅ **生產就緒**
|
||||
Reference in New Issue
Block a user