Skip to content

Commit

Permalink
Add suport for Video Width / Height via FFprobe
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Dec 13, 2012
1 parent 7d49771 commit a70c84c
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions src/MediaVorus/Media/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace MediaVorus\Media;

use FFMpeg\Exception\Exception as FFMpegException;
use FFMpeg\Exception\ExceptionInterface as FFMpegException;
use FFMpeg\FFProbe;
use MediaVorus\File;
use PHPExiftool\Writer;
Expand All @@ -24,8 +24,13 @@
*/
class Video extends Image
{
/**
* @var FFProbe
*/
protected $ffprobe;
protected $duration;
private $duration;
private $width;
private $height;

public function __construct(File $file, FileEntity $entity, Writer $writer, FFProbe $ffprobe = null)
{
Expand All @@ -42,6 +47,68 @@ public function getType()
return self::TYPE_VIDEO;
}

public function getWidth()
{
if ($this->width) {
return $this->width;
}

if (null !== $result = parent::getWidth()) {
return $result;
}

if ($this->ffprobe) {
try {
$data = json_decode($this->ffprobe->probeStreams($this->file->getPathname()), true);
} catch (FFMpegException $e) {
$data = array();
}
} else {
$data = array();
}

foreach ($data as $stream) {
foreach ($stream as $key => $value) {
if ($key == 'width') {
return $this->width = (float) $value;
}
}
}

return null;
}

public function getHeight()
{
if ($this->height) {
return $this->height;
}

if (null !== $result = parent::getHeight()) {
return $result;
}

if ($this->ffprobe) {
try {
$data = json_decode($this->ffprobe->probeStreams($this->file->getPathname()), true);
} catch (FFMpegException $e) {
$data = array();
}
} else {
$data = array();
}

foreach ($data as $stream) {
foreach ($stream as $key => $value) {
if ($key == 'height') {
return $this->height = (float) $value;
}
}
}

return null;
}

/**
* Get the duration of the video in seconds, null if unavailable
*
Expand All @@ -60,7 +127,11 @@ public function getDuration()
}

if ($this->ffprobe) {
$result = json_decode($this->ffprobe->probeFormat($this->file->getPathname()), true);
try {
$result = json_decode($this->ffprobe->probeFormat($this->file->getPathname()), true);
} catch (FFMpegException $e) {
$result = array();
}
} else {
$result = array();
}
Expand Down

0 comments on commit a70c84c

Please sign in to comment.