diff --git a/src/FFMpeg/Filters/Video/VideoFilters.php b/src/FFMpeg/Filters/Video/VideoFilters.php index 88f6207..91c929d 100644 --- a/src/FFMpeg/Filters/Video/VideoFilters.php +++ b/src/FFMpeg/Filters/Video/VideoFilters.php @@ -103,4 +103,17 @@ class VideoFilters extends AudioFilters return $this; } + + /** + * @param string $imagePath + * @param array $coordinates + * + * @return $this + */ + public function watermark($imagePath, array $coordinates = array()) + { + $this->media->addFilter(new WatermarkFilter($imagePath, $coordinates)); + + return $this; + } } diff --git a/src/FFMpeg/Filters/Video/WatermarkFilter.php b/src/FFMpeg/Filters/Video/WatermarkFilter.php new file mode 100644 index 0000000..065f177 --- /dev/null +++ b/src/FFMpeg/Filters/Video/WatermarkFilter.php @@ -0,0 +1,81 @@ + + * + * 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\Format\VideoInterface; +use FFMpeg\Media\Video; +use FFMpeg\Coordinate\TimeCode; + +class WatermarkFilter implements VideoFilterInterface +{ + /** @var string */ + private $watermarkPath; + /** @var array */ + private $coordinates; + /** @var integer */ + private $priority; + + + public function __construct($watermarkPath, array $coordinates = array(), $priority = 0) + { + $this->watermarkPath = $watermarkPath; + $this->coordinates = $coordinates; + $this->priority = $priority; + } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return $this->priority; + } + + /** + * {@inheritdoc} + */ + public function apply(Video $video, VideoInterface $format) + { + $position = isset($this->coordinates['position']) ? $this->coordinates['position'] : 'absolute'; + + switch($position) { + case 'relative': + if (isset($this->coordinates['top'])) { + $y = $this->coordinates['top']; + } + elseif (isset($this->coordinates['bottom'])) { + $y = sprintf('main_h - %d - overlay_h', $this->coordinates['bottom']); + } + else { + $y = 0; + } + + if (isset($this->coordinates['left'])) { + $x = $this->coordinates['left']; + } + elseif (isset($this->coordinates['right'])) { + $x = sprintf('main_w - %d - overlay_w', $this->coordinates['right']); + } + else { + $x = 0; + } + + break; + default: + $x = isset($this->coordinates['x']) ? $this->coordinates['x'] : 0; + $y = isset($this->coordinates['y']) ? $this->coordinates['y'] : 0; + break; + } + + return array('-vf', sprintf('overlay %s:%s', $x, $y)); + } +} diff --git a/tests/FFMpeg/Tests/Filters/Video/WatermarkFilterTest.php b/tests/FFMpeg/Tests/Filters/Video/WatermarkFilterTest.php new file mode 100644 index 0000000..82fb3e1 --- /dev/null +++ b/tests/FFMpeg/Tests/Filters/Video/WatermarkFilterTest.php @@ -0,0 +1,63 @@ + 320, 'height' => 240, 'codec_type' => 'video')); + $streams = new StreamCollection(array($stream)); + + $video = $this->getVideoMock(); + + $format = $this->getMock('FFMpeg\Format\VideoInterface'); + + $filter = new WatermarkFilter(__DIR__ . '/../../files/watermark.png'); + $this->assertEquals(array('-vf', 'overlay 0:0'), $filter->apply($video, $format)); + + // check size of video is unchanged + $this->assertEquals(320, $stream->get('width')); + $this->assertEquals(240, $stream->get('height')); + } + + public function testDifferentCoordinaates() + { + $video = $this->getVideoMock(); + $format = $this->getMock('FFMpeg\Format\VideoInterface'); + + // test position absolute + $filter = new WatermarkFilter(__DIR__ . '/../../files/watermark.png', array( + 'position' => 'absolute', + 'x' => 10, 'y' => 5 + )); + $this->assertEquals(array('-vf', 'overlay 10:5'), $filter->apply($video, $format)); + + // test position relative + $filter = new WatermarkFilter(__DIR__ . '/../../files/watermark.png', array( + 'position' => 'relative', + 'bottom' => 10, 'left' => 5 + )); + $this->assertEquals(array('-vf', 'overlay 5:main_h - 10 - overlay_h'), $filter->apply($video, $format)); + + // test position relative + $filter = new WatermarkFilter(__DIR__ . '/../../files/watermark.png', array( + 'position' => 'relative', + 'bottom' => 5, 'right' => 4 + )); + $this->assertEquals(array('-vf', 'overlay main_w - 4 - overlay_w:main_h - 5 - overlay_h'), $filter->apply($video, $format)); + + // test position relative + $filter = new WatermarkFilter(__DIR__ . '/../../files/watermark.png', array( + 'position' => 'relative', + 'left' => 5, 'top' => 11 + )); + $this->assertEquals(array('-vf', 'overlay 5:11'), $filter->apply($video, $format)); + } +} diff --git a/tests/files/waternark.png b/tests/files/waternark.png new file mode 100644 index 0000000..0c96e54 Binary files /dev/null and b/tests/files/waternark.png differ