Skip to content

Commit 4d4fc5a

Browse files
Add fragmented MP4 format support #31
Minor improvements
1 parent f27e342 commit 4d4fc5a

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/AutoRepresentations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private function getKiloBitrateValues(?array $k_bitrate_values): void
134134
*/
135135
private function getSideValues(?array $side_values): void
136136
{
137-
if (!empty($side_values)) {
137+
if (!is_null($side_values)) {
138138
$this->side_values = $side_values;
139139
return;
140140
}

src/Filters/HLSFilter.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ private function HLSFilter(HLS $hls): array
3636
$reps = $hls->getRepresentations();
3737
$path_parts = $hls->getPathInfo();
3838
$dirname = str_replace("\\", "/", $path_parts["dirname"]);
39-
list($ts_sub_dir, $base_url) = $this->getSubDirectory($hls, $dirname);
39+
list($seg_sub_dir, $base_url) = $this->getSubDirectory($hls, $dirname);
40+
$full_seg_filename = $dirname . "/" . $seg_sub_dir . $path_parts["filename"];
4041

4142
foreach ($reps as $key => $rep) {
4243
if ($key) {
@@ -64,8 +65,12 @@ private function HLSFilter(HLS $hls): array
6465
$filter = array_merge($filter, $this->getAudioBitrate($rep));
6566
$filter[] = "-maxrate";
6667
$filter[] = intval($rep->getKiloBitrate() * 1.2) . "k";
68+
$filter[] = "-hls_segment_type";
69+
$filter[] = $hls->getHlsSegmentType();
70+
$filter[] = "-hls_fmp4_init_filename";
71+
$filter[] = $path_parts["filename"] . "_" . $hls->getHlsFmp4InitFilename();
6772
$filter[] = "-hls_segment_filename";
68-
$filter[] = $dirname . "/" . $ts_sub_dir . $path_parts["filename"] . "_" . $rep->getHeight() . "p_%04d.ts";
73+
$filter[] = $this->getSegmentFilename($full_seg_filename, $rep, $hls->getHlsSegmentType());
6974
$filter = array_merge($filter, $this->getBaseURL($base_url));
7075
$filter = array_merge($filter, $this->getKeyInfo($hls));
7176
$filter = array_merge($filter, $hls->getAdditionalParams());
@@ -135,4 +140,16 @@ private function getAudioBitrate(Representation $rep): array
135140
{
136141
return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : [];
137142
}
143+
144+
/**
145+
* @param string $full_seg_filename
146+
* @param Representation $rep
147+
* @param string $type
148+
* @return string
149+
*/
150+
private function getSegmentFilename(string $full_seg_filename, Representation $rep, string $type): string
151+
{
152+
$ext = ($type === "fmp4") ? "m4s" : "ts";
153+
return $full_seg_filename . "_" . $rep->getHeight() . "p_%04d." . $ext;
154+
}
138155
}

src/HLS.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class HLS extends Streaming
4040
/** @var string */
4141
public $master_playlist;
4242

43+
/** @var string */
44+
private $hls_segment_type = 'mpegts';
45+
46+
/** @var string */
47+
private $hls_fmp4_init_filename = "init.mp4";
48+
4349
/** @var array */
4450
private $stream_info = [];
4551

@@ -179,6 +185,42 @@ public function setMasterPlaylist(string $master_playlist, array $stream_info =
179185
return $this;
180186
}
181187

188+
/**
189+
* @param string $hls_segment_type
190+
* @return HLS
191+
*/
192+
public function setHlsSegmentType(string $hls_segment_type): HLS
193+
{
194+
$this->hls_segment_type = $hls_segment_type;
195+
return $this;
196+
}
197+
198+
/**
199+
* @return string
200+
*/
201+
public function getHlsSegmentType(): string
202+
{
203+
return $this->hls_segment_type;
204+
}
205+
206+
/**
207+
* @param string $hls_fmp4_init_filename
208+
* @return HLS
209+
*/
210+
public function setHlsFmp4InitFilename(string $hls_fmp4_init_filename): HLS
211+
{
212+
$this->hls_fmp4_init_filename = $hls_fmp4_init_filename;
213+
return $this;
214+
}
215+
216+
/**
217+
* @return string
218+
*/
219+
public function getHlsFmp4InitFilename(): string
220+
{
221+
return $this->hls_fmp4_init_filename;
222+
}
223+
182224
/**
183225
* @return Filter
184226
*/

0 commit comments

Comments
 (0)