Просмотр исходного кода

新增压缩视频的公共方法,同步调整更新视频时保存压缩视频

lh 2 месяцев назад
Родитель
Сommit
ab24e4ac2e

+ 2 - 0
app/Console/Commands/CheckVideoGenerationTasksCommand.php

@@ -107,6 +107,8 @@ class CheckVideoGenerationTasksCommand extends Command
                 if ($videoTask->status === 'success') {
                     $updateData['video_task_status'] = '已完成';
                     $updateData['video_url'] = $videoTask->result_url;
+                    $compressed_video_url = compressVideo($updateData['video_url']);
+                    if ($compressed_video_url) $updateData['preview_video_url'] = $compressed_video_url;
                     $updateData['current_type'] = 2;
                     
                     // 如果有尾帧图片,也更新

+ 23 - 1
app/Http/Controllers/Anime/AnimeController.php

@@ -481,6 +481,8 @@ class AnimeController extends BaseController
                                         'current_type' => 2,
                                         'updated_at' => $now
                                     ];
+                                    $compressed_video_url = compressVideo($segmentUpdateData['video_url']);
+                                    if ($compressed_video_url) $segmentUpdateData['preview_video_url'] = $compressed_video_url;
                                     
                                     // 只有当video_duration存在且大于0时才更新
                                     if (isset($statusResult['video_duration']) && $statusResult['video_duration'] > 0) {
@@ -937,6 +939,8 @@ class AnimeController extends BaseController
                                                 'current_type' => 2,
                                                 'updated_at' => $now
                                             ];
+                                            $compressed_video_url = compressVideo($segmentUpdateData['video_url']);
+                                            if ($compressed_video_url) $segmentUpdateData['preview_video_url'] = $compressed_video_url;
                                             
                                             // 只有当video_duration存在且大于0时才更新
                                             if (isset($statusResult['video_duration']) && $statusResult['video_duration'] > 0) {
@@ -1568,9 +1572,27 @@ class AnimeController extends BaseController
             ->where('episode_id', $episodeId)
             ->where('pic_task_id', '!=', '')
             ->orderBy('segment_number')
-            ->select('segment_id', 'segment_number', 'pic_task_id', 'pic_task_status', 'img_url', 'audio_url', 'dialogue',  'voice_type')
             ->get();
 
+        if (env('APP_ENV') == 'production') {   // 正式环境才压缩
+            // 循环判断分镜中preview_video_url是否存在,如果没有但video_url有值,则压缩video_url并赋值给preview_video_url
+            foreach ($segments as $segment) {
+                $segment = (array)$segment;
+                if ((empty($segment['preview_video_url']) || $segment['preview_video_url'] === '') 
+                    && !empty($segment['video_url']) && $segment['video_url'] !== '') {
+                    // 压缩视频
+                    $compressedUrl = compressVideo($segment['video_url'], 'videos');
+                    
+                    // 只有压缩成功才更新数据库
+                    if ($compressedUrl !== '') {
+                        DB::table('mp_episode_segments')
+                            ->where('id', $segment['id'])
+                            ->update(['preview_video_url' => $compressedUrl]);
+                    }
+                }
+            }
+        }
+
         // 判断是否有未完成的任务
         $uncompleted_pic_count = DB::table('mp_episode_segments')->where('anime_id', $animeId)
             ->where('episode_id', $episodeId)->where('pic_task_id', '<>', '')->whereNotIn('pic_task_status', ['已完成', '失败'])->count('id');

+ 7 - 7
app/Libs/Helpers.php

@@ -987,7 +987,7 @@ function filterIntro($content)
  * 
  * @param string $videoUrl 视频URL地址
  * @param string $prefix 上传文件夹前缀(如:'videos')
- * @return string 返回压缩后的视频URL,失败返回原URL
+ * @return string 返回压缩后的视频URL,失败返回空字符串
  */
 function compressVideo($videoUrl, $prefix = 'videos')
 {
@@ -1012,7 +1012,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
 
         if (!file_exists($inputFile)) {
             dLog('video_compress')->error('视频下载失败', ['url' => $videoUrl]);
-            return $videoUrl;
+            return '';
         }
 
         $inputFileSize = filesize($inputFile);
@@ -1030,7 +1030,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
         if ($duration <= 0) {
             dLog('video_compress')->error('无法获取视频时长', ['file' => $inputFile]);
             @unlink($inputFile);
-            return $videoUrl;
+            return '';
         }
 
         // 计算目标码率
@@ -1071,7 +1071,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
                 'output' => $output
             ]);
             @unlink($inputFile);
