Added watermark functionality and unit tests

This commit is contained in:
sylvainv 2014-07-28 17:27:12 +08:00
commit 475d1a508a
4 changed files with 157 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -0,0 +1,81 @@
<?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\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));
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace FFMpeg\Tests\Filters\Video;
use FFMpeg\FFProbe\DataMapping\Stream;
use FFMpeg\FFProbe\DataMapping\StreamCollection;
use FFMpeg\Filters\Video\RotateFilter;
use FFMpeg\Filters\Video\WatermarkFilter;
use FFMpeg\Tests\TestCase;
class WatermarkFilterTest extends TestCase
{
public function testApplyWatermark()
{
$stream = new Stream(array('width' => 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));
}
}

BIN
tests/files/waternark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB