Last active
June 11, 2022 05:56
-
-
Save elizov/6b030923a179d6a72758c0eae674a680 to your computer and use it in GitHub Desktop.
Laravel custom validation rules for videos. https://medium.com/@elizov/laravel-custom-validation-rules-for-videos-89cbc90c9d94
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
use FFMpeg\FFMpeg; | |
class MaxVideoBitRate implements Rule | |
{ | |
protected $maxBitRate; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct(int $maxBitRate) | |
{ | |
$this->maxBitRate = $maxBitRate; | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$video = FFMpeg::create()->open($value->getRealPath()); | |
$videoInfo = $video->getStreams()->videos()->first(); | |
return $this->maxBitRate >= $videoInfo->get('bit_rate'); | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'Exceeded the maximum allowed bitrate value.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment