diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 54085e194f9..1a18bbb4869 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -576,6 +576,20 @@ public function height() return $this->dimensions()[1]; } + /** + * Get the asset's duration. + * + * @return float|null + */ + public function duration() + { + if (! $this->hasDuration()) { + return null; + } + + return $this->meta('duration'); + } + /** * Get the asset's orientation. * @@ -790,4 +804,9 @@ private function hasDimensions() { return $this->isImage() || $this->isSvg() || $this->isVideo(); } + + private function hasDuration() + { + return $this->isAudio() || $this->isVideo(); + } } diff --git a/src/Assets/AugmentedAsset.php b/src/Assets/AugmentedAsset.php index 8943c79d62b..44fdc6b81de 100644 --- a/src/Assets/AugmentedAsset.php +++ b/src/Assets/AugmentedAsset.php @@ -54,6 +54,11 @@ public function keys() 'orientation', 'ratio', 'mime_type', + 'duration', + 'duration_seconds', + 'duration_minutes', + 'duration_sec', + 'duration_min', ]); } @@ -134,4 +139,33 @@ protected function focusCss() { return Modify::value($this->get('focus'))->backgroundPosition()->fetch(); } + + protected function duration() + { + return round($this->data->duration()); + } + + protected function durationSeconds() + { + return $this->duration(); + } + + protected function durationSec() + { + return $this->duration(); + } + + protected function durationMinutes() + { + if (! $seconds = $this->duration()) { + return null; + } + + return floor($seconds / 60).':'.$seconds % 60; + } + + protected function durationMin() + { + return $this->durationMinutes(); + } }