Skip to content

Commit b280828

Browse files
authored
Merge pull request PHP-FFMpeg#268 from Romain/padding
Creation of a Pad filter which allows the user to resize his video, using black bars
2 parents bb029a5 + 4c0777b commit b280828

File tree

6 files changed

+141
-1
lines changed

6 files changed

+141
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
composer.phar
55
composer.lock
66
phpunit.xml
7+
sami.phar

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,28 @@ Resizes a video to a given size.
217217
$video->filters()->resize($dimension, $mode, $useStandards);
218218
```
219219

220-
The resize filter takes three parameters :
220+
The resize filter takes three parameters:
221221

222222
- `$dimension`, an instance of `FFMpeg\Coordinate\Dimension`
223223
- `$mode`, one of the constants `FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_*` constants
224224
- `$useStandards`, a boolean to force the use of the nearest aspect ratio standard.
225225

226+
If you want a video in a non-standard ratio, you can use the padding filter to resize your video in the desired size, and wrap it into black bars.
227+
228+
```php
229+
$video->filters()->pad($dimension);
230+
```
231+
232+
The pad filter takes one parameter:
233+
234+
- `$dimension`, an instance of `FFMpeg\Coordinate\Dimension`
235+
236+
Don't forget to save it afterwards.
237+
238+
```php
239+
$video->save(new FFMpeg\Format\Video\X264(), $new_file);
240+
```
241+
226242
###### Watermark
227243

228244
Watermark a video with a given image.

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"name": "Patrik Karisch",
2020
"email": "patrik@karisch.guru",
2121
"homepage": "http://www.karisch.guru"
22+
},
23+
{
24+
"name": "Romain Biard",
25+
"email": "romain.biard@gmail.com",
26+
"homepage": "https://www.strime.io/"
2227
}
2328
],
2429
"require": {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP-FFmpeg.
5+
*
6+
* (c) Strime <contact@strime.io>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FFMpeg\Filters\Video;
13+
14+
use FFMpeg\Coordinate\Dimension;
15+
use FFMpeg\Exception\RuntimeException;
16+
use FFMpeg\Media\Video;
17+
use FFMpeg\Format\VideoInterface;
18+
19+
class PadFilter implements VideoFilterInterface
20+
{
21+
/** @var Dimension */
22+
private $dimension;
23+
/** @var integer */
24+
private $priority;
25+
26+
public function __construct(Dimension $dimension, $priority = 0)
27+
{
28+
$this->dimension = $dimension;
29+
$this->priority = $priority;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getPriority()
36+
{
37+
return $this->priority;
38+
}
39+
40+
/**
41+
* @return Dimension
42+
*/
43+
public function getDimension()
44+
{
45+
return $this->dimension;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function apply(Video $video, VideoInterface $format)
52+
{
53+
$commands = array();
54+
55+
$commands[] = '-vf';
56+
$commands[] = 'scale=iw*min(' . $this->dimension->getWidth() . '/iw\,' . $this->dimension->getHeight() .'/ih):ih*min(' . $this->dimension->getWidth() . '/iw\,' . $this->dimension->getHeight() .'/ih),pad=' . $this->dimension->getWidth() . ':' . $this->dimension->getHeight() . ':(' . $this->dimension->getWidth() . '-iw)/2:(' . $this->dimension->getHeight() .'-ih)/2';
57+
58+
return $commands;
59+
}
60+
}

src/FFMpeg/Filters/Video/VideoFilters.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public function audioResample($rate)
9898
return $this;
9999
}
100100

101+
/**
102+
* Adds padding (black bars) to a video.
103+
*
104+
* @param Dimension $dimension
105+
*
106+
* @return VideoFilters
107+
*/
108+
public function pad(Dimension $dimension)
109+
{
110+
$this->media->addFilter(new PadFilter($dimension));
111+
112+
return $this;
113+
}
114+
101115
public function rotate($angle)
102116
{
103117
$this->media->addFilter(new RotateFilter($angle, 30));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\FFMpeg\Unit\Filters\Video;
4+
5+
use FFMpeg\Filters\Video\PadFilter;
6+
use Tests\FFMpeg\Unit\TestCase;
7+
use FFMpeg\FFProbe\DataMapping\Stream;
8+
use FFMpeg\FFProbe\DataMapping\StreamCollection;
9+
use FFMpeg\Coordinate\Dimension;
10+
11+
class PadFilterTest extends TestCase
12+
{
13+
/**
14+
* @dataProvider provideDimensions
15+
*/
16+
public function testApply(Dimension $dimension, $width, $height, $expected)
17+
{
18+
$video = $this->getVideoMock();
19+
$pathfile = '/path/to/file'.mt_rand();
20+
21+
$format = $this->getMock('FFMpeg\Format\VideoInterface');
22+
23+
$streams = new StreamCollection(array(
24+
new Stream(array(
25+
'codec_type' => 'video',
26+
'width' => $width,
27+
'height' => $height,
28+
))
29+
));
30+
31+
$filter = new PadFilter($dimension);
32+
$this->assertEquals($expected, $filter->apply($video, $format));
33+
}
34+
35+
public function provideDimensions()
36+
{
37+
return array(
38+
array(new Dimension(1000, 800), 640, 480, array('-vf', 'scale=iw*min(1000/iw\,800/ih):ih*min(1000/iw\,800/ih),pad=1000:800:(1000-iw)/2:(800-ih)/2')),
39+
array(new Dimension(300, 600), 640, 480, array('-vf', 'scale=iw*min(300/iw\,600/ih):ih*min(300/iw\,600/ih),pad=300:600:(300-iw)/2:(600-ih)/2')),
40+
array(new Dimension(100, 900), 640, 480, array('-vf', 'scale=iw*min(100/iw\,900/ih):ih*min(100/iw\,900/ih),pad=100:900:(100-iw)/2:(900-ih)/2')),
41+
array(new Dimension(1200, 200), 640, 480, array('-vf', 'scale=iw*min(1200/iw\,200/ih):ih*min(1200/iw\,200/ih),pad=1200:200:(1200-iw)/2:(200-ih)/2')),
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)