Skip to content

Instantly share code, notes, and snippets.

@rajeshtva
Created December 25, 2020 18:42
Show Gist options
  • Save rajeshtva/ff4e875a28e00d4e66470039b9efa765 to your computer and use it in GitHub Desktop.
Save rajeshtva/ff4e875a28e00d4e66470039b9efa765 to your computer and use it in GitHub Desktop.
rule to validate video length
<?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