Created
December 25, 2020 18:42
-
-
Save rajeshtva/ff4e875a28e00d4e66470039b9efa765 to your computer and use it in GitHub Desktop.
rule to validate video length
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 FFMpeg\FFProbe; | |
use Illuminate\Contracts\Validation\Rule; | |
class VideoDurationValidation implements Rule | |
{ | |
public $min; | |
public $max; | |
public $duration; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct($min = 0, $max = PHP_INT_MAX) | |
{ | |
$this->min = $min; | |
$this->max = $max; | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
// ddd($attribute, $value, 'from validationrules') | |
$ffprobe = FFProbe::create([ | |
'ffmpeg.binaries' => '/usr/bin/ffmpeg', | |
'ffprobe.binaries' => '/usr/bin/ffprobe', | |
'timeout' => 3600, | |
'ffmpeg.threads' => 12, | |
]); | |
$duration = $ffprobe | |
->format($value->getRealPath()) // extracts file information | |
->get('duration'); | |
$this->duration = round($duration); | |
// ddd($this->duration); | |
// ddd($value, $ffprobe->format($value->getRealPath()), $attribute); | |
$minFlag = (round($duration) >= $this->min) ? true : false; | |
$maxFlag = (round($duration) <= $this->max) ? true : false; | |
return $minFlag and $maxFlag; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
if ($this->duration < $this->min) { | |
return "Video is smaller than $this->min minute. upload should be between $this->min-$this->max minute"; | |
} else if ($this->duration > $this->max) | |
return "video is larger than $this->max minute. upload should be between $this->min-$this->max minute"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment