Skip to content

Commit c46f019

Browse files
- Add FileManager file
- Add Save to cloud option - Fixes some minor bugs
1 parent d8d39bb commit c46f019

File tree

7 files changed

+296
-69
lines changed

7 files changed

+296
-69
lines changed

README.md

Lines changed: 115 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This package provides an integration with [PHP-FFmpeg](https://github.com/PHP-FF
2727
- [Create HLS Files](#create-hls-files)
2828
- [Encrypted HLS](#encrypted-hls)
2929
- [Transcoding](#transcoding)
30-
- [Save to Amazon S3](#save-to-amazon-s3)
30+
- [Saving Files](#saving-files)
3131
- [Other Advanced Features](#other-advanced-features)
3232
- [Several Open Source Players](#several-open-source-players)
3333
- [Contributing and Reporting Bug](#contributing-and-reporting-bug)
@@ -94,6 +94,36 @@ $video = $ffmpeg->fromURL("https://www.aminyazdanpanah.com/my_sweetie.mp4");
9494

9595
Also, the path to save the file, the method of request, and [request options](http://docs.guzzlephp.org/en/stable/request-options.html) can be passed to the method.
9696

97+
``` php
98+
$api = 'https://www.aminyazdanpanah.com/api/v1.0';
99+
$save_to = '/var/www/media/videos/my_sweetie.mp4';
100+
$method = 'POST';
101+
$options = [
102+
'auth' => ['username', 'password', 'digest'],
103+
'form_params' => [
104+
'token' => 'YOR_TOKEN',
105+
'method' => 'download',
106+
'path' => ['dir3', 'videos', 'my_sweetie.mp4']
107+
],
108+
'headers' => [
109+
'User-Agent' => 'testing/1.0',
110+
'Accept' => 'application/json',
111+
'X-Authentication' => 'Bearer x1234'
112+
],
113+
'progress' => function(
114+
$downloadTotal,
115+
$downloadedBytes,
116+
$uploadTotal,
117+
$uploadedBytes
118+
) {
119+
$percentage = intval($downloadedBytes / $downloadTotal) * 100;
120+
echo "$percentage% is downloaded\n";
121+
},
122+
];
123+
124+
$video = $ffmpeg->fromURL($api, $save_to, $method, $options);
125+
```
126+
97127
#### 3. From Amazon S3
98128
Amazon S3 or Amazon Simple Storage Service is a service offered by [Amazon Web Services (AWS)](https://aws.amazon.com/) that provides object storage through a web service interface. [learn more](https://en.wikipedia.org/wiki/Amazon_S3)
99129

@@ -104,10 +134,10 @@ For downloading a file from Amazon S3, you need to pass an array as configuratio
104134

105135
``` php
106136
$config = [
107-
'version' => 'latest',
108-
'region' => 'us-west-1',
137+
'version' => 'latest',
138+
'region' => 'us-west-1',
109139
'credentials' => [
110-
'key' => 'my-access-key-id',
140+
'key' => 'my-access-key-id',
111141
'secret' => 'my-secret-access-key',
112142
]
113143
];
@@ -137,6 +167,8 @@ $video->DASH()
137167
Also, You can create multi-representations video files using `Representation` object:
138168

139169
``` php
170+
use Streaming\Representation;
171+
140172
$rep_1 = (new Representation())->setKiloBitrate(800)->setResize(1080 , 720);
141173
$rep_2 = (new Representation())->setKiloBitrate(300)->setResize(640 , 360);
142174

@@ -161,22 +193,24 @@ Create HLS files based on original video(auto generate qualities).
161193
``` php
162194
$video->HLS()
163195
->X264()
164-
->autoGenerateRepresentations()
196+
->autoGenerateRepresentations([720, 360]) // You can limit the numbers of representatons
165197
->save();
166198
```
167199

168200
Create multi-qualities video files using `Representation` object(set bit-rate and size manually):
169201

170202
``` php
203+
use Streaming\Representation;
204+
171205
$rep_1 = (new Representation())->setKiloBitrate(1000)->setResize(1080 , 720);
172206
$rep_2 = (new Representation())->setKiloBitrate(500)->setResize(640 , 360);
173207
$rep_3 = (new Representation())->setKiloBitrate(200)->setResize(480 , 270);
174208

175209
$video->HLS()
176210
->X264()
177-
->addRepresentation($rep_1) // Add a representation
178-
->addRepresentation($rep_2) // Add a representation
179-
->addRepresentation($rep_3) // Add a representation
211+
->addRepresentation($rep_1)
212+
->addRepresentation($rep_2)
213+
->addRepresentation($rep_3)
180214
->setHlsTime(5) // Set Hls Time. Default value is 10
181215
->setHlsAllowCache(false) // Default value is true
182216
->save();
@@ -201,15 +235,15 @@ Getting OpenSSL(Windows): https://slproweb.com/products/Win32OpenSSL.html
201235
You need to pass both 'URL to the key' and 'path to save a random key' to `generateRandomKeyInfo` method:
202236
``` php
203237
//A path you want to save a random key on your server
204-
$save_to = "/var/www/my_website_project/storage/keys/enc.key";
238+
$save_to = "/var/www/my_website_project/keys/enc.key";
205239

206240
//A URL (or a path) to access the key on your website
207241
$url = "https://www.aminyazdanpanah.com/keys/enc.key";// or "/keys/enc.key";
208242

209243
$video->HLS()
210244
->X264()
211245
->generateRandomKeyInfo($url, $save_to)
212-
->autoGenerateRepresentations()
246+
->autoGenerateRepresentations([1080, 480, 240])
213247
->save('/var/www/media/videos/hls/test.m3u8');
214248
```
215249
- **Note:** Alternatively, you can generate a key info using another library and pass the path of key info to the `setHlsKeyInfoFile` method.
@@ -251,7 +285,72 @@ $video->HLS()
251285
->save('/var/www/media/videos/dash/test.m3u8');
252286
```
253287

254-
### Save to Amazon S3
288+
### Saving Files
289+
There are three ways to save your packaged video files:
290+
291+
#### 1. To a Local Path
292+
You can pass a local path to `save` method. If there was no directory in the path, then the package auto create the path.
293+
294+
``` php
295+
$dash = $video->DASH()
296+
->HEVC()
297+
->autoGenerateRepresentations()
298+
->setAdaption('id=0,streams=v id=1,streams=a');
299+
300+
$dash->save('/var/www/media/videos/dash/test.mpd');
301+
```
302+
303+
It can also be null. The default path is the input path.
304+
305+
``` php
306+
$hls = $video->HLS()
307+
->X264()
308+
->autoGenerateRepresentations();
309+
310+
$hls->save();
311+
```
312+
- **NOTE:** If you opened a file from cloud and did not pass a path to save a file, then you have to pass a local path to the `save` method.
313+
314+
#### 2. To a Cloud
315+
You can save your files to cloud using `saveToCloud` method. This package uses [Guzzle](http://docs.guzzlephp.org/en/stable/index.html) to send and receive files.
316+
317+
- Before you get started, please read the Guzzle Document found **[here](http://docs.guzzlephp.org/en/stable/index.html)**.
318+
319+
320+
``` php
321+
$api = 'https://www.aminyazdanpanah.com/api/v1.0/video/uploading';
322+
$field_name = "MY_FILES";
323+
$method = 'POST';
324+
$headers = [
325+
'User-Agent' => 'testing/1.0',
326+
'Accept' => 'application/json',
327+
'X-Authentication' => 'Bearer x1234'
328+
]
329+
$options = [
330+
'auth' => ['username', 'password', 'digest'],
331+
'progress' => function(
332+
$downloadTotal,
333+
$downloadedBytes,
334+
$uploadTotal,
335+
$uploadedBytes
336+
) {
337+
$percentage = intval($uploadedBytes / $uploadTotal) * 100;
338+
echo "$percentage% is uploaded\n";
339+
},
340+
];
341+
342+
$dash->saveToCloud($api, $field_name, null, $method, $headers, $options);
343+
```
344+
NOTE: For more information about option visit [here](http://docs.guzzlephp.org/en/stable/request-options.html).
345+
346+
It can also be passed a path to save a copy files on the local path:
347+
348+
``` php
349+
$save_to = '/var/www/media/videos/my_sweetie.mp4';
350+
$hls->saveToCloud($api, $field_name, $save_to, $method, $headers, $options);
351+
```
352+
353+
#### 3. TO Amazon S3
255354
You can save and upload entire packaged video files to [Amazon S3](https://aws.amazon.com/). For uploading files, you need to have credentials.
256355

257356
``` php
@@ -269,23 +368,18 @@ $dest = 's3://bucket';
269368

270369
Upload DASH files to Amazon Simple Storage Service:
271370
``` php
272-
$video->DASH()
273-
->HEVC()
274-
->autoGenerateRepresentations()
275-
->setAdaption('id=0,streams=v id=1,streams=a')
276-
->saveToS3($config, $dest);
371+
$dash->saveToS3($config, $dest);
277372
```
278-
A filename can also be passed to save files on your local computer/server.
373+
A path can also be passed to save a copy files on your local computer/server.
279374

280375
``` php
281-
$video->HLS()
282-
->X264()
283-
->autoGenerateRepresentations()
284-
->saveToS3($config, $dest, '/var/www/media/videos/dash/test.m3u8');
376+
$hls->saveToS3($config, $dest, '/var/www/media/videos/dash/test.m3u8');
285377
```
286378

287379
For more information, please read [AWS SDK for PHP](https://aws.amazon.com/sdk-for-php/) document.
288380

381+
- **NOTE:** You can mix opening and saving options together. For Instance, you can open a file and save packaged files to Cloud (or vice versa).
382+
289383
### Other Advanced Features
290384
You can easily use other advanced features in the [PHP-FFMpeg](https://github.com/PHP-FFMpeg/PHP-FFMpeg) library. In fact, when you open a file with `open` method(or `fromURL`), it holds the Media object that belongs to the PHP-FFMpeg.
291385

src/AutoRepresentations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AutoRepresentations
2727
/**
2828
* AutoRepresentations constructor.
2929
* @param Stream $stream
30-
* @param null $side_values
30+
* @param null | array $side_values
3131
*/
3232
public function __construct(Stream $stream, $side_values)
3333
{

src/Export.php

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,58 @@ private function getPath($path): string
104104
} elseif ($this instanceof HLS) {
105105
$representations = $this->getRepresentations();
106106
$path = $dirname . "/" . $filename . "_" . end($representations)->getHeight() . "p.m3u8";
107-
ExportHLSPlaylist::savePlayList($dirname . "/" . $filename . ".m3u8", $this->getRepresentations(), $filename);
107+
ExportHLSPlaylist::savePlayList($dirname . DIRECTORY_SEPARATOR . $filename . ".m3u8", $this->getRepresentations(), $filename);
108108
}
109109

110110
return $path;
111111
}
112112

113113
/**
114-
* @param array $config
115-
* @param string $dest
114+
* @param string $url
115+
* @param string $name
116+
* @param array $headers
116117
* @param string|null $path
118+
* @param string $method
119+
* @param array $options
117120
* @return mixed
118121
* @throws Exception
119122
*/
120-
public function saveToS3(array $config, string $dest, string $path = null)
123+
public function saveToCloud(
124+
string $url,
125+
string $name,
126+
string $path = null,
127+
string $method = 'GET',
128+
array $headers = [],
129+
array $options = []
130+
)
121131
{
122-
$basename = Helper::randomString();
132+
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path);
133+
sleep(1);
123134

124-
if (null !== $path){
125-
$basename = pathinfo($path)["basename"];
126-
}
135+
$file_manager = new FileManager($url, $method, $options);
136+
$file_manager->uploadDirectory($tmp_dir, $name, $headers);
127137

128-
$tmp_dir = Helper::tmpDir();
129-
$tmp_file = $tmp_dir . $basename;
138+
$this->moveTmpFolder($path, $tmp_dir);
130139

131-
$results = $this->save($tmp_file);
140+
return $results;
141+
}
142+
143+
/**
144+
* @param array $config
145+
* @param string $dest
146+
* @param string|null $path
147+
* @return mixed
148+
* @throws Exception
149+
*/
150+
public function saveToS3(array $config, string $dest, string $path = null)
151+
{
152+
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path);
132153
sleep(1);
133154

134155
$aws = new AWS($config);
135156
$aws->uploadAndDownloadDirectory($tmp_dir, $dest);
136157

137-
if(null !== $path){
138-
$destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
139-
Helper::makeDir($destination);
140-
Helper::moveDir($tmp_dir, $destination);
141-
}else{
142-
Helper::deleteDirectory($tmp_dir);
143-
}
158+
$this->moveTmpFolder($path, $tmp_dir);
144159

145160
return $results;
146161
}
@@ -166,4 +181,39 @@ private function deleteOriginalFile()
166181
sleep(1);
167182
@unlink($this->media->getPath());
168183
}
184+
185+
/**
186+
* @param $path
187+
* @return array
188+
* @throws Exception
189+
*/
190+
private function saveToTemporaryFolder($path)
191+
{
192+
$basename = Helper::randomString();
193+
194+
if (null !== $path) {
195+
$basename = pathinfo($path)["basename"];
196+
}
197+
198+
$tmp_dir = Helper::tmpDir();
199+
$tmp_file = $tmp_dir . $basename;
200+
201+
return [$this->save($tmp_file), $tmp_dir];
202+
}
203+
204+
/**
205+
* @param string|null $path
206+
* @param $tmp_dir
207+
* @throws Exception
208+
*/
209+
private function moveTmpFolder(?string $path, $tmp_dir)
210+
{
211+
if (null !== $path) {
212+
$destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
213+
Helper::makeDir($destination);
214+
Helper::moveDir($tmp_dir, $destination);
215+
} else {
216+
Helper::deleteDirectory($tmp_dir);
217+
}
218+
}
169219
}

src/FFMpeg.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function fromURL(string $url, string $save_to = null, string $method = "G
6565

6666
$save_to = Helper::tmpFile($ext);
6767
}
68-
Helper::downloadFile($url, $save_to, $method, $request_options);
68+
$file_manager = new FileManager($url, $method, $request_options);
69+
$file_manager->downloadFile($save_to);
6970

7071
return $this->open($save_to, $is_tmp);
7172
}

0 commit comments

Comments
 (0)