lh 2 kuukautta sitten
vanhempi
commit
4b8433fdd0
2 muutettua tiedostoa jossa 187 lisäystä ja 1 poistoa
  1. 186 0
      app/Console/Test/TestCommand.php
  2. 1 1
      app/Services/Anime/AnimeService.php

+ 186 - 0
app/Console/Test/TestCommand.php

@@ -50,6 +50,11 @@ class TestCommand extends Command
      */
     public function handle()
     {
+
+        $this->handleHistoryTasks();
+        $this->handleHistorySegments();
+        dd('end!');
+
         // dd(testStoryboardParsing(''));
         $content = "";
         dd(handleScriptContent($content));
@@ -329,4 +334,185 @@ class TestCommand extends Command
         dd('成功!');
         // \Log::info('generate_json: '.$generate_json);
     }
+
+    /**
+     * 处理历史视频任务 - 压缩视频并更新 compressed_url
+     */
+    private function handleHistoryTasks()
+    {
+        $this->info('开始处理历史视频任务...');
+        
+        try {
+            // 查询所有 result_url 有值但 compressed_url 为空的成功任务
+            $tasks = DB::table('mp_generate_video_tasks')
+                ->where('status', 'success')
+                ->whereNotNull('result_url')
+                ->where('result_url', '!=', '')
+                ->where(function($query) {
+                    $query->whereNull('compressed_url')
+                          ->orWhere('compressed_url', '');
+                })
+                ->select('id', 'result_url')
+                ->get();
+            
+            $total = $tasks->count();
+            $this->info("找到 {$total} 个需要处理的任务");
+            
+            if ($total === 0) {
+                $this->info('没有需要处理的任务');
+                return;
+            }
+            
+            $successCount = 0;
+            $failCount = 0;
+            
+            foreach ($tasks as $index => $task) {
+                $taskId = $task->id;
+                $resultUrl = $task->result_url;
+                
+                $this->info("处理任务 [{$taskId}] ({$index + 1}/{$total})");
+                
+                try {
+                    // 压缩视频
+                    $compressedUrl = compressVideo($resultUrl);
+                    
+                    // 如果压缩失败,使用原始URL
+                    $finalUrl = $compressedUrl ?: $resultUrl;
+                    
+                    // 更新数据库
+                    $updated = DB::table('mp_generate_video_tasks')
+                        ->where('id', $taskId)
+                        ->update([
+                            'compressed_url' => $finalUrl,
+                            'updated_at' => date('Y-m-d H:i:s')
+                        ]);
+                    
+                    if ($updated) {
+                        $successCount++;
+                        if ($compressedUrl) {
+                            $this->info("✓ 任务 [{$taskId}] 压缩成功");
+                        } else {
+                            $this->warn("⚠ 任务 [{$taskId}] 压缩失败,使用原始URL");
+                        }
+                    } else {
+                        $failCount++;
+                        $this->error("✗ 任务 [{$taskId}] 更新失败");
+                    }
+                    
+                } catch (\Exception $e) {
+                    $failCount++;
+                    $this->error("✗ 任务 [{$taskId}] 处理异常: " . $e->getMessage());
+                    dLog('command')->error('处理历史视频任务异常', [
+                        'task_id' => $taskId,
+                        'error' => $e->getMessage()
+                    ]);
+                }
+                
+                // 每处理10个任务休息一下,避免压力过大
+                if (($index + 1) % 10 === 0) {
+                    sleep(1);
+                }
+            }
+            
+            $this->info("历史视频任务处理完成!");
+            $this->info("总数: {$total}, 成功: {$successCount}, 失败: {$failCount}");
+            
+        } catch (\Exception $e) {
+            $this->error('处理历史视频任务失败: ' . $e->getMessage());
+            dLog('command')->error('处理历史视频任务失败', ['error' => $e->getMessage()]);
+        }
+    }
+
+    /**
+     * 处理历史分镜数据 - 根据视频任务更新分镜表
+     */
+    private function handleHistorySegments()
+    {
+        $this->info('开始处理历史分镜数据...');
+        
+        try {
+            // 查询所有有 video_task_id 的分镜
+            $segments = DB::table('mp_episode_segments')
+                ->whereNotNull('video_task_id')
+                ->where('video_task_id', '>', 0)
+                ->select('segment_id', 'video_task_id')
+                ->get();
+            
+            $total = $segments->count();
+            $this->info("找到 {$total} 个需要处理的分镜");
+            
+            if ($total === 0) {
+                $this->info('没有需要处理的分镜');
+                return;
+            }
+            
+            $successCount = 0;
+            $skipCount = 0;
+            $failCount = 0;
+            
+            foreach ($segments as $index => $segment) {
+                $segmentId = $segment->segment_id;
+                $videoTaskId = $segment->video_task_id;
+                
+                $this->info("处理分镜 [{$segmentId}] ({$index + 1}/{$total})");
+                
+                try {
+                    // 查询对应的视频任务
+                    $videoTask = DB::table('mp_generate_video_tasks')
+                        ->where('id', $videoTaskId)
+                        ->where('status', 'success')
+                        ->whereNotNull('result_url')
+                        ->where('result_url', '!=', '')
+                        ->whereNotNull('compressed_url')
+                        ->where('compressed_url', '!=', '')
+                        ->select('result_url', 'compressed_url')
+                        ->first();
+                    
+                    if (!$videoTask) {
+                        $skipCount++;
+                        $this->warn("⊘ 分镜 [{$segmentId}] 对应的视频任务不存在或未完成");
+                        continue;
+                    }
+                    
+                    // 更新分镜表
+                    $updated = DB::table('mp_episode_segments')
+                        ->where('segment_id', $segmentId)
+                        ->update([
+                            'origin_video_url' => $videoTask->result_url,
+                            'video_url' => $videoTask->compressed_url,
+                            'updated_at' => date('Y-m-d H:i:s')
+                        ]);
+                    
+                    if ($updated) {
+                        $successCount++;
+                        $this->info("✓ 分镜 [{$segmentId}] 更新成功");
+                    } else {
+                        $failCount++;
+                        $this->error("✗ 分镜 [{$segmentId}] 更新失败");
+                    }
+                    
+                } catch (\Exception $e) {
+                    $failCount++;
+                    $this->error("✗ 分镜 [{$segmentId}] 处理异常: " . $e->getMessage());
+                    dLog('command')->error('处理历史分镜数据异常', [
+                        'segment_id' => $segmentId,
+                        'video_task_id' => $videoTaskId,
+                        'error' => $e->getMessage()
+                    ]);
+                }
+                
+                // 每处理10个分镜休息一下
+                if (($index + 1) % 10 === 0) {
+                    sleep(1);
+                }
+            }
+            
+            $this->info("历史分镜数据处理完成!");
+            $this->info("总数: {$total}, 成功: {$successCount}, 跳过: {$skipCount}, 失败: {$failCount}");
+            
+        } catch (\Exception $e) {
+            $this->error('处理历史分镜数据失败: ' . $e->getMessage());
+            dLog('command')->error('处理历史分镜数据失败', ['error' => $e->getMessage()]);
+        }
+    }
 }

+ 1 - 1
app/Services/Anime/AnimeService.php

@@ -863,7 +863,7 @@ class AnimeService
         // 获取历史视频
         $video_history = DB::table('mp_generate_video_tasks')->where('alias_segment_id', $segment_id)->where('status', 'success')->orderBy('created_at', 'desc')->get();
         foreach($video_history as $task) {
-            $result_url = getProp($task, 'result_url');
+            $result_url = getProp($task, 'compressed_url');
             if (is_json($result_url)) {
                 $url = array_merge($url, json_decode($result_url, true));
             }else {