From 1d462b9eb365e07c52d55d50a4c67b9ec1f79784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Geli=C5=9F?= Date: Sun, 16 Nov 2014 20:45:28 +0200 Subject: [PATCH 1/4] add crop filter --- src/FFMpeg/Filters/Video/CropFilter.php | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/FFMpeg/Filters/Video/CropFilter.php diff --git a/src/FFMpeg/Filters/Video/CropFilter.php b/src/FFMpeg/Filters/Video/CropFilter.php new file mode 100644 index 0000000..70e3067 --- /dev/null +++ b/src/FFMpeg/Filters/Video/CropFilter.php @@ -0,0 +1,53 @@ + + * + * 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) + { + return array( + '-filter:v', + 'crop=' . + $this->dimension->getWidth() .':' . $this->dimension->getHeight() . ':' . $this->point->getX() . ':' . $this->point->getY() + ); + } +} From 6507d1d0217a26e5aa4a01ca804a4fa53141e5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Geli=C5=9F?= Date: Sun, 16 Nov 2014 20:54:00 +0200 Subject: [PATCH 2/4] add filter to the VideoFilters --- src/FFMpeg/Filters/Video/VideoFilters.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/FFMpeg/Filters/Video/VideoFilters.php b/src/FFMpeg/Filters/Video/VideoFilters.php index 55281e4..00a99d9 100644 --- a/src/FFMpeg/Filters/Video/VideoFilters.php +++ b/src/FFMpeg/Filters/Video/VideoFilters.php @@ -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 From b1778f0764f2066a70dfeeeda54d60c7a21d9153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Geli=C5=9F?= Date: Sun, 16 Nov 2014 21:28:05 +0200 Subject: [PATCH 3/4] update stream parameters --- src/FFMpeg/Filters/Video/CropFilter.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/FFMpeg/Filters/Video/CropFilter.php b/src/FFMpeg/Filters/Video/CropFilter.php index 70e3067..06b2b9c 100644 --- a/src/FFMpeg/Filters/Video/CropFilter.php +++ b/src/FFMpeg/Filters/Video/CropFilter.php @@ -44,6 +44,13 @@ class CropFilter implements VideoFilterInterface */ 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=' . From 6c39d07274c3c81980cd49f9db59e999a3ab17fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Geli=C5=9F?= Date: Sun, 16 Nov 2014 21:28:14 +0200 Subject: [PATCH 4/4] add tests --- .../Tests/Filters/Video/CropFilterTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/FFMpeg/Tests/Filters/Video/CropFilterTest.php diff --git a/tests/FFMpeg/Tests/Filters/Video/CropFilterTest.php b/tests/FFMpeg/Tests/Filters/Video/CropFilterTest.php new file mode 100644 index 0000000..140fe74 --- /dev/null +++ b/tests/FFMpeg/Tests/Filters/Video/CropFilterTest.php @@ -0,0 +1,40 @@ + 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')); + } + +}