移除"从素材提取尾帧"功能(上游已实现类似功能)
- 移除 ExtractFrame handler 和路由 - 移除 AssetService 中的 ExtractFrameFromAsset 方法 - 移除 FFmpeg 中的 ExtractFrame 方法 - 移除前端 extractFrame API 和相关 UI Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -2,11 +2,8 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
models "github.com/drama-generator/backend/domain/models"
|
||||
"github.com/drama-generator/backend/infrastructure/external/ffmpeg"
|
||||
@@ -326,120 +323,3 @@ func (s *AssetService) ImportFromVideoGen(videoGenID uint) (*models.Asset, error
|
||||
|
||||
return asset, nil
|
||||
}
|
||||
|
||||
// ExtractFrameRequest 视频帧提取请求
|
||||
type ExtractFrameRequest struct {
|
||||
Position string `json:"position"` // "first" 或 "last"
|
||||
StoryboardID uint `json:"storyboard_id"` // 关联的分镜 ID
|
||||
FrameType string `json:"frame_type"` // image_generations 的帧类型,默认 "first"
|
||||
}
|
||||
|
||||
// ExtractFrameFromAsset 从视频素材中提取帧并保存到 image_generations 表
|
||||
func (s *AssetService) ExtractFrameFromAsset(assetID uint, req *ExtractFrameRequest) (*models.ImageGeneration, error) {
|
||||
// 获取素材
|
||||
var asset models.Asset
|
||||
if err := s.db.First(&asset, assetID).Error; err != nil {
|
||||
return nil, fmt.Errorf("asset not found: %w", err)
|
||||
}
|
||||
|
||||
// 验证是否为视频素材
|
||||
if asset.Type != models.AssetTypeVideo {
|
||||
return nil, fmt.Errorf("asset is not a video")
|
||||
}
|
||||
|
||||
// 默认值
|
||||
position := req.Position
|
||||
if position == "" {
|
||||
position = "last"
|
||||
}
|
||||
frameType := req.FrameType
|
||||
if frameType == "" {
|
||||
frameType = "first" // 提取的尾帧用作下一个分镜的首帧
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
fileName := fmt.Sprintf("frame_%s_%d_%s.jpg", position, assetID, timestamp)
|
||||
|
||||
// 创建临时目录
|
||||
tmpDir := "./data/tmp"
|
||||
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create temp directory: %w", err)
|
||||
}
|
||||
outputPath := filepath.Join(tmpDir, fileName)
|
||||
|
||||
// 使用 FFmpeg 提取帧
|
||||
_, err := s.ffmpeg.ExtractFrame(asset.URL, outputPath, position)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to extract frame: %w", err)
|
||||
}
|
||||
|
||||
var imageURL string
|
||||
|
||||
// 根据存储类型上传
|
||||
if s.ossStorage != nil {
|
||||
// 上传到 OSS
|
||||
url, err := s.ossStorage.UploadWithFilename(outputPath, "extracted_frames", fileName)
|
||||
if err != nil {
|
||||
s.log.Errorw("Failed to upload to OSS, falling back to local", "error", err)
|
||||
} else {
|
||||
imageURL = url
|
||||
s.log.Infow("Frame uploaded to OSS", "url", url)
|
||||
// 删除临时文件
|
||||
os.Remove(outputPath)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果 OSS 上传失败或未配置,使用本地存储
|
||||
if imageURL == "" {
|
||||
localPath := "./data/storage"
|
||||
baseURL := "/static"
|
||||
if s.cfg != nil && s.cfg.Storage.LocalPath != "" {
|
||||
localPath = s.cfg.Storage.LocalPath
|
||||
}
|
||||
if s.cfg != nil && s.cfg.Storage.BaseURL != "" {
|
||||
baseURL = s.cfg.Storage.BaseURL
|
||||
}
|
||||
|
||||
// 移动文件到最终位置
|
||||
finalDir := filepath.Join(localPath, "extracted_frames")
|
||||
if err := os.MkdirAll(finalDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create output directory: %w", err)
|
||||
}
|
||||
finalPath := filepath.Join(finalDir, fileName)
|
||||
if err := os.Rename(outputPath, finalPath); err != nil {
|
||||
return nil, fmt.Errorf("failed to move file: %w", err)
|
||||
}
|
||||
|
||||
imageURL = fmt.Sprintf("%s/extracted_frames/%s", baseURL, fileName)
|
||||
}
|
||||
|
||||
// 获取 DramaID
|
||||
var dramaID uint
|
||||
if asset.DramaID != nil {
|
||||
dramaID = *asset.DramaID
|
||||
}
|
||||
|
||||
// 创建 image_generation 记录
|
||||
imageGen := &models.ImageGeneration{
|
||||
DramaID: dramaID,
|
||||
StoryboardID: &req.StoryboardID,
|
||||
Prompt: fmt.Sprintf("Extracted %s frame from video asset #%d", position, assetID),
|
||||
Status: models.ImageStatusCompleted,
|
||||
ImageURL: &imageURL,
|
||||
FrameType: &frameType,
|
||||
}
|
||||
|
||||
if err := s.db.Create(imageGen).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to create image generation record: %w", err)
|
||||
}
|
||||
|
||||
s.log.Infow("Frame extracted and saved",
|
||||
"asset_id", assetID,
|
||||
"position", position,
|
||||
"image_gen_id", imageGen.ID,
|
||||
"frame_type", frameType,
|
||||
"image_url", imageURL)
|
||||
|
||||
return imageGen, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user