Skip to content

Commit 67aa0be

Browse files
committed
Option to disable format sorting in HLS exporter.
1 parent 5dc1e51 commit 67aa0be

File tree

4 files changed

+119
-4
lines changed

4 files changed

+119
-4
lines changed

CHANGELOG.md

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

33
All Notable changes to `pbmedia/laravel-ffmpeg` will be documented in this file
44

5+
## 2.1.0 - 2018-04-10
6+
7+
### Added
8+
- Option to disable format sorting in HLS exporter.
9+
10+
### Deprecated
11+
- Nothing
12+
13+
### Fixed
14+
- Nothing
15+
16+
### Removed
17+
- Nothing
18+
19+
### Security
20+
- Nothing
21+
22+
## 2.0.1 - 2018-02-30
23+
24+
### Added
25+
- Nothing
26+
27+
### Deprecated
28+
- Nothing
29+
30+
### Fixed
31+
- Symfony 4.0 workaround
32+
33+
### Removed
34+
- Nothing
35+
36+
### Security
37+
- Nothing
38+
39+
## 2.0.0 - 2018-02-19
40+
41+
### Added
42+
- Support for Laravel 5.6.
43+
44+
### Deprecated
45+
- Nothing
46+
47+
### Fixed
48+
- Nothing
49+
50+
### Removed
51+
- Support for Laravel 5.5 and earlier.
52+
53+
### Security
54+
- Nothing
55+
556
## 1.3.0 - 2017-11-13
657

758
### Added

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ $exporter = FFMpeg::open('steve_howe.mp4')
235235
});
236236
```
237237

238+
As of version 2.1.0 you can disable the sorting of the added formats as most players choose the first format as the default one.
239+
240+
``` php
241+
$exporter = FFMpeg::open('steve_howe.mp4')
242+
->exportForHLS()
243+
->dontSortFormats();
244+
```
245+
238246
## Advanced
239247

240248
The Media object you get when you 'open' a file, actually holds the Media object that belongs to the [underlying driver](https://github.com/PHP-FFMpeg/PHP-FFMpeg). It handles dynamic method calls as you can see [here](https://github.com/pascalbaljetmedia/laravel-ffmpeg/blob/master/src/Media.php#L114-L117). This way all methods of the underlying driver are still available to you.

src/HLSPlaylistExporter.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class HLSPlaylistExporter extends MediaExporter
1717

1818
protected $progressCallback;
1919

20+
protected $sortFormats = true;
21+
2022
public function addFormat(VideoInterface $format, callable $callback = null): MediaExporter
2123
{
2224
$segmentedExporter = $this->getSegmentedExporterFromFormat($format);
@@ -30,6 +32,13 @@ public function addFormat(VideoInterface $format, callable $callback = null): Me
3032
return $this;
3133
}
3234

35+
public function dontSortFormats()
36+
{
37+
$this->sortFormats = false;
38+
39+
return $this;
40+
}
41+
3342
public function getFormatsSorted(): array
3443
{
3544
return array_map(function ($exporter) {
@@ -39,9 +48,11 @@ public function getFormatsSorted(): array
3948

4049
public function getSegmentedExportersSorted(): array
4150
{
42-
usort($this->segmentedExporters, function ($exportedA, $exportedB) {
43-
return $exportedA->getFormat()->getKiloBitrate() <=> $exportedB->getFormat()->getKiloBitrate();
44-
});
51+
if ($this->sortFormats) {
52+
usort($this->segmentedExporters, function ($exportedA, $exportedB) {
53+
return $exportedA->getFormat()->getKiloBitrate() <=> $exportedB->getFormat()->getKiloBitrate();
54+
});
55+
}
4556

4657
return $this->segmentedExporters;
4758
}
@@ -121,7 +132,9 @@ protected function getMasterPlaylistContents(): string
121132
{
122133
$lines = ['#EXTM3U'];
123134

124-
foreach ($this->getSegmentedExportersSorted() as $segmentedExporter) {
135+
$segmentedExporters = $this->sortFormats ? $this->getSegmentedExportersSorted() : $this->getSegmentedExporters();
136+
137+
foreach ($segmentedExporters as $segmentedExporter) {
125138
$bitrate = $segmentedExporter->getFormat()->getKiloBitrate() * 1000;
126139

127140
$lines[] = '#EXT-X-STREAM-INF:BANDWIDTH=' . $bitrate;

tests/HLSPlaylistExporterTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,47 @@ public function testCreationOfPlaylist()
180180

181181
@unlink($this->srcDir . '/MyPlaylist.m3u8');
182182
}
183+
184+
public function testDisableSortingOfFormats()
185+
{
186+
$file = $this->getVideoMedia()->getFile();
187+
188+
$media = Mockery::mock(Media::class);
189+
$media->shouldReceive('getFile')->once()->andReturn($file);
190+
191+
$playlist = 'MyPlaylist.m3u8';
192+
193+
$formatA = (new \FFMpeg\Format\Video\X264)->setKiloBitrate(384);
194+
$formatB = (new \FFMpeg\Format\Video\X264)->setKiloBitrate(512);
195+
196+
$media->shouldReceive('addFilter')->once();
197+
198+
$media->shouldReceive('save')->once()->withArgs([
199+
$formatA, $this->srcDir . '/MyPlaylist_384_%05d.ts',
200+
]);
201+
202+
$media->shouldReceive('save')->once()->withArgs([
203+
$formatB, $this->srcDir . '/MyPlaylist_512_%05d.ts',
204+
]);
205+
206+
$exporter = new HLSPlaylistExporter($media);
207+
208+
$exporter->addFormat($formatB)
209+
->addFormat($formatA)
210+
->dontSortFormats()
211+
->setPlaylistPath($playlist)
212+
->setSegmentLength(15);
213+
214+
$exporter->toDisk('local')->save($playlist);
215+
216+
$this->assertEquals(file_get_contents($this->srcDir . '/MyPlaylist.m3u8'),
217+
'#EXTM3U' . PHP_EOL .
218+
'#EXT-X-STREAM-INF:BANDWIDTH=512000' . PHP_EOL .
219+
'MyPlaylist_512.m3u8' . PHP_EOL .
220+
'#EXT-X-STREAM-INF:BANDWIDTH=384000' . PHP_EOL .
221+
'MyPlaylist_384.m3u8'
222+
);
223+
224+
@unlink($this->srcDir . '/MyPlaylist.m3u8');
225+
}
183226
}

0 commit comments

Comments
 (0)