2024-08-03 12:46:12 +08:00
|
|
|
|
import { OperateBookType } from "../../../define/enum/bookEnum";
|
|
|
|
|
|
import { Book } from "../../../model/book";
|
|
|
|
|
|
import { errorMessage, successMessage } from "../../Public/generalTools";
|
|
|
|
|
|
import { GeneralResponse } from "../../../model/generalResponse";
|
|
|
|
|
|
import { Setting } from '../../../main/setting/setting'
|
|
|
|
|
|
import path from 'path'
|
|
|
|
|
|
import { CheckFolderExistsOrCreate } from "../../../define/Tools/file";
|
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
|
import { ClipDraft } from '../../Public/clipDraft'
|
2024-08-18 16:22:19 +08:00
|
|
|
|
import { BookServiceBasic } from "../ServiceBasic/bookServiceBasic";
|
2024-10-23 22:59:29 +08:00
|
|
|
|
import { isEmpty } from "lodash";
|
|
|
|
|
|
import JianyingService from "../jianying/jianyingService";
|
2024-08-03 12:46:12 +08:00
|
|
|
|
|
|
|
|
|
|
export class BookVideo {
|
|
|
|
|
|
setting: Setting
|
2024-08-18 16:22:19 +08:00
|
|
|
|
bookServiceBasic: BookServiceBasic
|
2024-10-23 22:59:29 +08:00
|
|
|
|
jianyingService: JianyingService
|
2024-08-03 12:46:12 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
|
this.setting = new Setting(global)
|
2024-08-18 16:22:19 +08:00
|
|
|
|
this.bookServiceBasic = new BookServiceBasic()
|
2024-10-23 22:59:29 +08:00
|
|
|
|
this.jianyingService = new JianyingService()
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 将小说中的生成视频的数据,写道小说批次任务中
|
|
|
|
|
|
* @param id
|
|
|
|
|
|
* @param operateBookType
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UseBookVideoDataToBookTask(id: string, operateBookType: OperateBookType) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log(id, operateBookType)
|
|
|
|
|
|
let book = undefined as Book.SelectBook;
|
|
|
|
|
|
let bookTasks = undefined as Book.SelectBookTask[];
|
|
|
|
|
|
if (operateBookType == OperateBookType.BOOK) {
|
2024-10-23 22:59:29 +08:00
|
|
|
|
book = await this.bookServiceBasic.GetBookDataById(id);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
if (book == null) {
|
|
|
|
|
|
throw new Error('未找到对应的小说')
|
|
|
|
|
|
}
|
2024-10-23 22:59:29 +08:00
|
|
|
|
bookTasks = (await this.bookServiceBasic.GetBookTaskData({
|
2024-08-03 12:46:12 +08:00
|
|
|
|
bookId: id,
|
2024-10-23 22:59:29 +08:00
|
|
|
|
})).bookTasks;
|
2024-08-03 12:46:12 +08:00
|
|
|
|
} else if (operateBookType == OperateBookType.BOOKTASK) {
|
2024-10-23 22:59:29 +08:00
|
|
|
|
let bookTask = await this.bookServiceBasic.GetBookTaskDataById(id);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
if (bookTask == null) {
|
|
|
|
|
|
throw new Error('未找到对应的小说任务')
|
|
|
|
|
|
}
|
2024-10-23 22:59:29 +08:00
|
|
|
|
book = await this.bookServiceBasic.GetBookDataById(bookTask.bookId);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
if (book == null) {
|
|
|
|
|
|
throw new Error('未找到对应的小说')
|
|
|
|
|
|
}
|
|
|
|
|
|
bookTasks = [bookTask];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error("未知的操作类型");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (bookTasks.length <= 0) {
|
|
|
|
|
|
throw new Error("没有需要操作的小说数据,请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将修改数据放在一个事务中
|
2024-08-18 16:22:19 +08:00
|
|
|
|
for (let i = 0; i < bookTasks.length; i++) {
|
|
|
|
|
|
const element = bookTasks[i];
|
|
|
|
|
|
this.bookServiceBasic.UpdetedBookTaskData(element.id, {
|
|
|
|
|
|
backgroundMusic: book.backgroundMusic,
|
|
|
|
|
|
friendlyReminder: book.friendlyReminder,
|
|
|
|
|
|
draftSrtStyle: book.draftSrtStyle,
|
|
|
|
|
|
srtPath: book.srtPath,
|
|
|
|
|
|
audioPath: book.audioPath,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-08-03 12:46:12 +08:00
|
|
|
|
return successMessage({
|
|
|
|
|
|
backgroundMusic: book.backgroundMusic,
|
|
|
|
|
|
friendlyReminder: book.friendlyReminder,
|
|
|
|
|
|
draftSrtStyle: book.draftSrtStyle,
|
|
|
|
|
|
srtPath: book.srtPath,
|
|
|
|
|
|
audioPath: book.audioPath,
|
|
|
|
|
|
}, "将小说中的生成视频的数据,写到小说批次任务中成功", "BookTask_UseBookVideoDataToBookTask")
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return errorMessage('将小说中的生成视频的数据,写到小说批次任务中失败,错误信息如下:' + error.toString(), "BookTask_UseBookVideoDataToBookTask");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 生成配置文件,用于生成草稿或者是合成视频
|
|
|
|
|
|
* @param book 小说数据
|
|
|
|
|
|
* @param bookTask 对应的小说任务数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async GenerateConfigFile(book: Book.SelectBook, bookTask: Book.SelectBookTask): Promise<void> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 先修改通用设置
|
|
|
|
|
|
let saveProjectRes = await this.setting.ModifySampleSetting(JSON.stringify({
|
|
|
|
|
|
project_path: book.bookFolderPath,
|
|
|
|
|
|
project_name: book.name,
|
|
|
|
|
|
}))
|
|
|
|
|
|
if (saveProjectRes.code == 0) {
|
|
|
|
|
|
throw new Error("修改通用设置失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
// 开始生成配置文件
|
|
|
|
|
|
|
|
|
|
|
|
let configPath = path.join(book.bookFolderPath, "scripts/config.json");
|
|
|
|
|
|
await CheckFolderExistsOrCreate(path.dirname(configPath));
|
|
|
|
|
|
|
|
|
|
|
|
// 开始配置
|
2024-10-23 22:59:29 +08:00
|
|
|
|
let bookTaskDetail = await this.bookServiceBasic.GetBookTaskDetailData({
|
2024-08-03 12:46:12 +08:00
|
|
|
|
bookTaskId: bookTask.id
|
2024-10-23 22:59:29 +08:00
|
|
|
|
});
|
2024-08-03 12:46:12 +08:00
|
|
|
|
|
|
|
|
|
|
let configData = {
|
|
|
|
|
|
srt_time_information: [],
|
|
|
|
|
|
video_config: {
|
|
|
|
|
|
srt_path: bookTask.srtPath,
|
|
|
|
|
|
audio_path: bookTask.audioPath,
|
|
|
|
|
|
draft_srt_style: bookTask.draftSrtStyle ? bookTask.draftSrtStyle : "0",
|
|
|
|
|
|
background_music: bookTask.backgroundMusic,
|
|
|
|
|
|
friendly_reminder: bookTask.friendlyReminder ? bookTask.friendlyReminder : "0",
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < bookTaskDetail.length; i++) {
|
|
|
|
|
|
const element = bookTaskDetail[i];
|
|
|
|
|
|
let frameData = {
|
|
|
|
|
|
no: element.no,
|
|
|
|
|
|
id: element.id,
|
|
|
|
|
|
lastId: i == 0 ? '' : bookTaskDetail[i - 1].id,
|
|
|
|
|
|
word: element.word,
|
|
|
|
|
|
old_image: element.oldImage,
|
|
|
|
|
|
after_gpt: element.afterGpt,
|
|
|
|
|
|
start_time: element.startTime,
|
|
|
|
|
|
end_time: element.endTime,
|
|
|
|
|
|
timeLimit: `${element.startTime} -- ${element.endTime}`,
|
2024-10-23 22:59:29 +08:00
|
|
|
|
subValue: element.subValue,
|
2024-08-03 12:46:12 +08:00
|
|
|
|
character_tags: [],
|
|
|
|
|
|
gpt_prompt: element.gptPrompt,
|
|
|
|
|
|
mjMessage: element.mjMessage,
|
|
|
|
|
|
prompt_json: '',
|
|
|
|
|
|
name: element.name + '.png',
|
|
|
|
|
|
outImagePath: element.outImagePath,
|
|
|
|
|
|
subImagePath: element.subImagePath,
|
|
|
|
|
|
scene_tags: [],
|
|
|
|
|
|
imageLock: element.imageLock,
|
|
|
|
|
|
prompt: element.prompt
|
|
|
|
|
|
}
|
|
|
|
|
|
configData.srt_time_information.push(frameData)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 完毕,将数据写出
|
|
|
|
|
|
await fs.promises.writeFile(configPath, JSON.stringify(configData));
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 23:19:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 添加剪映草稿
|
|
|
|
|
|
* @param id
|
|
|
|
|
|
* @param operateBookType
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
2024-10-23 22:59:29 +08:00
|
|
|
|
async AddJianyingDraft(id: string | string[], operateBookType: OperateBookType): Promise<GeneralResponse.ErrorItem | GeneralResponse.SuccessItem> {
|
2024-08-03 12:46:12 +08:00
|
|
|
|
try {
|
|
|
|
|
|
console.log(id, operateBookType)
|
|
|
|
|
|
let book = undefined as Book.SelectBook
|
2024-10-23 22:59:29 +08:00
|
|
|
|
let bookTasks = [] as Book.SelectBookTask[]
|
|
|
|
|
|
if (operateBookType == OperateBookType.ASSIGNBOOKTASK) {
|
|
|
|
|
|
if (id.length <= 0) {
|
|
|
|
|
|
throw new Error("没有找到小说批次任务,请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
for (let i = 0; i < id.length; i++) {
|
|
|
|
|
|
const element = id[i];
|
|
|
|
|
|
let tempBookTask = await this.bookServiceBasic.GetBookTaskDataById(element as string);
|
|
|
|
|
|
bookTasks.push(tempBookTask)
|
|
|
|
|
|
book = await this.bookServiceBasic.GetBookDataById(tempBookTask.bookId);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (operateBookType == OperateBookType.BOOKTASK) {
|
|
|
|
|
|
// 直接获取对应的数据
|
2024-10-23 22:59:29 +08:00
|
|
|
|
let tempBookTask = await this.bookServiceBasic.GetBookTaskDataById(id as string);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
if (tempBookTask == null) {
|
|
|
|
|
|
throw new Error("没有找到小说批次任务,请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
bookTasks = [tempBookTask]
|
2024-10-23 22:59:29 +08:00
|
|
|
|
book = await this.bookServiceBasic.GetBookDataById(tempBookTask.bookId);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
if (book == null) {
|
|
|
|
|
|
throw new Error
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error("未知的操作类型");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (bookTasks.length <= 0) {
|
|
|
|
|
|
throw new Error("没有找到小说批次任务,请检查")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是不是生成单个,每次生成都要修改一下配置文件
|
|
|
|
|
|
// 调用生成草稿的方法
|
|
|
|
|
|
|
|
|
|
|
|
let result = []
|
|
|
|
|
|
for (let i = 0; i < bookTasks.length; i++) {
|
|
|
|
|
|
const element = bookTasks[i];
|
2024-10-23 22:59:29 +08:00
|
|
|
|
// 判断是不是又依赖的草稿,又依赖的草稿,对于依赖的草稿进行操作
|
|
|
|
|
|
|
|
|
|
|
|
if ((!isEmpty(element.draftDepend) && operateBookType != OperateBookType.ASSIGNBOOKTASK) || (operateBookType == OperateBookType.ASSIGNBOOKTASK && !isEmpty(book.draftDepend))) {
|
|
|
|
|
|
let draft_name = `${book.name}_${element.name}`;
|
|
|
|
|
|
if (draft_name == element.draftDepend) {
|
|
|
|
|
|
throw new Error("生成的草稿名称和依赖的草稿名称一样,请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
await this.jianyingService.GenerateDraftFromDepend(
|
|
|
|
|
|
operateBookType == OperateBookType.ASSIGNBOOKTASK ? book.draftDepend : element.draftDepend,
|
|
|
|
|
|
element.imageFolder, draft_name);
|
|
|
|
|
|
result.push(draft_name);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await this.GenerateConfigFile(book, element);
|
|
|
|
|
|
// 数据处理完毕,开始输出
|
|
|
|
|
|
let clipDraft = new ClipDraft(global, [element.name, {
|
|
|
|
|
|
srt_path: operateBookType == OperateBookType.ASSIGNBOOKTASK ? book.srtPath : element.srtPath,
|
|
|
|
|
|
audio_path: operateBookType == OperateBookType.ASSIGNBOOKTASK ? book.audioPath : element.audioPath,
|
|
|
|
|
|
draft_srt_style: operateBookType == OperateBookType.ASSIGNBOOKTASK ? (book.draftSrtStyle ? book.draftSrtStyle : '0') : (element.draftSrtStyle ? element.draftSrtStyle : "0"),
|
|
|
|
|
|
background_music: operateBookType == OperateBookType.ASSIGNBOOKTASK ? book.backgroundMusic : element.backgroundMusic,
|
|
|
|
|
|
friendly_reminder: operateBookType == OperateBookType.ASSIGNBOOKTASK ? (book.friendlyReminder ? book.bookFolderPath : '0') : (element.friendlyReminder ? element.friendlyReminder : "0"),
|
|
|
|
|
|
}])
|
|
|
|
|
|
let res = await clipDraft.addDraft();
|
|
|
|
|
|
if (res.code == 0) {
|
|
|
|
|
|
throw new Error(res.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
result.push(res.draft_name);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return successMessage(result, `${result.join('\n')} ${'\n'} 剪映草稿添加成功`, "BookTask_AddJianyingDraft")
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return errorMessage('添加剪映草稿失败,错误信息如下:' + error.toString(), "BookTask_AddJianyingDraft");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|