123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace App\Console\Commands;
- use App\Libraries\CommonHelper;
- use Illuminate\Console\Command;
- use Illuminate\Http\File;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Dcat\Admin\Traits\HasUploadedFile;
- use FFMpeg\FFMpeg;
- use FFMpeg\Coordinate\TimeCode;
- class GeneratePreviewVideo extends Command
- {
- use HasUploadedFile;
-
- protected $signature = 'command:preview-video';
-
- protected $description = '生成预览视频';
- public $timeSecond = 5;
-
- public function handle()
- {
- $disk = $this->disk('oss');
-
- $localPreviewPath = storage_path('tmp/previews');
- if (!file_exists($localPreviewPath)) {
- mkdir($localPreviewPath, 0777, true);
- }
- $reslut = DB::table('site_preview_video')
- ->where('status', '==', 0)
- ->orderBy('id')
- ->limit(1)
- ->get();
- foreach ($reslut as $item) {
- try {
- $video_oss_url = $this->ossUrl($item->video_url);
- $fileName = basename($video_oss_url);
- $fileInfo = pathinfo($fileName);
-
- $localFilePath = $localPreviewPath. '/'. $fileName;
- $this->saveLocal($video_oss_url, $localFilePath);
- $previewName = $fileInfo['filename'] . '_preview.' . $fileInfo['extension'];
- $previewPath = storage_path('tmp/previews/'.$previewName);
-
- $this->generatePreview($localFilePath, $previewPath);
-
- $uploadPath = 'videos/uploads/previews/'. date('Ym');
- $file = new File($previewPath);
- $disk->putFileAs($uploadPath, $file, $previewName);
- DB::table('site_preview_video')
- ->where('id', $item->id)
- ->update([
- 'preview_url' => $uploadPath . '/' . $previewName,
- 'status' => 1
- ]);
-
- unlink($localFilePath);
- unlink($previewPath);
- } catch (\Exception $e) {
- DB::table('site_preview_video')
- ->where('id', $item->id)
- ->update([
- 'remark' => $e->getMessage(),
- 'status' => -1
- ]);
- }
- }
-
- return dd('success');
- }
- private function saveLocal($url, $localPath) {
- $remote = fopen($url, 'rb');
- $local = fopen($localPath, 'wb');
- while (!feof($remote)) {
- fwrite($local, fread($remote, 8192));
- }
- fclose($remote);
- fclose($local);
- return $localPath;
- }
- private function ossUrl($url)
- {
- if (strpos($url, 'http:') === 0 || strpos($url, 'https:') === 0) {
- return $url;
- }
- return "https://mietublcom.oss-cn-hongkong.aliyuncs.com".'/'.$url;
- }
- public function generatePreview($inputUrl, $outputPath) {
- $ffmpeg = FFMpeg::create([
- 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
- 'ffprobe.binaries' => '/usr/bin/ffprobe',
- 'timeout' => 300
- ]);
- $video = $ffmpeg->open($inputUrl);
- $video->filters()->clip(TimeCode::fromSeconds(0), TimeCode::fromSeconds($this->timeSecond));
-
- $format = new \FFMpeg\Format\Video\X264();
- $format->setAdditionalParameters([
- '-an'
- ]);
- $video->save($format, $outputPath);
- return $outputPath;
- }
-
- function importVideo()
- {
- try {
-
- DB::table('site_album')
- ->where('video', '<>', '[]')
- ->orderBy('id')
- ->chunkById(10000, function ($albums) {
- $insertData = [];
- foreach ($albums as $album) {
-
- $videos = json_decode($album->video, true);
- if (json_last_error() !== JSON_ERROR_NONE || !is_array($videos)) {
- Log::warning("Invalid JSON in album ID: {$album->id}");
- continue;
- }
- foreach ($videos as $video) {
-
- if (empty($video['video_src']) || empty($video['cover'])) {
- Log::warning("Missing video_src or cover in album ID: {$album->id}");
- continue;
- }
-
- $videoUrl = substr($video['video_src'], 0, 255);
-
- $insertData[] = [
- 'video_url' => $videoUrl,
- 'preview_url' => '',
- 'status' => 0,
- 'album_id' => $album->id,
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- ];
- }
- }
-
- if (!empty($insertData)) {
- DB::table('site_preview_video')->insert($insertData);
- }
- });
- return true;
- } catch (\Exception $e) {
- Log::error("Video import failed: " . $e->getMessage());
- return false;
- }
- }
- }
|