Skip to content

Commit 98e80be

Browse files
committed
Support for filters
1 parent f0ca174 commit 98e80be

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This package provides an integration with FFmpeg for Laravel 5.1 and higher. The
1313
You can install the package via composer:
1414

1515
``` bash
16-
composer require pbmedia/laravel-ffmpeg dev-master
16+
composer require pbmedia/laravel-ffmpeg
1717
```
1818

1919
Add the service provider and facade to your ```app.php``` config file:
@@ -65,6 +65,33 @@ FFMpeg::fromDisk('videos')
6565
->save('FrameAt10sec.png');
6666
```
6767

68+
You can add filters through a ```Closure``` or by using PHP-FFMpeg's Filter objects:
69+
70+
``` php
71+
FFMpeg::fromDisk('videos')
72+
->open('steve_howe.mp4')
73+
->addFilter(function ($filters) {
74+
$filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
75+
})
76+
->export()
77+
->toDisk('converted_videos')
78+
->inFormat(new \FFMpeg\Format\Video\X264)
79+
->save('small_steve.mkv');
80+
81+
// or
82+
83+
$start = \FFMpeg\Coordinate\TimeCode::fromSeconds(5)
84+
$clipFilter = new \FFMpeg\Filters\Video\ClipFilter($start);
85+
86+
FFMpeg::fromDisk('videos')
87+
->open('steve_howe.mp4')
88+
->addFilter($clipFilter)
89+
->export()
90+
->toDisk('converted_videos')
91+
->inFormat(new \FFMpeg\Format\Video\X264)
92+
->save('short_steve.mkv');
93+
```
94+
6895
Chain multiple convertions:
6996

7097
``` php
@@ -77,19 +104,16 @@ FFMpeg::open('my_movie.mov')
77104
// export to FTP, converted in WMV
78105
->export()
79106
->toDisk('ftp')
80-
->inFormat(new FFMpeg\Format\Video\WMV)
107+
->inFormat(new \FFMpeg\Format\Video\WMV)
81108
->save('my_movie.wmv')
82109

83110
// export to Amazon S3, converted in X264
84111
->export()
85112
->toDisk('s3')
86-
->inFormat(new FFMpeg\Format\Video\X264)
113+
->inFormat(new \FFMpeg\Format\Video\X264)
87114
->save('my_movie.mkv');
88115
```
89116

90-
## To do
91-
* Writing test for exporting frames
92-
93117
## Changelog
94118

95119
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/Media.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pbmedia\LaravelFFMpeg;
44

5+
use Closure;
56
use FFMpeg\Coordinate\TimeCode;
67
use FFMpeg\Media\MediaTypeInterface;
78

@@ -56,6 +57,19 @@ public function getFrameFromTimecode(TimeCode $timecode): Frame
5657
return new Frame($this->getFile(), $frame);
5758
}
5859

60+
public function addFilter(): Media
61+
{
62+
$arguments = func_get_args();
63+
64+
if (isset($arguments[0]) && $arguments[0] instanceof Closure) {
65+
call_user_func_array($arguments[0], [$this->media->filters()]);
66+
} else {
67+
call_user_func_array([$this->media, 'addFilter'], $arguments);
68+
}
69+
70+
return $this;
71+
}
72+
5973
protected function selfOrArgument($argument)
6074
{
6175
return ($argument === $this->media) ? $this : $argument;

tests/AudioVideoTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Pbmedia\LaravelFFMpeg\Tests;
44

5+
use FFMpeg\Coordinate\Dimension;
6+
use FFMpeg\Coordinate\TimeCode;
7+
use FFMpeg\Filters\Video\ClipFilter;
58
use FFMpeg\Media\Video;
69
use Illuminate\Contracts\Filesystem\Factory as Filesystems;
710
use Mockery;
@@ -48,6 +51,30 @@ public function testDiskClass()
4851
$this->assertEquals($disk->getPath(), $this->srcDir . '/');
4952
}
5053

54+
public function testAddingAFilterWithAClosure()
55+
{
56+
$media = $this->getVideoMedia();
57+
58+
$this->assertCount(0, $media->getFiltersCollection());
59+
60+
$media->addFilter(function ($filters) {
61+
$filters->resize(new Dimension(640, 480));
62+
});
63+
64+
$this->assertCount(1, $media->getFiltersCollection());
65+
}
66+
67+
public function testAddingAFilterWithAnObject()
68+
{
69+
$media = $this->getVideoMedia();
70+
71+
$this->assertCount(0, $media->getFiltersCollection());
72+
73+
$media->addFilter(new ClipFilter(TimeCode::fromSeconds(5)));
74+
75+
$this->assertCount(1, $media->getFiltersCollection());
76+
}
77+
5178
public function testExportingToLocalDisk()
5279
{
5380
$media = $this->getGuitarMedia();

0 commit comments

Comments
 (0)