1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
class SegmentedExporter extends MediaExporter |
6
|
|
|
{ |
7
|
|
|
protected $filter; |
8
|
|
|
|
9
|
|
|
protected $playlistPath; |
10
|
|
|
|
11
|
|
|
protected $segmentLength = 10; |
12
|
|
|
|
13
|
|
|
protected $saveMethod = 'saveStream'; |
14
|
|
|
|
15
|
|
|
public function setPlaylistPath(string $playlistPath): MediaExporter |
16
|
|
|
{ |
17
|
|
|
$this->playlistPath = $playlistPath; |
18
|
|
|
|
19
|
|
|
return $this; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function setSegmentLength(int $segmentLength): MediaExporter |
23
|
|
|
{ |
24
|
|
|
$this->segmentLength = $segmentLength; |
25
|
|
|
|
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getFilter(): SegmentedFilter |
30
|
|
|
{ |
31
|
|
|
if ($this->filter) { |
32
|
|
|
return $this->filter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->filter = new SegmentedFilter( |
36
|
|
|
$this->getPlaylistFullPath(), |
37
|
|
|
$this->segmentLength |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function saveStream(string $playlistPath): MediaExporter |
42
|
|
|
{ |
43
|
|
|
$this->setPlaylistPath($playlistPath); |
44
|
|
|
|
45
|
|
|
$this->media->addFilter( |
46
|
|
|
$this->getFilter() |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->media->save( |
50
|
|
|
$this->getFormat(), |
51
|
|
|
$this->getSegmentFullPath() |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getFullPathForFilename(string $filename) |
58
|
|
|
{ |
59
|
|
|
return implode(DIRECTORY_SEPARATOR, [ |
60
|
|
|
pathinfo($this->playlistPath, PATHINFO_DIRNAME), $filename, |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getPlaylistFullPath(): string |
65
|
|
|
{ |
66
|
|
|
return $this->getFullPathForFilename( |
67
|
|
|
$this->getPlaylistFilename() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getSegmentFullPath(): string |
72
|
|
|
{ |
73
|
|
|
return $this->getFullPathForFilename( |
74
|
|
|
$this->getSegmentFilename() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPlaylistName(): string |
79
|
|
|
{ |
80
|
|
|
return pathinfo($this->playlistPath, PATHINFO_FILENAME); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPlaylistFilename(): string |
84
|
|
|
{ |
85
|
|
|
return $this->getFormattedFilename('.m3u8'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getSegmentFilename(): string |
89
|
|
|
{ |
90
|
|
|
return $this->getFormattedFilename('_%05d.ts'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getFormattedFilename(string $suffix = ''): string |
94
|
|
|
{ |
95
|
|
|
return implode([ |
96
|
|
|
$this->getPlaylistName(), |
97
|
|
|
'_', |
98
|
|
|
$this->getFormat()->getKiloBitrate(), |
|
|
|
|
99
|
|
|
]) . $suffix; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: