GeneratePreviewVideo.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Libraries\CommonHelper;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Http\File;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Dcat\Admin\Traits\HasUploadedFile;
  10. use FFMpeg\FFMpeg;
  11. use FFMpeg\Coordinate\TimeCode;
  12. /*
  13. * 生成预览10秒视频
  14. *
  15. * @author <EMAIL>
  16. * 运行命令:php artisan command:preview-video
  17. */
  18. class GeneratePreviewVideo extends Command
  19. {
  20. use HasUploadedFile;
  21. /**
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'command:preview-video';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '生成预览视频';
  33. public $timeSecond = 5;
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $disk = $this->disk('oss');
  42. //如果目录不存在则创建
  43. $localPreviewPath = storage_path('tmp/previews');
  44. if (!file_exists($localPreviewPath)) {
  45. mkdir($localPreviewPath, 0777, true);
  46. }
  47. $reslut = DB::table('site_preview_video')
  48. ->where('status', '==', 0) // 过滤非空 video 字段
  49. ->orderBy('id') // 按主键排序确保顺序
  50. ->limit(1) // 限制处理 1 条
  51. ->get();
  52. foreach ($reslut as $item) {
  53. try {
  54. $video_oss_url = $this->ossUrl($item->video_url);
  55. $fileName = basename($video_oss_url);
  56. $fileInfo = pathinfo($fileName);
  57. //下载$video_url到本地
  58. $localFilePath = $localPreviewPath. '/'. $fileName;
  59. $this->saveLocal($video_oss_url, $localFilePath);
  60. $previewName = $fileInfo['filename'] . '_preview.' . $fileInfo['extension'];
  61. $previewPath = storage_path('tmp/previews/'.$previewName);
  62. //截取视频前5秒
  63. $this->generatePreview($localFilePath, $previewPath);
  64. //上传到OSS
  65. $uploadPath = 'videos/uploads/previews/'. date('Ym').'/';
  66. $file = new File($previewPath);
  67. $disk->putFileAs($uploadPath, $file, $previewName);
  68. DB::table('site_preview_video')
  69. ->where('id', $item->id)
  70. ->update([
  71. 'preview_url' => $uploadPath . '/' . $previewName,
  72. 'status' => 1
  73. ]);
  74. } catch (\Exception $e) {
  75. DB::table('site_preview_video')
  76. ->where('id', $item->id)
  77. ->update([
  78. 'remark' => $e->getMessage(),
  79. 'status' => -1
  80. ]);
  81. }
  82. }
  83. //$this->importVideo();
  84. return dd('success1');
  85. }
  86. private function saveLocal($url, $localPath) {
  87. $remote = fopen($url, 'rb');
  88. $local = fopen($localPath, 'wb');
  89. while (!feof($remote)) {
  90. fwrite($local, fread($remote, 8192)); // 8KB分块
  91. }
  92. fclose($remote);
  93. fclose($local);
  94. return $localPath;
  95. }
  96. private function ossUrl($url)
  97. {
  98. if (strpos($url, 'http:') === 0 || strpos($url, 'https:') === 0) {
  99. return $url;
  100. }
  101. return "https://mietublcom.oss-cn-hongkong.aliyuncs.com".'/'.$url;
  102. }
  103. public function generatePreview($inputUrl, $outputPath) {
  104. $ffmpeg = FFMpeg::create([
  105. 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
  106. 'ffprobe.binaries' => '/usr/bin/ffprobe',
  107. 'timeout' => 300
  108. ]);
  109. $video = $ffmpeg->open($inputUrl);
  110. $video->filters()->clip(TimeCode::fromSeconds(0), TimeCode::fromSeconds($this->timeSecond));
  111. // 输出为H.264编码的MP4
  112. $format = new \FFMpeg\Format\Video\X264();
  113. $video->save($format, $outputPath);
  114. return $outputPath;
  115. }
  116. /*ssssssss
  117. * 把旧数据导入新的预览表
  118. */
  119. function importVideo()
  120. {
  121. try {
  122. // 分批处理数据,避免内存溢出
  123. DB::table('site_album')
  124. ->where('video', '<>', '[]') // 过滤非空 video 字段
  125. ->orderBy('id') // 按主键排序确保顺序
  126. ->chunkById(10000, function ($albums) { // 每次处理 100 条
  127. $insertData = [];
  128. foreach ($albums as $album) {
  129. // 跳过无效 JSON 数据
  130. $videos = json_decode($album->video, true);
  131. if (json_last_error() !== JSON_ERROR_NONE || !is_array($videos)) {
  132. Log::warning("Invalid JSON in album ID: {$album->id}");
  133. continue;
  134. }
  135. foreach ($videos as $video) {
  136. // 校验必要字段存在性
  137. if (empty($video['video_src']) || empty($video['cover'])) {
  138. Log::warning("Missing video_src or cover in album ID: {$album->id}");
  139. continue;
  140. }
  141. // 处理 URL 长度限制
  142. $videoUrl = substr($video['video_src'], 0, 255);
  143. // 构建插入数据
  144. $insertData[] = [
  145. 'video_url' => $videoUrl,
  146. 'preview_url' => '',
  147. 'status' => 0,
  148. 'created_at' => Carbon::now(),
  149. 'updated_at' => Carbon::now(),
  150. ];
  151. }
  152. }
  153. // 插入剩余数据
  154. if (!empty($insertData)) {
  155. DB::table('site_preview_video')->insert($insertData);
  156. }
  157. });
  158. return true;
  159. } catch (\Exception $e) {
  160. Log::error("Video import failed: " . $e->getMessage());
  161. return false;
  162. }
  163. }
  164. }