Skip to content

Commit ab558f6

Browse files
- custom clouds
- save to multiple clouds - bug fixes and other minor improvements
1 parent 20660a5 commit ab558f6

File tree

10 files changed

+355
-104
lines changed

10 files changed

+355
-104
lines changed

README.md

Lines changed: 175 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ You can pass a local path of video to the `open` method:
7878
$video = $ffmpeg->open('/var/www/media/videos/test.mp4');
7979
```
8080

81-
#### 2. From a Cloud
81+
#### 2. From Clouds
82+
83+
##### Form a Cloud
8284
You can open a file by passing a URL to the `fromURL` method:
8385
``` php
84-
$video = $ffmpeg->fromURL('https://www.aminyazdanpanah.com/my_sweetie.mp4');
86+
$cloud = new \Streaming\Clouds\Cloud('https://www.aminyazdanpanah.com/my_sweetie.mp4');
87+
$from_cloud = ['cloud' => $cloud];
88+
89+
$video = $ffmpeg->openFromCloud($from_cloud);
8590
```
8691

8792
A path to save the file, the method of request, and **[request options](http://docs.guzzlephp.org/en/stable/request-options.html)** can also be passed to the method.
@@ -118,11 +123,14 @@ $options = [
118123
},
119124
];
120125

121-
$video = $ffmpeg->fromURL($api, $save_to, $method, $options);
126+
$cloud = new \Streaming\Clouds\Cloud($api, $method, $options);
127+
$from_cloud = ['cloud' => $cloud];
128+
129+
$video = $ffmpeg->openFromCloud($from_cloud, $save_to);
122130
```
123131
**NOTE:** This package uses **[Guzzle](https://github.com/guzzle/guzzle)** to send and receive files. [Learn more](http://docs.guzzlephp.org/en/stable/index.html).
124132

125-
#### 3. From Amazon S3
133+
##### From Amazon S3
126134
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)
127135
- For getting credentials, you need to have an AWS account or you can **[create one](https://portal.aws.amazon.com/billing/signup#/start)**.
128136

@@ -136,14 +144,23 @@ $config = [
136144
'secret' => 'my-secret-access-key',
137145
]
138146
];
139-
$bucket = 'my-bucket-name';
140-
$key = '/videos/my_sweetie.mp4';
141147

142-
$video = $ffmpeg->fromS3($config, $bucket, $key);
148+
$aws_cloud = new \Streaming\Clouds\AWS($config);
149+
$aws_cloud_download_options = [
150+
'Bucket' => 'my-bucket-name',
151+
'Key' => '/videos/my_sweetie.mp4'
152+
];
153+
154+
$from_aws_cloud = [
155+
'cloud' => $aws_cloud,
156+
'options' => $aws_cloud_download_options
157+
];
158+
159+
$video = $ffmpeg->openFromCloud($from_aws_cloud);
143160
```
144161
A path can also be passed to save the file on your local machine.
145162

146-
#### 4. From Google Cloud Storage
163+
##### From Google Cloud Storage
147164
**[Google Cloud Storage](https://console.cloud.google.com/storage)** is a RESTful online file storage web service for storing and accessing data on Google Cloud Platform infrastructure. The service combines the performance and scalability of Google's cloud with advanced security and sharing capabilities. It is an Infrastructure as a Service (IaaS), comparable to Amazon S3 online storage service. Contrary to Google Drive and according to different service specifications, Google Cloud Storage appears to be more suitable for enterprises. [Learn more](https://en.wikipedia.org/wiki/Google_Storage)
148165
- For creating credentials, read the Cloud Storage Authentication found **[here](https://cloud.google.com/storage/docs/authentication)** or you can **[create it](https://console.cloud.google.com/apis/credentials)** directly (Select the "Service account key" option).
149166

@@ -152,28 +169,93 @@ For downloading a file from Google Cloud Storage, you need to pass an associativ
152169
$config = [
153170
'keyFilePath' => '/path/to/credentials.json' // Alternativaely, you can authenticate by setting the environment variable. See https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-php
154171
];
155-
$bucket = 'my_bucket';
156-
$name = 'my_sweetie.mp4';
172+
$bucket = 'yourbucket';
157173

158-
$video = $ffmpeg->fromGCS($config, $bucket, $name);
174+
$google_cloud = new GoogleCloudStorage($config, $bucket);
175+
$google_cloud_download_options = [
176+
'filename' => 'my_sweetie.mp4',
177+
OTHER_OPTION => OTHER_VALUE_OPTION
178+
];
179+
180+
$from_google_cloud = [
181+
'cloud' => $google_cloud,
182+
'options' => $google_cloud_download_options
183+
];
184+
185+
$video = $ffmpeg->openFromCloud($from_google_cloud);
159186
```
160187
A path can also be passed to save the file on your local machine.
161188

