Skip to content

Commit e3ef434

Browse files
author
pascalbaljet
committed
Monitor HLS exports
1 parent 183763b commit e3ef434

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

CHANGELOG.md

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

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

5+
## 1.3.0 - 2017-11-13
6+
7+
### Added
8+
- Support for monitoring the progress of a HLS Export.
9+
10+
### Deprecated
11+
- Nothing
12+
13+
### Fixed
14+
- Some refactoring
15+
16+
### Removed
17+
- Nothing
18+
19+
### Security
20+
- Nothing
21+
522
## 1.2.0 - 2017-11-13
623

724
### Added

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ $durationInSeconds = $media->getDurationInSeconds(); // returns an int
177177
$durationInMiliseconds = $media->getDurationInMiliseconds(); // returns a float
178178
```
179179

180+
When opening or saving files from or to a remote disk, temporary files will be created on your server. After you're done exporting or processing these files, you could clean them up by calling the ```cleanupTemporaryFiles()``` method:
181+
182+
``` php
183+
FFMpeg::cleanupTemporaryFiles();
184+
```
185+
186+
## HLS
187+
180188
You can create a M3U8 playlist to do [HLS](https://en.wikipedia.org/wiki/HTTP_Live_Streaming). Exporting is currently only supported on local disks.
181189

182190
``` php
@@ -215,11 +223,14 @@ FFMpeg::open('steve_howe.mp4')
215223
->save('adaptive_steve.m3u8');
216224
```
217225

218-
219-
When opening or saving files from or to a remote disk, temporary files will be created on your server. After you're done exporting or processing these files, you could clean them up by calling the ```cleanupTemporaryFiles()``` method:
226+
As of version 1.3.0 you can monitor the transcoding progress of a HLS export. Use the ```onProgress``` method to provide a callback which gives you the completed percentage.
220227

221228
``` php
222-
FFMpeg::cleanupTemporaryFiles();
229+
$exporter = FFMpeg::open('steve_howe.mp4')
230+
->exportForHLS()
231+
->onProgress(function ($percentage) {
232+
echo "$percentage % transcoded";
233+
});
223234
```
224235

225236
## Advanced

src/HLSPlaylistExporter.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class HLSPlaylistExporter extends MediaExporter
1515

1616
protected $saveMethod = 'savePlaylist';
1717

18+
protected $progressCallback;
19+
1820
public function addFormat(VideoInterface $format, callable $callback = null): MediaExporter
1921
{
2022
$segmentedExporter = $this->getSegmentedExporterFromFormat($format);
@@ -75,11 +77,43 @@ public function getSegmentedExporters(): array
7577
return $this->segmentedExporters;
7678
}
7779

80+
public function onProgress(callable $callback)
81+
{
82+
$this->progressCallback = $callback;
83+
84+
return $this;
85+
}
86+
87+
private function getSegmentedProgressCallback($key): callable
88+
{
89+
return function ($video, $format, $percentage) use ($key) {
90+
$previousCompletedSegments = $key / count($this->segmentedExporters) * 100;
91+
92+
call_user_func($this->progressCallback,
93+
$previousCompletedSegments + ($percentage / count($this->segmentedExporters))
94+
);
95+
};
96+
}
97+
98+
public function prepareSegmentedExporters()
99+
{
100+
foreach ($this->segmentedExporters as $key => $segmentedExporter) {
101+
if ($this->progressCallback) {
102+
$segmentedExporter->getFormat()->on('progress', $this->getSegmentedProgressCallback($key));
103+
}
104+
105+
$segmentedExporter->setSegmentLength($this->segmentLength);
106+
}
107+
108+
return $this;
109+
}
110+
78111
protected function exportStreams()
79112
{
80-
foreach ($this->segmentedExporters as $segmentedExporter) {
81-
$segmentedExporter->setSegmentLength($this->segmentLength)
82-
->saveStream($this->playlistPath);
113+
$this->prepareSegmentedExporters();
114+
115+
foreach ($this->segmentedExporters as $key => $segmentedExporter) {
116+
$segmentedExporter->saveStream($this->playlistPath);
83117
}
84118
}
85119

tests/HLSPlaylistExporterTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,43 @@ public function testSegmentExporters()
6969
$this->assertEquals(15, $segmentedExporters[1]->getFilter()->getSegmentLength());
7070
}
7171

72+
public function testProgressCallbackOfSegmentExporters()
73+
{
74+
$file = $this->getVideoMedia()->getFile();
75+
76+
$media = Mockery::mock(Media::class);
77+
$media->shouldReceive('getFile')->once()->andReturn($file);
78+
79+
$playlist = 'MyPlaylist.m3u8';
80+
81+
$formatA = (new \FFMpeg\Format\Video\X264)->setKiloBitrate(384);
82+
$formatB = (new \FFMpeg\Format\Video\X264)->setKiloBitrate(512);
83+
84+
$totalPercentage = 0;
85+
$HLSExporter = new HLSPlaylistExporter($media);
86+
87+
$segmentedExporters = $HLSExporter->addFormat($formatA)
88+
->addFormat($formatB)
89+
->onProgress(function ($percentage) use (&$totalPercentage) {
90+
$totalPercentage = $percentage;
91+
})
92+
->prepareSegmentedExporters()
93+
->getSegmentedExporters();
94+
95+
$segmentedExporters[0]->getFormat()->emit('progress', [
96+
$file, $formatA, 30,
97+
]);
98+
99+
$this->assertEquals(15, $totalPercentage);
100+
101+
$segmentedExporters[1]->getFormat()->emit('progress', [
102+
$file, $formatA, 60,
103+
]);
104+
105+
$this->assertEquals(80, $totalPercentage);
106+
107+
}
108+
72109
public function testSegmentExportersWithCallback()
73110
{
74111
$media = $this->getVideoMedia();

0 commit comments

Comments
 (0)