-            return $videoUrl;
+            return '';
         }
 
         $outputFileSize = filesize($outputFile);
@@ -1091,7 +1091,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
             dLog('video_compress')->error('无法打开压缩后的视频文件', ['file' => $outputFile]);
             @unlink($inputFile);
             @unlink($outputFile);
-            return $videoUrl;
+            return '';
         }
         
         $filename = $uniqueId . '.mp4';
@@ -1116,7 +1116,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
 
         if (!$compressedUrl) {
             dLog('video_compress')->error('压缩视频上传失败');
-            return $videoUrl;
+            return '';
         }
 
         dLog('video_compress')->info('压缩视频上传成功', [
@@ -1146,7 +1146,7 @@ function compressVideo($videoUrl, $prefix = 'videos')
             @unlink($outputFile);
         }
         
-        return $videoUrl; // 出错时返回原URL
+        return ''; // 出错时返回空字符串
     }
 }
 

+ 11 - 0
app/Services/AIGeneration/AIVideoGenerationService.php

@@ -336,6 +336,8 @@ class AIVideoGenerationService
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
                     ];
+                    $compressed_video_url = compressVideo($segmentUpdateData['video_url']);
+                    if ($compressed_video_url) $segmentUpdateData['preview_video_url'] = $compressed_video_url;
                     
                     // 只有当video_duration存在且大于0时才更新
                     if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {
@@ -598,6 +600,8 @@ class AIVideoGenerationService
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
                     ];
+                    $compressed_video_url = compressVideo($segmentUpdateData['video_url']);
+                    if ($compressed_video_url) $segmentUpdateData['preview_video_url'] = $compressed_video_url;
                     
                     // 只有当video_duration存在且大于0时才更新
                     if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {
@@ -884,6 +888,8 @@ class AIVideoGenerationService
                         $video_name = $video_name . $video_ext;
                         $url = uploadStreamByTos('video', file_get_contents($video_url), $video_name);
                         $updateData['result_url'] = $url;
+                        $compressed_video_url = compressVideo($updateData['result_url']);
+                        if ($compressed_video_url) $updateData['preview_video_url'] = $compressed_video_url;
                         
                         dLog('generate')->info('Seedance 视频保存成功: ' . $url);
                     } catch (\Exception $e) {
@@ -986,6 +992,9 @@ class AIVideoGenerationService
                 if (isset($updateData['result_url'])) {
                     $segmentUpdateData['video_url'] = $updateData['result_url'];
                 }
+                if (isset($updateData['preview_video_url'])) {
+                    $segmentUpdateData['preview_video_url'] = $updateData['preview_video_url'];
+                }
             } elseif ($taskStatus === MpGenerateVideoTask::STATUS_FAILED) {
                 $segmentUpdateData['video_task_status'] = '失败';
             } elseif ($taskStatus === MpGenerateVideoTask::STATUS_PROCESSING) {
@@ -1376,6 +1385,8 @@ class AIVideoGenerationService
                         'last_frame_url' => $statusInfo['last_frame_url'] ?? '',
                         'updated_at' => $now
                     ];
+                    $compressed_video_url = compressVideo($segmentUpdateData['video_url']);
+                    if ($compressed_video_url) $segmentUpdateData['preview_video_url'] = $compressed_video_url;
                     
                     // 只有当video_duration存在且大于0时才更新
                     if (isset($statusInfo['video_duration']) && $statusInfo['video_duration'] > 0) {

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

@@ -103,6 +103,7 @@ class AnimeService
                 'video_duration' => getProp($segment, 'video_duration', 0),
                 'video_time_point_start' => getProp($segment, 'video_time_point_start', 0),
                 'video_time_point_end' => getProp($segment, 'video_time_point_end', getProp($segment, 'video_duration', 0)),
+                'preview_video_url' => getProp($segment, 'preview_video_url'),
                 'last_frame_url' => getProp($segment, 'last_frame_url'),
                 'current_type' => getProp($segment, 'current_type'),
             ];