162189

163-
#### 5. From Microsoft Azure Storage
190+
##### From Microsoft Azure Storage
164191
**[Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)** is Microsoft's cloud storage solution for modern data storage scenarios. Azure Storage offers a massively scalable object store for data objects, a file system service for the cloud, a messaging store for reliable messaging, and a NoSQL store. [Learn more](https://docs.microsoft.com/en-us/azure/storage/common/storage-introduction)
165192
- To authenticate the service, please click **[here](https://docs.microsoft.com/en-us/azure/app-service/overview-authentication-authorization)**.
166193

167194
For downloading a file from Microsoft Azure Storage, you need to pass string connection, the name of your container, and the name of your file in the container to the `fromMAS` method:
168195
``` php
169196
$connectionString = 'DefaultEndpointsProtocol=https;AccountName=<yourAccount>;AccountKey=<yourKey>';
170-
$container = 'your_container';
171-
$blob = 'my_sweetie.mp4';
172197

173-
$video = $ffmpeg->fromMAS($connectionString, $container, $blob);
198+
$microsoft_azure = new \Streaming\Clouds\MicrosoftAzure($connectionString);
199+
$microsoft_azure_download_options = [
200+
'container' => 'your_container',
201+
'blob' => 'my_sweetie.mp4'
202+
];
203+
204+
$from_microsoft_azure = [
205+
'cloud' => $microsoft_azure,
206+
'options' => $microsoft_azure_download_options
207+
];
208+
209+
$video = $ffmpeg->openFromCloud($from_microsoft_azure);
174210
```
175211
A path can also be passed to save the file on your local machine.
176212

213+
##### From a Custom Cloud
214+
You can create a custom cloud by implementing `CloudInterface`:
215+
``` php
216+
use Streaming\Clouds\CloudInterface;
217+
218+
class CustomCloud implements CloudInterface
219+
{
220+
221+
/**
222+
* Upload a entire directory to a cloud
223+
* @param string $dir
224+
* @param array $options
225+
*/
226+
public function uploadDirectory(string $dir, array $options): void
227+
{
228+
// TODO: Implement uploadDirectory() method.
229+
}
230+
231+
/**
232+
* Download a file from a cloud
233+
* @param string $save_to
234+
* @param array $options
235+
*/
236+
public function download(string $save_to, array $options): void
237+
{
238+
// TODO: Implement download() method.
239+
}
240+
}
241+
```
242+
243+
After creating your own cloud class, you can easily pass the cloud options to the `openFromCloud` method:
244+
``` php
245+
$custom_cloud = new CustomCloud();
246+
// The array below will be passed to the `download` method in the `CustomCloud` class.
247+
$custom_cloud_download_options = [
248+
YOUR_OPTIONS => YOUR_VALUE_OPTIONS
249+
];
250+
251+
$from_custom_cloud = [
252+
'cloud' => $custom_cloud,
253+
'options' => $custom_cloud_download_options
254+
];
255+
256+
$video = $ffmpeg->openFromCloud($from_custom_cloud);
257+
```
258+
177259
### DASH
178260
**[Dynamic Adaptive Streaming over HTTP (DASH)](http://dashif.org/)**, also known as MPEG-DASH, is an adaptive bitrate streaming technique that enables high quality streaming of media content over the Internet delivered from conventional HTTP web servers.
179261

@@ -328,17 +410,13 @@ $hls->save();
328410
```
329411
**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.
330412

331-
#### 2. To a Cloud
413+
#### 2. To Clouds
414+
415+
##### To a Cloud
332416
You can save your files to a cloud using the `saveToCloud` method.
333417
``` php
334418
$api = 'https://www.aminyazdanpanah.com/api/v1.0/video/uploading';
335-
$field_name = 'MY_FILES';
336419
$method = 'POST';
337-
$headers = [
338-
'User-Agent' => 'Mozilla/5.0 (compatible; AminYazdanpanahBot/1.0; +http://aminyazdanpanah.com/bots)',
339-
'Accept' => 'application/json',
340-
'Authorization' => 'Bearer ACCESS_TOKEN'
341-
];
342420
$current_percentage = 0;
343421
$options = [
344422
'auth' => ['username', 'password', 'digest'],
@@ -358,62 +436,113 @@ $options = [
358436
},
359437
];
360438

361-
$dash->saveToCloud($api, $field_name, null, $method, $headers, $options);
439+
$cloud = new \Streaming\Clouds\Cloud($api, $method, $options);
440+
$cloud_upload_options = [
441+
'name' => MY_FILES_name
442+
'headers' => [
443+
'User-Agent' => 'Mozilla/5.0 (compatible; AminYazdanpanahBot/1.0; +http://aminyazdanpanah.com/bots)',
444+
'Accept' => 'application/json',
445+
'Authorization' => 'Bearer ACCESS_TOKEN'
446+
]
447+
];
448+
449+
$to_cloud = [
450+
'cloud' => $cloud,
451+
'options' => $cloud_upload_options
452+
];
453+
454+
$dash->save(null, $to_cloud);
362455
```
363456
A path can also be passed to save a copy of files on your local machine:
364457
``` php
365-
$save_to = '/var/www/media/videos/hls/test.m3u8';
366-
$hls->saveToCloud($api, $field_name, $save_to, $method, $headers, $options);
458+
$hls->save('/var/www/media/videos/hls/test.m3u8', $to_cloud);
367459
```
368460

369-
#### 3. TO Amazon S3
461+
##### TO Amazon S3
370462
You can save and upload entire packaged video files to **[Amazon S3](https://aws.amazon.com/)**. For uploading files, you need to have credentials.
371463
``` php
372464
$config = [
373-
'version' => 'latest',
374-
'region' => 'us-west-1',
465+
'version' => 'latest',
466+
'region' => 'us-west-1',
375467
'credentials' => [
376-
'key' => 'my-access-key-id',
468+
'key' => 'my-access-key-id',
377469
'secret' => 'my-secret-access-key',
378470
]
379471
];
380-
$dest = 's3://bucket';
381-
```
382-
Upload DASH files to Amazon Simple Storage Service:
383-
``` php
384-
$dash->saveToS3($config, $dest);
472+
473+
$aws_cloud = new \Streaming\Clouds\AWS($config);
474+
$aws_cloud_upload_options = [
475+
'dest' => 's3://bucket'
476+
];
477+
478+
$to_aws_cloud = [
479+
'cloud' => $aws_cloud,
480+
'options' => $aws_cloud_upload_options
481+
];
482+
483+
$dash->save(null, $to_aws_cloud);
385484
```
386485
A path can also be passed to save a copy of files on your local machine.
387486
``` php
388-
$hls->saveToS3($config, $dest, '/var/www/media/videos/hls/test.m3u8');
487+
$hls->save('/var/www/media/videos/hls/test.m3u8', $to_aws_cloud);
389488
```
390489

391-
#### 4. TO Google Cloud Storage
490+
##### TO Google Cloud Storage
392491
You can save and upload entire packaged video files to **[Google Cloud Storage](https://console.cloud.google.com/storage)**. For uploading files, you need to have credentials.
393492
``` php
394493
$config = [
395-
'keyFilePath' => '/path/to/credentials.json'
494+
'keyFilePath' => __DIR__ . "/amin-723b115176e9.json"
495+
];
496+
$bucket = 'staging.amin-158006.appspot.com';
497+
498+
$google_cloud = new \Streaming\Clouds\GoogleCloudStorage($config, $bucket);
499+
// You can add options to the upload/download method(e.g. encryption) or it can be null
500+
$google_cloud_upload_options = [
501+
'encryptionKey' => base64EncryptionKey,
502+
OTHER_OPTION => OTHER_VALUE_OPTIONS
503+
];
504+
505+
$to_google_cloud= [
506+
'cloud' => $google_cloud,
507+
'options' => $google_cloud_upload_options
396508
];
397-
$bucket = 'my_bucket';
398509

399-
$dash->saveToGCS($config, $bucket);
510+
$dash->save(null, $to_google_cloud);
400511
```
401512
A path can also be passed to save a copy of files on your local machine.
402513
``` php
403-
$hls->saveToGCS($config, $bucket, '/var/www/media/videos/hls/test.m3u8');
514+
$hls->save('/var/www/media/videos/hls/test.m3u8', $to_google_cloud);
404515
```
405516

406-
#### 5. TO Microsoft Azure Storage
517+
##### TO Microsoft Azure Storage
407518
You can save and upload the entire files to **[Microsoft Azure Storage](https://azure.microsoft.com/en-us/features/storage-explorer/)**. For uploading files, you need to have credentials.
408519
``` php
409520
$connectionString = 'DefaultEndpointsProtocol=https;AccountName=<yourAccount>;AccountKey=<yourKey>';
410-
$container = 'your_container';
411521

412-
$dash->saveToMAS($connectionString, $container);
522+
$microsoft_azure = new \Streaming\Clouds\MicrosoftAzure($connectionString);
523+
$microsoft_azure_upload_options = [
524+
'container' => 'your_container'
525+
];
526+
527+
$to_microsoft_azure = [
528+
'cloud' => $microsoft_azure,
529+
'options' => $microsoft_azure_upload_options
530+
];
531+
532+
$dash->save(null, $to_microsoft_azure);
413533
```
534+
535+
##### TO a Custom Cloud
536+
You can upload your file to a custom cloud. Please see [Custom Cloud](#from-a-custom-cloud) for more information
537+
538+
##### TO multiple Clouds
539+
You can save your files to multiple clouds:
540+
``` php
541+
$dash->save(null, [$to_aws_cloud, $to_google_cloud, $to_microsoft_azure, $to_custom_cloud]);
542+
```
414543
A path can also be passed to save a copy of files on your local machine.
415544
``` php
416-
$hls->saveToMAS($connectionString, $container, '/var/www/media/videos/hls/test.m3u8');
545+
$hls->save('/var/www/media/videos/hls/test.m3u8', [$to_google_cloud, $to_custom_cloud]);
417546
```
418547

419548
**NOTE:** You can mix opening and saving options together. For instance, you can open a file on your local machine and save packaged files to a Cloud (or vice versa).
@@ -469,6 +598,8 @@ You can use these libraries to play your streams.
469598
- HLS: **[hls.js](https://github.com/video-dev/hls.js)**
470599
- **Android**
471600
- DASH and HLS: **[ExoPlayer](https://github.com/google/ExoPlayer)**
601+
- **Windows, Linux, and macOS**
602+
- DASH and HLS: **[VLC media player](https://github.com/videolan/vlc)**
472603

473604
**NOTE:** You should pass a manifest of streams(e.g. `https://www.aminyazdanpanah.com/videos/dash/lesson-1/test.mpd` or `/videos/hls/lesson-2/test.m3u8` ) to these players.
474605

src/Clouds/AWS.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,37 @@ public function __construct(array $config)
3333
}
3434

3535
/**
36-
* @param string $save_to
36+
* @param string $dir
3737
* @param array $options
38-
* @throws Exception
3938
*/
40-
public function download(string $save_to, array $options): void
39+
public function uploadDirectory(string $dir, array $options): void
4140
{
42-
$bucket = $options['bucket'];
43-
$key = $options['key'];
41+
$dest = $options['dest'];
4442

4543
try {
46-
$file = $this->s3->getObject([
47-
'Bucket' => $bucket,
48-
'Key' => $key
49-
]);
50-
51-
if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) {
52-
$body = $file->get('Body');
53-
file_put_contents($save_to, $body);
54-
} else {
55-
throw new Exception("There is no file in the bucket");
56-
}
44+
$manager = new Transfer($this->s3, $dir, $dest);
45+
$manager->transfer();
5746
} catch (S3Exception $e) {
5847
throw new RuntimeException("There was an error downloading the file.\n error: " . $e->getMessage(), $e->getCode(), $e);
5948
}
6049
}
6150

6251
/**
63-
* @param string $dir
52+
* @param string $save_to
6453
* @param array $options
54+
* @throws Exception
6555
*/
66-
public function uploadDirectory(string $dir, array $options): void
56+
public function download(string $save_to, array $options): void
6757
{
68-
$dest = $options['dest'];
69-
7058
try {
71-
$manager = new Transfer($this->s3, $dir, $dest);
72-
$manager->transfer();
59+
$file = $this->s3->getObject($options);
60+
61+
if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) {
62+
$body = $file->get('Body');
63+
file_put_contents($save_to, $body);
64+
} else {
65+
throw new Exception("There is no file in the bucket");
66+
}
7367
} catch (S3Exception $e) {
7468
throw new RuntimeException("There was an error downloading the file.\n error: " . $e->getMessage(), $e->getCode(), $e);
7569
}

0 commit comments

Comments
 (0)