mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-14 09:01:42 -04:00
84 lines
2.8 KiB
PHP
84 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Http\Controllers\API;
|
|
|
|
use FFMpeg\FFMpeg;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\StudioPlus\Entities\Upload;
|
|
use Modules\StudioPlus\Transformers\UploadResource;
|
|
|
|
class VideoUploadController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$uploads = Upload::query()
|
|
->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']);
|
|
}
|
|
}
|