Skip to content

Commit c371706

Browse files
bug fixes #27
1 parent 4991cfb commit c371706

File tree

6 files changed

+149
-90
lines changed

6 files changed

+149
-90
lines changed

README.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,12 @@ $rep_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
106106
$rep_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
107107
$rep_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
108108
$rep_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
109-
$rep_2k = (new Representation)->setKiloBitrate(6144)->setResize(2560, 1440);
110-
$rep_4k = (new Representation)->setKiloBitrate(17408)->setResize(3840, 2160);
109+
$rep_2k = (new Representation)->setKiloBitrate(6144)->setResize(2560, 1440);
110+
$rep_4k = (new Representation)->setKiloBitrate(17408)->setResize(3840, 2160);
111111

112112
$video->DASH()
113113
->HEVC()
114-
->addRepresentation($rep_144p)// add a representation
115-
->addRepresentation($rep_240p)
116-
->addRepresentation($rep_360p)
117-
->addRepresentation($rep_480p)
118-
->addRepresentation($rep_720p)
119-
->addRepresentation($rep_1080p)
120-
->addRepresentation($rep_2k)
121-
->addRepresentation($rep_4k)
114+
->addRepresentations($rep_144p, $rep_240p, $rep_360p, $rep_480p, $rep_720p, $rep_1080p, $rep_2k, $rep_4k)// add representations
122115
->setAdaption('id=0,streams=v id=1,streams=a') // Set a adaption.
123116
->save('/var/www/media/videos/dash-stream.mpd');
124117
```
@@ -144,9 +137,7 @@ $rep_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
144137
$video->HLS()
145138
->X264()
146139
->setHlsBaseUrl('https://bucket.s3-us-west-1.amazonaws.com/videos') // Add a base URL
147-
->addRepresentation($rep_360p)
148-
->addRepresentation($rep_480p)
149-
->addRepresentation($rep_720p)
140+
->addRepresentations($rep_360p, $rep_480p, $rep_720p)// add representations
150141
->setHlsTime(5) // Set Hls Time. Default value is 10
151142
->setHlsAllowCache(false) // Default value is true
152143
->save();
@@ -167,7 +158,7 @@ $url = 'https://www.aminyazdanpanah.com/PATH_TO_KEY_DIRECTORY/random_key.key';//
167158
$video->HLS()
168159
->X264()
169160
->setTsSubDirectory('ts_files')// put all ts files in a subdirectory
170-
->generateRandomKeyInfo($url, $save_to)
161+
->encryption($save_to, $url)
171162
->autoGenerateRepresentations([1080, 480, 240])
172163
->save('/var/www/media/videos/hls-stream.m3u8');
173164
```
@@ -309,7 +300,6 @@ You can use these libraries to play your streams.
309300
- **[MPEGDASH-iOS-Player](https://github.com/MPEGDASHPlayer/MPEGDASH-iOS-Player)**
310301
- HLS:
311302
- **[Player](https://github.com/piemonte/Player)**
312-
- **[Mamba](https://github.com/Comcast/mamba)**
313303
- **Windows, Linux, and macOS**
314304
- DASH and HLS:
315305
- **[FFmpeg(ffplay)](https://github.com/FFmpeg/FFmpeg)**

src/ExportHLSPlaylist.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ private static function generateContents(array $representations, string $basenam
3535
$content[] = "#EXT-X-VERSION:3";
3636

3737
foreach ($representations as $representation) {
38-
if ($representation instanceof Representation) {
39-
$content[] = "#EXT-X-STREAM-INF:BANDWIDTH=" . $representation->getKiloBitrate() * 1024 . ",RESOLUTION=" . $representation->getResize();
40-
$content[] = $basename . "_" . $representation->getHeight() . "p.m3u8";
41-
}
38+
$content[] = "#EXT-X-STREAM-INF:BANDWIDTH=" . $representation->getKiloBitrate() * 1024 . ",RESOLUTION=" . $representation->getResize();
39+
$content[] = $basename . "_" . $representation->getHeight() . "p.m3u8";
4240
}
4341

4442
return implode(PHP_EOL, $content);

src/Filters/DASHFilter.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Streaming\DASH;
1717
use Streaming\Format\X264;
18-
use Streaming\Representation;
1918

2019
class DASHFilter extends Filter
2120
{
@@ -36,16 +35,14 @@ private function DASHFilter(DASH $dash)
3635
$filter = $this->getAdditionalFilters($dash->getFormat(), count($dash->getRepresentations()));
3736

3837
foreach ($dash->getRepresentations() as $key => $representation) {
39-
if ($representation instanceof Representation) {
40-
$filter[] = "-map";
41-
$filter[] = "0";
42-
$filter[] = "-b:v:" . $key;
43-
$filter[] = $representation->getKiloBitrate() . "k";
38+
$filter[] = "-map";
39+
$filter[] = "0";
40+
$filter[] = "-b:v:" . $key;
41+
$filter[] = $representation->getKiloBitrate() . "k";
4442

45-
if (null !== $representation->getResize()) {
46-
$filter[] = "-s:v:" . $key;
47-
$filter[] = $representation->getResize();
48-
}
43+
if (null !== $representation->getResize()) {
44+
$filter[] = "-s:v:" . $key;
45+
$filter[] = $representation->getResize();
4946
}
5047
}
5148

src/Filters/HLSFilter.php

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Streaming\FileManager;
1515
use Streaming\HLS;
16-
use Streaming\Representation;
1716
use Streaming\Utilities;
1817

1918
class HLSFilter extends Filter
@@ -34,11 +33,58 @@ public function setFilter($media): void
3433
private function HLSFilter(HLS $hls)
3534
{
3635
$filter = [];
37-
$total_count = count($representations = $hls->getRepresentations());
38-
$counter = 0;
36+
$representations = $hls->getRepresentations();
3937
$path_parts = $hls->getPathInfo();
4038
$dirname = str_replace("\\", "/", $path_parts["dirname"]);
41-
$filename = substr($path_parts["filename"], -100);
39+
list($ts_sub_dir, $base_url) = $this->getSubDirectory($hls, $dirname);
40+
41+
foreach ($representations as $key => $representation) {
42+
if ($key) {
43+
$filter = array_merge($filter, $this->getFormats($hls));
44+
}
45+
46+
$filter[] = "-s:v";
47+
$filter[] = $representation->getResize();
48+
$filter[] = "-crf";
49+
$filter[] = "20";
50+
$filter[] = "-sc_threshold";
51+
$filter[] = "0";
52+
$filter[] = "-g";
53+
$filter[] = "48";
54+
$filter[] = "-keyint_min";
55+
$filter[] = "48";
56+
$filter[] = "-hls_list_size";
57+
$filter[] = "0";
58+
$filter[] = "-hls_time";
59+
$filter[] = $hls->getHlsTime();
60+
$filter[] = "-hls_allow_cache";
61+
$filter[] = (int)$hls->isHlsAllowCache();
62+
$filter[] = "-b:v";
63+
$filter[] = $representation->getKiloBitrate() . "k";
64+
$filter[] = "-maxrate";
65+
$filter[] = intval($representation->getKiloBitrate() * 1.2) . "k";
66+
$filter[] = "-hls_segment_filename";
67+
$filter[] = $dirname . "/" . $ts_sub_dir . $path_parts["filename"] . "_" . $representation->getHeight() . "p_%04d.ts";
68+
$filter = array_merge($filter, $this->getBaseURL($base_url));
69+
$filter = array_merge($filter, $this->getKeyInfo($hls));
70+
$filter[] = "-strict";
71+
$filter[] = $hls->getStrict();
72+
73+
if (end($representations) !== $representation) {
74+
$filter[] = $dirname . "/" . $path_parts["filename"] . "_" . $representation->getHeight() . "p.m3u8";
75+
}
76+
}
77+
78+
return $filter;
79+
}
80+
81+
/**
82+
* @param HLS $hls
83+
* @param $dirname
84+
* @return array
85+
*/
86+
private function getSubDirectory(HLS $hls, $dirname)
87+
{
4288
$ts_sub_dir = Utilities::appendSlash($hls->getTsSubDirectory());
4389
$base_url = Utilities::appendSlash($hls->getHlsBaseUrl());
4490

@@ -47,49 +93,42 @@ private function HLSFilter(HLS $hls)
4793
$base_url = $base_url . $hls->getTsSubDirectory() . "/";
4894
}
4995

50-
foreach ($representations as $representation) {
51-
if ($representation instanceof Representation) {
52-
$filter[] = "-s:v";
53-
$filter[] = $representation->getResize();
54-
$filter[] = "-crf";
55-
$filter[] = "20";
56-
$filter[] = "-sc_threshold";
57-
$filter[] = "0";
58-
$filter[] = "-g";
59-
$filter[] = "48";
60-
$filter[] = "-keyint_min";
61-
$filter[] = "48";
62-
$filter[] = "-hls_list_size";
63-
$filter[] = "0";
64-
$filter[] = "-hls_time";
65-
$filter[] = $hls->getHlsTime();
66-
$filter[] = "-hls_allow_cache";
67-
$filter[] = (int)$hls->isHlsAllowCache();
68-
$filter[] = "-b:v";
69-
$filter[] = $representation->getKiloBitrate() . "k";
70-
$filter[] = "-maxrate";
71-
$filter[] = intval($representation->getKiloBitrate() * 1.2) . "k";
72-
$filter[] = "-hls_segment_filename";
73-
$filter[] = $dirname . "/" . $ts_sub_dir . $filename . "_" . $representation->getHeight() . "p_%04d.ts";
74-
75-
if ($base_url) {
76-
$filter[] = "-hls_base_url";
77-
$filter[] = $base_url;
78-
}
79-
80-
if ($hls->getHlsKeyInfoFile()) {
81-
$filter[] = "-hls_key_info_file";
82-
$filter[] = $hls->getHlsKeyInfoFile();
83-
}
84-
85-
$filter[] = "-strict";
86-
$filter[] = $hls->getStrict();
87-
88-
if (++$counter !== $total_count) {
89-
$filter[] = $dirname . "/" . $filename . "_" . $representation->getHeight() . "p.m3u8";
90-
}
91-
}
96+
return [$ts_sub_dir, $base_url];
97+
}
98+
99+
private function getFormats(HLS $hls)
100+
{
101+
$format = ['-c:v', $hls->getFormat()->getVideoCodec()];
102+
103+
$audio_format = $hls->getFormat()->getAudioCodec();
104+
if ($audio_format) {
105+
$format = array_merge($format, ['-c:a', $audio_format]);
106+
}
107+
108+
return $format;
109+
}
110+
111+
private function getBaseURL($base_url)
112+
{
113+
$filter = [];
114+
115+
if ($base_url) {
116+
$filter[] = "-hls_base_url";
117+
$filter[] = $base_url;
92118
}
119+
120+
return $filter;
121+
}
122+
123+
private function getKeyInfo(HLS $hls)
124+
{
125+
$filter = [];
126+
127+
if ($hls->getHlsKeyInfoFile()) {
128+
$filter[] = "-hls_key_info_file";
129+
$filter[] = $hls->getHlsKeyInfoFile();
130+
}
131+
93132
return $filter;
94133
}
95134
}

src/HLS.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,32 @@ public function setHlsKeyInfoFile(string $hls_key_info_file): HLS
102102
}
103103

104104
/**
105+
* @param string $save_to
105106
* @param string $url
106-
* @param string $path
107107
* @param int $length
108108
* @return HLS
109109
*/
110-
public function generateRandomKeyInfo(string $url, string $path, int $length = 16): HLS
110+
public function encryption(string $save_to, string $url, int $length = 16): HLS
111111
{
112-
$this->setHlsKeyInfoFile(KeyInfo::generate($url, $path, $length));
112+
$this->setHlsKeyInfoFile(KeyInfo::generate($url, $save_to, $length));
113113
$this->tmp_key_info_file = true;
114114

115115
return $this;
116116
}
117117

118+
/**
119+
* @param string $url
120+
* @param string $path
121+
* @param int $length
122+
* @return HLS
123+
* @deprecated Please use encryption instead
124+
*/
125+
public function generateRandomKeyInfo(string $url, string $path, int $length = 16): HLS
126+
{
127+
@trigger_error('generateRandomKeyInfo method is deprecated and will be removed in a future release. Use encryption instead.', E_USER_DEPRECATED);
128+
return $this->encryption($path, $url, $length);
129+
}
130+
118131
/**
119132
* @return string
120133
*/

src/Traits/Representations.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Streaming\Traits;
1313

1414
use Streaming\AutoRepresentations;
15-
use Streaming\Exception\Exception;
15+
use Streaming\Exception\InvalidArgumentException;
1616
use Streaming\Representation;
1717

1818
trait Representations
@@ -23,15 +23,32 @@ trait Representations
2323
/**
2424
* @param Representation $rep
2525
* @return $this
26-
* @throws Exception
26+
* @deprecated Please use addRepresentations instead
2727
*/
2828
public function addRepresentation(Representation $rep)
2929
{
30-
if (!$this->format) {
31-
throw new Exception('Format has not been set');
30+
@trigger_error('addRepresentation method is deprecated and will be removed in a future release. Use addRepresentations instead.', E_USER_DEPRECATED);
31+
$this->checkFormat();
32+
$this->representations[] = $rep;
33+
34+
return $this;
35+
}
36+
37+
/**
38+
* @return $this
39+
*/
40+
public function addRepresentations()
41+
{
42+
$this->checkFormat();
43+
$reps = func_get_args();
44+
45+
foreach ($reps as $rep) {
46+
if (!$rep instanceof Representation) {
47+
throw new InvalidArgumentException('It must be instance of Representation object');
48+
}
3249
}
3350

34-
$this->representations[] = $rep;
51+
$this->representations = $reps;
3552
return $this;
3653
}
3754

@@ -47,17 +64,22 @@ public function getRepresentations(): array
4764
* @param array $side_values
4865
* @param array|null $k_bitrate_values
4966
* @return $this
50-
* @throws Exception
5167
*/
5268
public function autoGenerateRepresentations(array $side_values = null, array $k_bitrate_values = null)
5369
{
54-
if (!$this->format) {
55-
throw new Exception('Format has not been set');
56-
}
57-
58-
$this->representations = (new AutoRepresentations($this->getMedia()->probe(), $side_values, $k_bitrate_values))
59-
->get();
70+
$this->checkFormat();
71+
$this->representations = (new AutoRepresentations($this->getMedia()->probe(), $side_values, $k_bitrate_values))->get();
6072

6173
return $this;
6274
}
75+
76+
/**
77+
* check whether format is set or nor
78+
*/
79+
private function checkFormat()
80+
{
81+
if (!$this->format) {
82+
throw new InvalidArgumentException('First you must set the format of the video');
83+
}
84+
}
6385
}

0 commit comments

Comments
 (0)