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
|
|
View Code Duplication |
public function getPlaylistFullPath(): string |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return implode(DIRECTORY_SEPARATOR, [ |
60
|
|
|
pathinfo($this->playlistPath, PATHINFO_DIRNAME), |
61
|
|
|
$this->getPlaylistFilename(), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function getSegmentFullPath(): string |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return implode(DIRECTORY_SEPARATOR, [ |
68
|
|
|
pathinfo($this->playlistPath, PATHINFO_DIRNAME), |
69
|
|
|
$this->getSegmentFilename(), |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getPlaylistPath(): string |
74
|
|
|
{ |
75
|
|
|
return $this->playlistPath; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPlaylistName(): string |
79
|
|
|
{ |
80
|
|
|
return pathinfo($this->getPlaylistPath(), 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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.