ソースを参照

视频任务完成时更新视频时长字段

lh 2 ヶ月 前
コミット
e4e4ed1adc
1 ファイル変更113 行追加10 行削除
  1. 113 10
      app/Services/AIGeneration/AIVideoGenerationService.php

+ 113 - 10
app/Services/AIGeneration/AIVideoGenerationService.php

@@ -235,6 +235,31 @@ class AIVideoGenerationService
                     $returnData['result_url'] = $url;
                 }
                 
+                // 计算视频时长
+                $video_duration = 0;
+                if (isset($result['duration'])) {
+                    $video_duration = (int)$result['duration'];
+                } elseif (isset($result['frames']) && isset($result['framespersecond'])) {
+                    $frames = (int)$result['frames'];
+                    $fps = (int)$result['framespersecond'];
+                    if ($fps > 0) {
+                        $video_duration = (int)floor($frames / $fps);
+                    }
+                }
+                
+                // 如果没有计算出时长,使用任务表中的设定值
+                if ($video_duration <= 0 && !empty($task->extra_params)) {
+                    $extraParams = is_array($task->extra_params) ? $task->extra_params : json_decode($task->extra_params, true);
+                    if (isset($extraParams['duration'])) {
+                        $video_duration = (int)$extraParams['duration'];
+                    }
+                }
+                
+                // 只有当video_duration > 0时才添加到返回数据中
+                if ($video_duration > 0) {
+                    $returnData['video_duration'] = $video_duration;
+                }
+                
                 $returnData['status'] = 'success';
             } elseif (in_array($taskStatus, ['not_found', 'expired'])) {
                 $returnData['status'] = 'failed';
@@ -301,13 +326,21 @@ class AIVideoGenerationService
                     
                     $now = date('Y-m-d H:i:s');
                     
-                    // 更新分镜表
-                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update([
+                    // 构建更新数据
+                    $segmentUpdateData = [
                         'video_url' => $statusInfo['result_url'],
                         'video_task_status' => '已完成',
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
-                    ]);
+                    ];
+                    
+                    // 只有当video_duration存在且大于0时才更新
+                    if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {
+                        $segmentUpdateData['video_duration'] = $statusInfo['video_duration'];
+                    }
+                    
+                    // 更新分镜表
+                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update($segmentUpdateData);
                     
                     DB::commit();
                     
@@ -530,7 +563,7 @@ class AIVideoGenerationService
         }
     }
 
-    // 更新即梦AI视频任务状态
+    // 更新Seedance视频任务状态
     private function updateSeedanceTask($task) {
         $statusInfo = $this->querySeedanceTaskStatus($task);
         if (!isset($statusInfo['status'])) return;
@@ -547,13 +580,21 @@ class AIVideoGenerationService
                     
                     $now = date('Y-m-d H:i:s');
                     
-                    // 更新分镜表
-                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update([
+                    // 构建更新数据
+                    $segmentUpdateData = [
                         'video_url' => $statusInfo['result_url'],
                         'video_task_status' => '已完成',
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
-                    ]);
+                    ];
+                    
+                    // 只有当video_duration存在且大于0时才更新
+                    if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {
+                        $segmentUpdateData['video_duration'] = $statusInfo['video_duration'];
+                    }
+                    
+                    // 更新分镜表
+                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update($segmentUpdateData);
                     
                     DB::commit();
                     
@@ -683,6 +724,33 @@ class AIVideoGenerationService
                     $updateData['result_url'] = $url;
                 }
                 
+                // 计算视频时长
+                $video_duration = 0;
+                if (isset($responseData['duration'])) {
+                    // 如果直接返回duration,使用该值
+                    $video_duration = (int)$responseData['duration'];
+                } elseif (isset($responseData['frames']) && isset($responseData['framespersecond'])) {
+                    // 如果返回frames和framespersecond,计算时长
+                    $frames = (int)$responseData['frames'];
+                    $fps = (int)$responseData['framespersecond'];
+                    if ($fps > 0) {
+                        $video_duration = (int)floor($frames / $fps);
+                    }
+                }
+                
+                // 如果没有计算出时长,使用任务表中的设定值
+                if ($video_duration <= 0 && !empty($task->extra_params)) {
+                    $extraParams = is_array($task->extra_params) ? $task->extra_params : json_decode($task->extra_params, true);
+                    if (isset($extraParams['duration'])) {
+                        $video_duration = (int)$extraParams['duration'];
+                    }
+                }
+                
+                // 只有当video_duration > 0时才添加到更新数据中
+                if ($video_duration > 0) {
+                    $updateData['video_duration'] = $video_duration;
+                }
+                
                 // 处理尾帧图片(如果启用了 return_last_frame)
                 if (isset($responseData['content']['last_frame_url']) && !empty($responseData['content']['last_frame_url'])) {
                     $pic_url = $responseData['content']['last_frame_url'];
@@ -1173,6 +1241,33 @@ class AIVideoGenerationService
                     $video_name = $video_name . $video_ext;
                     $url = uploadStreamByTos('video', file_get_contents($video_url), $video_name);
                     $updateData['result_url'] = $url;
+                    
+                    // 计算视频时长
+                    $video_duration = 0;
+                    if (isset($video['duration'])) {
+                        $video_duration = (int)$video['duration'];
+                    } elseif (isset($taskData['duration'])) {
+                        $video_duration = (int)$taskData['duration'];
+                    } elseif (isset($video['frames']) && isset($video['framespersecond'])) {
+                        $frames = (int)$video['frames'];
+                        $fps = (int)$video['framespersecond'];
+                        if ($fps > 0) {
+                            $video_duration = (int)floor($frames / $fps);
+                        }
+                    }
+                    
+                    // 如果没有计算出时长,使用任务表中的设定值
+                    if ($video_duration <= 0 && !empty($task->extra_params)) {
+                        $extraParams = is_array($task->extra_params) ? $task->extra_params : json_decode($task->extra_params, true);
+                        if (isset($extraParams['duration'])) {
+                            $video_duration = (int)$extraParams['duration'];
+                        }
+                    }
+                    
+                    // 只有当video_duration > 0时才添加到更新数据中
+                    if ($video_duration > 0) {
+                        $updateData['video_duration'] = $video_duration;
+                    }
                 }
                 return $updateData;
             } elseif ($taskStatus === 'failed') {
@@ -1251,13 +1346,21 @@ class AIVideoGenerationService
                     
                     $now = date('Y-m-d H:i:s');
                     
-                    // 更新分镜表
-                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update([
+                    // 构建更新数据
+                    $segmentUpdateData = [
                         'video_url' => $statusInfo['result_url'],
                         'video_task_status' => '已完成',
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
-                    ]);
+                    ];
+                    
+                    // 只有当video_duration存在且大于0时才更新
+                    if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {
+                        $segmentUpdateData['video_duration'] = $statusInfo['video_duration'];
+                    }
+                    
+                    // 更新分镜表
+                    DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update($segmentUpdateData);
                     
                     DB::commit();