with(['thumbnails']) ->where('user_id', auth()->id()) ->latest() ->when( $request->filled('per_page'), fn ($query) => $query->paginate($request->per_page), fn ($query) => $query->paginate(15) ); return UploadResource::collection($uploads); } public function upload(Request $request) { $fileName = $request->file('video')->getClientOriginalName(); $explodedFileName = explode('.', $fileName); $title = collect(Arr::except($explodedFileName, array_key_last($explodedFileName)))->join(' '); $upload = Upload::create([ 'user_id' => auth()->id(), 'title' => $title, 'filename' => $fileName, ]); $folderPath = "public/videos/" . auth()->id() . "/upload-{$upload->id}"; if (!file_exists(storage_path("app/$folderPath"))) { Storage::makeDirectory($folderPath); } $filePath = Storage::put($folderPath, $request->video); $videoUrl = Storage::url($filePath); $upload->video_url = $videoUrl; $upload->save(); $ffmpeg = FFMpeg::create(); $video = $ffmpeg->open(url($videoUrl)); $ffprobe = \FFMpeg\FFProbe::create(get_ffprobe_config()); $codeAc = $ffprobe->format(url($videoUrl))->get('duration'); $timeFrameInSeconds = 2; for ($i = 0; $i < 3; $i++) { if ($timeFrameInSeconds < round($codeAc, PHP_ROUND_HALF_DOWN)) { $thumbnailFilename = Str::random(10) . ".jpg"; $thumbnailFilePath = "$folderPath/thumbnails/$thumbnailFilename"; $thumbnailFullPath = storage_path("app/$thumbnailFilePath"); if (!file_exists(storage_path("app/$folderPath/thumbnails"))) { Storage::makeDirectory("$folderPath/thumbnails"); } $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds($timeFrameInSeconds)) ->save($thumbnailFullPath); $upload->thumbnails()->create([ 'thumbnail_url' => Storage::url($thumbnailFilePath), 'filename' => $thumbnailFilename ]); $timeFrameInSeconds += 2; } } return $upload->load(['thumbnails']); } }