video-flow-b/app/service/usecase/ShotEditUsecase.ts
2025-08-07 19:39:21 +08:00

138 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { VideoSegmentEntity } from "../domain/Entities";
import { LensType } from "../domain/valueObject";
import {
getShotList,
regenerateShot,
optimizeShotContent,
} from "@/api/video_flow";
/**
* 视频片段编辑用例
* 负责视频片段内容的初始化、修改和优化
*/
export class VideoSegmentEditUseCase {
private loading: boolean = false;
private abortController: AbortController | null = null;
/**
* @description 获取视频片段列表
* @param projectId 项目ID
* @returns Promise<VideoSegmentEntity[]> 视频片段列表
*/
async getVideoSegmentList(projectId: string): Promise<VideoSegmentEntity[]> {
try {
this.loading = true;
const response = await getShotList({ projectId });
if (!response.successful) {
throw new Error(response.message || "获取视频片段列表失败");
}
return response.data || [];
} catch (error) {
console.error("获取视频片段列表失败:", error);
throw error;
} finally {
this.loading = false;
}
}
/**
* @description 通过视频镜头描述数据重新生成视频
* @param shotPrompt 镜头描述数据
* @param shotId 视频片段ID可选如果重新生成现有片段
* @param roleReplaceParams 角色替换参数(可选)
* @param sceneReplaceParams 场景替换参数(可选)
* @returns Promise<VideoSegmentEntity> 重新生成的视频片段
*/
async regenerateVideoSegment(
shotPrompt: LensType[],
shotId?: string,
roleReplaceParams?: { oldId: string; newId: string }[],
sceneReplaceParams?: { oldId: string; newId: string }[]
): Promise<VideoSegmentEntity> {
try {
this.loading = true;
// 创建新的中断控制器
this.abortController = new AbortController();
const response = await regenerateShot({
shotId,
shotPrompt,
roleReplaceParams,
sceneReplaceParams,
});
if (!response.successful) {
throw new Error(response.message || "重新生成视频片段失败");
}
return response.data;
} catch (error) {
if (this.abortController?.signal.aborted) {
console.log("视频片段重新生成被中断");
throw new Error("操作被中断");
}
console.error("重新生成视频片段失败:", error);
throw error;
} finally {
this.loading = false;
this.abortController = null;
}
}
/**
* @description 通过AI优化镜头数据包含对话内容
* @param shotId 视频片段ID
* @param userRequirement 用户优化需求
* @param lensData 镜头数据数组
* @returns Promise<LensType[]> 优化后的镜头数据
*/
async optimizeVideoContent(
shotId: string,
userRequirement: string,
lensData: LensType[]
): Promise<LensType[]> {
try {
this.loading = true;
// 调用AI优化接口
const response = await optimizeShotContent({
shotId,
userRequirement,
lensData,
});
if (!response.successful) {
throw new Error(response.message || "AI优化视频内容失败");
}
return response.data;
} catch (error) {
console.error("AI优化视频内容失败:", error);
throw error;
} finally {
this.loading = false;
}
}
/**
* @description 中断当前操作
*/
abortOperation(): void {
if (this.abortController) {
this.abortController.abort();
this.loading = false;
}
}
/**
* @description 获取加载状态
* @returns boolean 是否正在加载
*/
isLoading(): boolean {
return this.loading;
}
}