Skip to content

Commit fb53558

Browse files
bug fixes and other minor improvements
1 parent c371706 commit fb53558

24 files changed

+305
-243
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This package provides integration with **[PHP-FFMpeg](https://github.com/PHP-FFM
2727
- [Other Advanced Features](#other-advanced-features)
2828
- [Asynchronous Task Execution](#asynchronous-task-execution)
2929
- [Several Open Source Players](#several-open-source-players)
30-
- [Contributing and Reporting Bug](#contributing-and-reporting-bugs)
30+
- [Contributing and Reporting Bugs](#contributing-and-reporting-bugs)
3131
- [Credits](#credits)
3232
- [License](#license)
3333

@@ -80,7 +80,7 @@ $video = $ffmpeg->open('/var/www/media/videos/video.mp4');
8080
#### 2. From Clouds
8181
You can open a file from a cloud by passing an array of cloud configuration to the `openFromCloud` method.
8282

83-
In **[this page](https://video.aminyazdanpanah.com/start/open-clouds)**, you will find some examples of opening a file from **[Amazon Web Services (AWS)](https://aws.amazon.com/)**, **[Google Cloud Storage](https://console.cloud.google.com/storage)**, **[Microsoft Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)**, and a custom cloud.
83+
In **[this page](https://video.aminyazdanpanah.com/start/open-clouds)**, you will find some examples of opening a file from **[Amazon S3](https://aws.amazon.com/s3)**, **[Google Cloud Storage](https://console.cloud.google.com/storage)**, **[Microsoft Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)**, and a custom cloud.
8484
``` php
8585
$video = $ffmpeg->openFromCloud($from_google_cloud);
8686
```
@@ -100,18 +100,18 @@ Generate representations manually:
100100
``` php
101101
use Streaming\Representation;
102102

103-
$rep_144p = (new Representation)->setKiloBitrate(95)->setResize(256, 144);
104-
$rep_240p = (new Representation)->setKiloBitrate(150)->setResize(426, 240);
105-
$rep_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
106-
$rep_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
107-
$rep_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
108-
$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);
103+
$r_144p = (new Representation)->setKiloBitrate(95)->setResize(256, 144);
104+
$r_240p = (new Representation)->setKiloBitrate(150)->setResize(426, 240);
105+
$r_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
106+
$r_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
107+
$r_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
108+
$r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
109+
$r_2k = (new Representation)->setKiloBitrate(6144)->setResize(2560, 1440);
110+
$r_4k = (new Representation)->setKiloBitrate(17408)->setResize(3840, 2160);
111111

112112
$video->DASH()
113113
->HEVC()
114-
->addRepresentations($rep_144p, $rep_240p, $rep_360p, $rep_480p, $rep_720p, $rep_1080p, $rep_2k, $rep_4k)// add representations
114+
->addRepresentations($r_144p, $r_240p, $r_360p, $r_480p, $r_720p, $r_1080p, $r_2k, $r_4k)//add representations
115115
->setAdaption('id=0,streams=v id=1,streams=a') // Set a adaption.
116116
->save('/var/www/media/videos/dash-stream.mpd');
117117
```
@@ -130,14 +130,14 @@ Generate representations manually:
130130
``` php
131131
use Streaming\Representation;
132132

133-
$rep_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
134-
$rep_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
135-
$rep_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
133+
$r_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
134+
$r_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
135+
$r_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
136136

137137
$video->HLS()
138138
->X264()
139139
->setHlsBaseUrl('https://bucket.s3-us-west-1.amazonaws.com/videos') // Add a base URL
140-
->addRepresentations($rep_360p, $rep_480p, $rep_720p)// add representations
140+
->addRepresentations($r_360p, $r_480p, $r_720p)
141141
->setHlsTime(5) // Set Hls Time. Default value is 10
142142
->setHlsAllowCache(false) // Default value is true
143143
->save();
@@ -147,7 +147,7 @@ $video->HLS()
147147
#### Encrypted HLS
148148
The encryption process requires some kind of secret (key) together with an encryption algorithm. HLS uses AES in cipher block chaining (CBC) mode. This means each block is encrypted using the ciphertext of the preceding block. [Learn more](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation)
149149

150-
You must specify a path to save a random key on your local machine and also a URL(or a path) to access the key on your website(the key you will save must be accessible from your website). You must pass both these parameters to the `generateRandomKeyInfo` method:
150+
You must specify a path to save a random key on your local machine and also a URL(or a path) to access the key on your website(the key you will save must be accessible from your website). You must pass both these parameters to the `encryption` method:
151151
``` php
152152
//A path you want to save a random key on your server
153153
$save_to = '/home/public_html/PATH_TO_KEY_DIRECTORY/random_key.key';
@@ -206,16 +206,16 @@ $hls = $video->HLS()
206206

207207
$hls->save();
208208
```
209-
**NOTE:** If you open a file from cloud and did not pass a path to save a file, you will have to pass a local path to the `save` method.
209+
**NOTE:** If you opened a file from cloud and did not pass a path to save a file, you will have to pass a local path to the `save` method.
210210

211211
#### 2. To Clouds
212212
You can save your files to a cloud by passing an array of cloud configuration to the `save` method.
213213

214-
In **[this page](https://video.aminyazdanpanah.com/start/open-clouds)**, you will find some examples of saving files to **[Amazon Web Services (AWS)](https://aws.amazon.com/)**, **[Google Cloud Storage](https://console.cloud.google.com/storage)**, **[Microsoft Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)**, and a custom cloud.
214+
In **[this page](https://video.aminyazdanpanah.com/start/open-clouds)**, you will find some examples of saving files to **[Amazon S3](https://aws.amazon.com/s3)**, **[Google Cloud Storage](https://console.cloud.google.com/storage)**, **[Microsoft Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)**, and a custom cloud.
215215
``` php
216216
$dash->save(null, [$to_aws_cloud, $to_google_cloud, $to_microsoft_azure, $to_custom_cloud]);
217217
```
218-
A path can also be passed to save a copy of files on your local machine.
218+
A path can also be passed to save a copy of files to your local machine.
219219
``` php
220220
$hls->save('/var/www/media/videos/hls-stream.m3u8', [$to_google_cloud, $to_custom_cloud]);
221221
```

src/AutoRepresentations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function getKiloBitRate(): int
6464
{
6565
if (!$this->video->has('bit_rate')) {
6666
if (!$this->format->has('bit_rate')) {
67-
throw new InvalidArgumentException("We could determine the value of video bitrate");
67+
throw new InvalidArgumentException("We could not determine the value of video's bitrate");
6868
}
6969

7070
return intval(($this->format->get('bit_rate') / 1024) * .9);
@@ -81,7 +81,7 @@ public function get(): array
8181
$k_bitrate = $this->getKiloBitRate();
8282
list($w, $h, $r) = $this->getDimensions();
8383

84-
$reps[] = $this->addRep($k_bitrate, $w, $h);
84+
$reps = [$this->addRep($k_bitrate, $w, $h)];
8585

8686
foreach ($this->side_values as $key => $height) {
8787
$reps[] = $this->addRep($this->k_bitrate_values[$key], Utilities::roundToEven($r * $height), $height);
@@ -99,7 +99,7 @@ public function get(): array
9999
*/
100100
private function addRep($k_bitrate, $width, $height): Representation
101101
{
102-
return (new Representation())->setKiloBitrate($k_bitrate)->setResize($width, $height);
102+
return (new Representation)->setKiloBitrate($k_bitrate)->setResize($width, $height);
103103
}
104104

105105
/**

src/Clouds/CloudManager.php renamed to src/Clouds/Cloud.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414

1515

1616
use Streaming\Exception\InvalidArgumentException;
17-
use Streaming\FileManager;
17+
use Streaming\File;
1818

19-
class CloudManager
19+
class Cloud
2020
{
2121
/**
2222
* @param array $clouds
2323
* @param string $tmp_dir
2424
*/
25-
public static function uploadDirectory(?array $clouds, ?string $tmp_dir): void
25+
public static function uploadDirectory(array $clouds, string $tmp_dir): void
2626
{
27-
if (!$clouds) {
28-
return;
29-
}
30-
3127
if (!is_array(current($clouds))) {
3228
$clouds = [$clouds];
3329
}
@@ -44,25 +40,24 @@ public static function uploadDirectory(?array $clouds, ?string $tmp_dir): void
4440
*/
4541
public static function download(array $cloud, string $save_to = null): array
4642
{
47-
list($save_to, $is_tmp) = $save_to ? [$save_to, false] : [FileManager::tmpFile(), true];
43+
list($save_to, $is_tmp) = $save_to ? [$save_to, false] : [File::tmpFile(), true];
4844
static::transfer($cloud, __FUNCTION__, $save_to);
4945

5046
return [$save_to, $is_tmp];
5147
}
5248

5349
/**
54-
* @param $cloud
50+
* @param $cloud_c
5551
* @param $method
5652
* @param $path
5753
*/
58-
private static function transfer($cloud, $method, $path): void
54+
private static function transfer(array $cloud_c, string $method, string $path): void
5955
{
60-
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
61-
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
62-
call_user_func_array([$cloud['cloud'], $method], [$path, $options]);
56+
extract($cloud_c);
57+
if (isset($cloud) && $cloud instanceof CloudInterface) {
58+
call_user_func_array([$cloud, $method], [$path, $options ?? []]);
6359
} else {
64-
throw new InvalidArgumentException('You must pass an array of clouds to the save method.
65-
and the cloud must be instance of CloudInterface');
60+
throw new InvalidArgumentException('The cloud key must be instance of the CloudInterface');
6661
}
6762
}
6863
}

src/DASH.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ protected function getFilter(): Filter
4747
{
4848
return new DASHFilter($this);
4949
}
50-
}
50+
}

src/Export.php

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

1414
use FFMpeg\Exception\ExceptionInterface;
15-
use Streaming\Clouds\CloudManager;
15+
use Streaming\Clouds\Cloud;
1616
use Streaming\Exception\InvalidArgumentException;
1717
use Streaming\Exception\RuntimeException;
1818
use Streaming\Filters\Filter;
@@ -90,11 +90,25 @@ public function getPathInfo(): array
9090
/**
9191
* @param string|null $path
9292
*/
93-
private function moveTmpFolder(?string $path): void
93+
private function moveTmp(?string $path): void
9494
{
95-
if ($this->tmp_dir && $path) {
96-
FileManager::moveDir($this->tmp_dir, pathinfo($path, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR);
95+
if ($this->isTmpDir() && !is_null($path)) {
96+
File::moveDir($this->tmp_dir, dirname($path));
9797
$this->path_info = pathinfo($path);
98+
$this->tmp_dir = '';
99+
}
100+
}
101+
102+
103+
/**
104+
* @param array $clouds
105+
* @param string $path
106+
*/
107+
private function clouds(array $clouds, ?string $path): void
108+
{
109+
if (!empty($clouds)) {
110+
Cloud::uploadDirectory($clouds, $this->tmp_dir);
111+
$this->moveTmp($path);
98112
}
99113
}
100114

@@ -103,15 +117,14 @@ private function moveTmpFolder(?string $path): void
103117
*/
104118
private function getPath(): string
105119
{
106-
$path = substr(str_replace("\\", "/", $this->path_info["dirname"] . "/" . $this->path_info["filename"]), 0, PHP_MAXPATHLEN);
120+
$path = str_replace("\\", "/", $this->path_info["dirname"] . "/" . $this->path_info["filename"]);
107121

108122
if ($this instanceof DASH) {
109-
$path = $path . ".mpd";
123+
$path .= ".mpd";
110124
} elseif ($this instanceof HLS) {
111-
ExportHLSPlaylist::savePlayList($path . ".m3u8", $this->getRepresentations(), $this->path_info["filename"]);
112-
113-
$representations = $this->getRepresentations();
114-
$path = $path . "_" . end($representations)->getHeight() . "p.m3u8";
125+
$reps = $this->getRepresentations();
126+
HLSPlaylist::save($path . ".m3u8", $reps);
127+
$path .= "_" . end($reps)->getHeight() . "p.m3u8";
115128
}
116129

117130
return $path;
@@ -125,44 +138,44 @@ abstract protected function getFilter(): Filter;
125138
/**
126139
* Run FFmpeg to package media content
127140
*/
128-
private function runFFmpeg(): void
141+
private function run(): void
129142
{
130143
try {
131144
$this->media
132145
->addFilter($this->getFilter())
133146
->save($this->getFormat(), $this->getPath());
134147
} catch (ExceptionInterface $e) {
135-
throw new RuntimeException(sprintf("There was an error saving files: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e);
148+
throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
136149
}
137150
}
138151

139152
/**
140-
* @param $path
153+
* @param string|null $path
141154
*/
142-
private function tmpDirectory($path): void
155+
private function tmpDirectory(?string $path): void
143156
{
144-
$basename = $path ? pathinfo($path, PATHINFO_BASENAME) : Utilities::randomString();
157+
$basename = $path ? basename($path) : Utilities::randomString();
145158

146-
$this->tmp_dir = FileManager::tmpDir();
159+
$this->tmp_dir = File::tmpDir();
147160
$this->path_info = pathinfo($this->tmp_dir . $basename);
148161
}
149162

150163
/**
151164
* @param $path
152165
* @param $clouds
153166
*/
154-
private function createPathInfoAndTmpDir($path, $clouds): void
167+
private function makePaths(?string $path, array $clouds): void
155168
{
156-
if (null !== $path) {
157-
$this->path_info = pathinfo($path);
158-
FileManager::makeDir($this->path_info["dirname"]);
159-
}
160-
161169
if ($clouds) {
162170
$this->tmpDirectory($path);
163-
}
171+
} elseif (!is_null($path)) {
172+
if (strlen($path) > PHP_MAXPATHLEN) {
173+
throw new InvalidArgumentException("The path is too long");
174+
}
164175

165-
if (null === $path && $this->media->isTmp() && !$clouds) {
176+
File::makeDir(dirname($path));
177+
$this->path_info = pathinfo($path);
178+
} elseif ($this->media->isTmp()) {
166179
throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
167180
}
168181
}
@@ -175,22 +188,9 @@ private function createPathInfoAndTmpDir($path, $clouds): void
175188
*/
176189
public function save(string $path = null, array $clouds = [], bool $metadata = true)
177190
{
178-
/**
179-
* Synopsis
180-
* ------------------------------------------------------------------------------
181-
* 1. Create directory path, path info array, and temporary folders(if it is required).
182-
* 2. Build object and run FFmpeg to package media content and save on the local machine.
183-
* 3. If the cloud is specified, entire packaged files will be uploaded to clouds.
184-
* 4. If files were saved into a tmp folder, then they will be moved to the local path(if the path is specified).
185-
* 5. Return all video and also streams' metadata and save as a json file on the local machine(it won't save metadata to clouds because of some security reasons).
186-
* 6. In the end, clear all tmp files.
187-
* ------------------------------------------------------------------------------
188-
*/
189-
190-
$this->createPathInfoAndTmpDir($path, $clouds);
191-
$this->runFFmpeg();
192-
CloudManager::uploadDirectory($clouds, $this->tmp_dir);
193-
$this->moveTmpFolder($path);
191+
$this->makePaths($path, $clouds);
192+
$this->run();
193+
$this->clouds($clouds, $path);
194194

195195
return $metadata ? (new Metadata($this))->extract() : $this;
196196
}
@@ -207,7 +207,7 @@ public function __destruct()
207207
}
208208

209209
if ($this->tmp_dir) {
210-
FileManager::deleteDirectory($this->tmp_dir);
210+
File::deleteDirectory($this->tmp_dir);
211211
}
212212

213213
if ($this instanceof HLS && $this->tmp_key_info_file) {

src/ExportHLSPlaylist.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)