Merge pull request #134 from cangelis/cropfilter

Add Crop Filter
This commit is contained in:
Romain Neutron 2015-01-30 20:03:31 +01:00
commit 559708c0e4
3 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,60 @@
<?php
/*
* This file is part of PHP-FFmpeg.
*
* (c) Alchemy <dev.team@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FFMpeg\Filters\Video;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Coordinate\Point;
use FFMpeg\Format\VideoInterface;
use FFMpeg\Media\Video;
class CropFilter implements VideoFilterInterface
{
/** @var integer */
protected $priority;
/** @var Dimension */
protected $dimension;
/** @var Point */
protected $point;
public function __construct(Point $point, Dimension $dimension, $priority = 0)
{
$this->priority = $priority;
$this->dimension = $dimension;
$this->point = $point;
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
return $this->priority;
}
/**
* {@inheritdoc}
*/
public function apply(Video $video, VideoInterface $format)
{
foreach ($video->getStreams()->videos() as $stream) {
if ($stream->has('width') && $stream->has('height')) {
$stream->set('width', $this->dimension->getWidth());
$stream->set('height', $this->dimension->getHeight());
}
}
return array(
'-filter:v',
'crop=' .
$this->dimension->getWidth() .':' . $this->dimension->getHeight() . ':' . $this->point->getX() . ':' . $this->point->getY()
);
}
}

View file

@ -11,6 +11,7 @@
namespace FFMpeg\Filters\Video;
use FFMpeg\Coordinate\Point;
use FFMpeg\Media\Video;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\Coordinate\Dimension;
@ -104,6 +105,21 @@ class VideoFilters extends AudioFilters
return $this;
}
/**
* Crops the video
*
* @param Point $point
* @param Dimension $dimension
*
* @return VideoFilters
*/
public function crop(Point $point, Dimension $dimension)
{
$this->media->addFilter(new CropFilter($point, $dimension));
return $this;
}
/**
* @param string $imagePath
* @param array $coordinates

View file

@ -0,0 +1,40 @@
<?php
namespace FFMpeg\Tests\Filters\Video;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Coordinate\Point;
use FFMpeg\FFProbe\DataMapping\Stream;
use FFMpeg\FFProbe\DataMapping\StreamCollection;
use FFMpeg\Filters\Video\CropFilter;
use FFMpeg\Tests\TestCase;
class CropFilterTest extends TestCase
{
public function testCommandParamsAreCorrectAndStreamIsUpdated()
{
$stream = new Stream(array('width' => 320, 'height' => 240, 'codec_type' => 'video'));
$streams = new StreamCollection(array($stream));
$video = $this->getVideoMock();
$video->expects($this->once())
->method('getStreams')
->will($this->returnValue($streams));
$format = $this->getMock('FFMpeg\Format\VideoInterface');
$dimension = new Dimension(200, 150);
$point = new Point(25, 35);
$filter = new CropFilter($point, $dimension);
$expected = array(
'-filter:v',
'crop=' . $dimension->getWidth() . ":" . $dimension->getHeight() . ":" . $point->getX() . ":" . $point->getY()
);
$this->assertEquals($expected, $filter->apply($video, $format));
$this->assertEquals(200, $stream->get('width'));
$this->assertEquals(150, $stream->get('height'));
}
}