CheckVideoGenerationTasksCommand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\AIGeneration\AIVideoGenerationService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CheckVideoGenerationTasksCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'AIGeneration:checkVideoTasks';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '检查视频生成任务状态并更新结果';
  20. /**
  21. * Execute the console command.
  22. *
  23. * @param AIVideoGenerationService $videoGenerationService
  24. * @return int
  25. */
  26. public function handle(AIVideoGenerationService $videoGenerationService)
  27. {
  28. dLog('command')->info('开始检查视频生成任务状态...');
  29. // 执行50s
  30. $time_start = time();
  31. try {
  32. $count = DB::table('mp_generate_video_tasks')->where('status', 'processing')->count('id');
  33. while ($count > 0) {
  34. $time_diff = time() - $time_start;
  35. sleep(3);
  36. if ($time_diff > 50) break;
  37. $videoGenerationService->updatePendingTasks();
  38. $count = DB::table('mp_generate_video_tasks')->where('status', 'processing')->count('id');
  39. }
  40. // 更新分镜表中的视频任务状态
  41. $this->updateSegmentVideoStatus();
  42. dLog('command')->info('视频任务状态检查完成');
  43. } catch (\Exception $e) {
  44. dLog('command')->error('视频任务状态检查失败: ' . $e->getMessage());
  45. logDB('command', 'error', '定时任务:视频生成任务状态检查失败', ['error' => $e->getMessage()]);
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. /**
  51. * 更新分镜表中的视频任务状态
  52. *
  53. * @return void
  54. */
  55. private function updateSegmentVideoStatus()
  56. {
  57. try {
  58. // 获取所有状态为"生成中"的分镜
  59. $segments = DB::table('mp_episode_segments')
  60. ->where('video_task_status', '生成中')
  61. ->whereNotNull('video_task_id')
  62. ->where('video_task_id', '<>', '')
  63. ->select('segment_id', 'video_task_id')
  64. ->get();
  65. if ($segments->isEmpty()) {
  66. return;
  67. }
  68. dLog('command')->info('开始更新分镜视频任务状态', ['count' => $segments->count()]);
  69. $successCount = 0;
  70. $failedCount = 0;
  71. $processingCount = 0;
  72. foreach ($segments as $segment) {
  73. $videoTaskId = $segment->video_task_id;
  74. $segmentId = $segment->segment_id;
  75. // 查询视频生成任务状态
  76. $videoTask = DB::table('mp_generate_video_tasks')
  77. ->where('id', $videoTaskId)
  78. ->select('id', 'status', 'result_url', 'compressed_url', 'last_frame_url', 'error_message', 'result_json', 'extra_params', 'alias_segment_id', 'alias_act_id')
  79. ->first();
  80. if (!$videoTask) {
  81. dLog('command')->warning('分镜关联的视频任务不存在', [
  82. 'segment_id' => $segmentId,
  83. 'video_task_id' => $videoTaskId
  84. ]);
  85. continue;
  86. }
  87. $updateData = ['updated_at' => date('Y-m-d H:i:s')];
  88. // 根据任务状态更新分镜表
  89. if ($videoTask->status === 'success') {
  90. $updateData['video_task_status'] = '已完成';
  91. $updateData['origin_video_url'] = $videoTask->result_url;
  92. $updateData['video_url'] = $videoTask->compressed_url ?: $videoTask->result_url;
  93. $updateData['current_type'] = 2;
  94. // 如果有尾帧图片,也更新
  95. if (!empty($videoTask->last_frame_url)) {
  96. $updateData['last_frame_url'] = $videoTask->last_frame_url;
  97. }
  98. // 计算视频时长
  99. $video_duration = 0;
  100. if (!empty($videoTask->result_json)) {
  101. $resultJson = is_array($videoTask->result_json) ? $videoTask->result_json : json_decode($videoTask->result_json, true);
  102. if (isset($resultJson['duration'])) {
  103. // 如果直接返回duration,使用该值
  104. $video_duration = (int)$resultJson['duration'];
  105. } elseif (isset($resultJson['frames']) && isset($resultJson['framespersecond'])) {
  106. // 如果返回frames和framespersecond,计算时长
  107. $frames = (int)$resultJson['frames'];
  108. $fps = (int)$resultJson['framespersec8ond'];
  109. if ($fps > 0) {
  110. $video_duration = (int)floor($frames / $fps);
  111. }
  112. }
  113. }
  114. // 如果没有计算出时长,使用任务表中的设定值
  115. if ($video_duration <= 0 && !empty($videoTask->extra_params)) {
  116. $extraParams = is_array($videoTask->extra_params) ? $videoTask->extra_params : json_decode($videoTask->extra_params, true);
  117. if (isset($extraParams['duration'])) {
  118. $video_duration = (int)$extraParams['duration'];
  119. }
  120. }
  121. // 只有当video_duration > 0时才添加到更新数据中
  122. if ($video_duration > 0) {
  123. $updateData['video_duration'] = $video_duration;
  124. $updateData['video_time_point_start'] = 0;
  125. // $updateData['video_time_point_end'] = $video_duration;
  126. $updateData['video_time_point_end'] = $segment->audio_duration ?? 0;
  127. }
  128. $successCount++;
  129. // 更新分镜视频成功后新增对话记录
  130. $this->addVideoGenerationRecords($videoTask, $segmentId, $videoTask->result_url);
  131. dLog('command')->info('分镜视频生成成功', [
  132. 'segment_id' => $segmentId,
  133. 'video_task_id' => $videoTaskId,
  134. 'video_url' => $videoTask->result_url,
  135. 'video_duration' => $video_duration
  136. ]);
  137. } elseif ($videoTask->status === 'failed') {
  138. $updateData['video_task_status'] = '失败';
  139. $failedCount++;
  140. dLog('command')->error('分镜视频生成失败', [
  141. 'segment_id' => $segmentId,
  142. 'video_task_id' => $videoTaskId,
  143. 'error' => $videoTask->error_message
  144. ]);
  145. logDB('command', 'error', '分镜视频生成失败', [
  146. 'segment_id' => $segmentId,
  147. 'video_task_id' => $videoTaskId,
  148. 'error' => $videoTask->error_message
  149. ]);
  150. } else {
  151. // 仍在处理中,不更新
  152. $processingCount++;
  153. continue;
  154. }
  155. // 更新分镜表
  156. DB::table('mp_episode_segments')
  157. ->where('segment_id', $segmentId)
  158. ->update($updateData);
  159. }
  160. dLog('command')->info('分镜视频任务状态更新完成', [
  161. 'total' => $segments->count(),
  162. 'success' => $successCount,
  163. 'failed' => $failedCount,
  164. 'processing' => $processingCount
  165. ]);
  166. } catch (\Exception $e) {
  167. dLog('command')->error('更新分镜视频任务状态异常: ' . $e->getMessage());
  168. logDB('command', 'error', '更新分镜视频任务状态异常', [
  169. 'error' => $e->getMessage(),
  170. 'trace' => $e->getTraceAsString()
  171. ]);
  172. }
  173. }
  174. /**
  175. * 添加视频生成对话记录
  176. * @param array $task 任务数据
  177. * @param string|int $segmentIdOrActId segment_id(分镜模式)或 act_id(全能模式)
  178. * @param string $videoUrl 视频URL
  179. * @return void
  180. */
  181. private function addVideoGenerationRecords($task, $segmentIdOrActId, $videoUrl)
  182. {
  183. try {
  184. $segment_id = getProp($task, 'alias_segment_id');
  185. $act_id = getProp($task, 'alias_act_id');
  186. if ($segment_id) {
  187. // 分镜模式:使用segment_id查询
  188. $segment = DB::table('mp_episode_segments as a')->leftJoin('mp_animes as b', 'a.anime_id', 'b.id')
  189. ->where('a.segment_id', $segmentIdOrActId)
  190. ->select('b.user_id as uid', 'a.anime_id', 'a.episode_number', 'a.tail_frame', 'a.segment_id')
  191. ->first();
  192. } elseif ($act_id) {
  193. // 全能模式:使用id查询
  194. $segment = DB::table('mp_episode_segments as a')->leftJoin('mp_animes as b', 'a.anime_id', 'b.id')
  195. ->where('a.id', $segmentIdOrActId)
  196. ->select('b.user_id as uid', 'a.anime_id', 'a.episode_number', 'a.tail_frame', 'a.segment_id', 'a.id')
  197. ->first();
  198. } else {
  199. dLog('generate')->warning('无法确定查询模式,任务缺少segment_id和act_id', ['segment_id_or_act_id' => $segmentIdOrActId]);
  200. return;
  201. }
  202. if (!$segment) {
  203. dLog('generate')->warning('分镜信息不存在,无法添加对话记录', ['segment_id_or_act_id' => $segmentIdOrActId]);
  204. return;
  205. }
  206. $now = date('Y-m-d H:i:s');
  207. // 根据模式确定保存的字段和值
  208. if ($segment_id) {
  209. // 分镜模式:保存segment_id字段
  210. $recordField = 'segment_id';
  211. $recordValue = $segmentIdOrActId;
  212. $userContent = '图片转视频';
  213. $assistantContent = $segment->tail_frame ?: '';
  214. } else {
  215. // 全能模式:保存act_id字段
  216. $recordField = 'act_id';
  217. $recordValue = $segmentIdOrActId;
  218. $userContent = '片段转视频';
  219. $assistantContent = $segment->act_content ?: '';
  220. }
  221. // 保存对话记录
  222. $records = [
  223. [
  224. 'uid' => $segment->uid,
  225. 'anime_id' => $segment->anime_id,
  226. 'sequence' => $segment->episode_number,
  227. 'role' => 'user',
  228. 'content' => $userContent,
  229. $recordField => $recordValue,
  230. 'video_url' => '',
  231. 'created_at' => $now,
  232. 'updated_at' => $now
  233. ],
  234. [
  235. 'uid' => $segment->uid,
  236. 'anime_id' => $segment->anime_id,
  237. 'sequence' => $segment->episode_number,
  238. 'role' => 'assistant',
  239. 'content' => $assistantContent,
  240. $recordField => $recordValue,
  241. 'video_url' => $videoUrl,
  242. 'created_at' => $now,
  243. 'updated_at' => $now
  244. ]
  245. ];
  246. DB::table('mp_anime_records')->insert($records);
  247. dLog('generate')->info('视频生成对话记录添加成功', [
  248. 'segment_id_or_act_id' => $segmentIdOrActId,
  249. 'video_url' => $videoUrl,
  250. 'uid' => $segment->uid,
  251. 'anime_id' => $segment->anime_id
  252. ]);
  253. } catch (\Exception $e) {
  254. dLog('generate')->error('添加视频生成对话记录失败: ' . $e->getMessage(), [
  255. 'segment_id' => $segmentIdOrActId,
  256. 'video_url' => $videoUrl,
  257. 'error' => $e->getMessage()
  258. ]);
  259. }
  260. }
  261. }