CheckVideoGenerationTasksCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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', 'last_frame_url', 'error_message')
  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['video_url'] = $videoTask->result_url;
  92. $updateData['current_type'] = 2;
  93. // 如果有尾帧图片,也更新
  94. if (!empty($videoTask->last_frame_url)) {
  95. $updateData['last_frame_url'] = $videoTask->last_frame_url;
  96. }
  97. $successCount++;
  98. dLog('command')->info('分镜视频生成成功', [
  99. 'segment_id' => $segmentId,
  100. 'video_task_id' => $videoTaskId,
  101. 'video_url' => $videoTask->result_url
  102. ]);
  103. } elseif ($videoTask->status === 'failed') {
  104. $updateData['video_task_status'] = '失败';
  105. $failedCount++;
  106. dLog('command')->error('分镜视频生成失败', [
  107. 'segment_id' => $segmentId,
  108. 'video_task_id' => $videoTaskId,
  109. 'error' => $videoTask->error_message
  110. ]);
  111. logDB('command', 'error', '分镜视频生成失败', [
  112. 'segment_id' => $segmentId,
  113. 'video_task_id' => $videoTaskId,
  114. 'error' => $videoTask->error_message
  115. ]);
  116. } else {
  117. // 仍在处理中,不更新
  118. $processingCount++;
  119. continue;
  120. }
  121. // 更新分镜表
  122. DB::table('mp_episode_segments')
  123. ->where('segment_id', $segmentId)
  124. ->update($updateData);
  125. }
  126. dLog('command')->info('分镜视频任务状态更新完成', [
  127. 'total' => $segments->count(),
  128. 'success' => $successCount,
  129. 'failed' => $failedCount,
  130. 'processing' => $processingCount
  131. ]);
  132. } catch (\Exception $e) {
  133. dLog('command')->error('更新分镜视频任务状态异常: ' . $e->getMessage());
  134. logDB('command', 'error', '更新分镜视频任务状态异常', [
  135. 'error' => $e->getMessage(),
  136. 'trace' => $e->getTraceAsString()
  137. ]);
  138. }
  139. }
  140. }