Skip to content

Commit e6cc461

Browse files
remove deprecated and unused methods, bug fixes, and other minor improvements
1 parent 8e077c7 commit e6cc461

File tree

9 files changed

+57
-480
lines changed

9 files changed

+57
-480
lines changed

README.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ $ffmpeg = Streaming\FFMpeg::create($config);
7070
```
7171

7272
### Opening a File
73-
There are several ways to open a file:
73+
There are two ways to open a file:
7474

7575
#### 1. From a Local Path
7676
You can pass a local path of video to the `open` method:
@@ -92,29 +92,38 @@ $video = $ffmpeg->openFromCloud($from_google_cloud);
9292

9393
Similar to Apple's HTTP Live Streaming (HLS) solution, MPEG-DASH works by breaking the content into a sequence of small HTTP-based file segments, each segment containing a short interval of playback time of content that is potentially many hours in duration, such as a movie or the live broadcast of a sports event. The content is made available at a variety of different bit rates, i.e., alternative segments encoded at different bit rates covering aligned short intervals of playback time. While the content is being played back by an MPEG-DASH client, the client uses a bit rate adaptation (ABR) algorithm to automatically select the segment with the highest bit rate possible that can be downloaded in time for playback without causing stalls or re-buffering events in the playback. The current MPEG-DASH reference client dash.js offers both buffer-based (BOLA) and hybrid (DYNAMIC) bit rate adaptation algorithms. Thus, an MPEG-DASH client can seamlessly adapt to changing network conditions and provide high quality playback with fewer stalls or re-buffering events. [Learn more](https://en.wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP)
9494

95-
Create DASH Files:
95+
Create DASH files:
9696
``` php
9797
$video->DASH()
9898
->HEVC() // Format of the video. Alternatives: X264() and VP9()
9999
->autoGenerateRepresentations() // Auto generate representations
100100
->setAdaption('id=0,streams=v id=1,streams=a') // Set the adaption.
101101
->save(); // It can be passed a path to the method or it can be null
102102
```
103-
You can also create multi-representations video files using the `Representation` object:
103+
Generate representations manually:
104104
``` php
105105
use Streaming\Representation;
106106

107-
$rep_1 = (new Representation())->setKiloBitrate(800)->setResize(1280 , 720);
108-
$rep_2 = (new Representation())->setKiloBitrate(300)->setResize(640 , 360);
107+
$rep_144 = (new Representation)->setKiloBitrate(95)->setResize(256 , 144);
108+
$rep_240 = (new Representation)->setKiloBitrate(150)->setResize(426 , 240);
109+
$rep_360 = (new Representation)->setKiloBitrate(276)->setResize(640 , 360);
110+
$rep_480 = (new Representation)->setKiloBitrate(750)->setResize(854 , 480);
111+
$rep_720 = (new Representation)->setKiloBitrate(2048)->setResize(1280 , 720);
112+
$rep_1080 = (new Representation)->setKiloBitrate(4096)->setResize(1920 , 1080);
113+
$rep_1440 = (new Representation)->setKiloBitrate(6096)->setResize(2560 , 1440);
109114

110115
$video->DASH()
111116
->HEVC()
112-
->addRepresentation($rep_1) // Add a representation
113-
->addRepresentation($rep_2)
117+
->addRepresentation($rep_144)// add a representation
118+
->addRepresentation($rep_240)
119+
->addRepresentation($rep_360)
120+
->addRepresentation($rep_480)
121+
->addRepresentation($rep_720)
122+
->addRepresentation($rep_1080)
123+
->addRepresentation($rep_1440)
114124
->setAdaption('id=0,streams=v id=1,streams=a') // Set a adaption.
115125
->save('/var/www/media/videos/dash-stream.mpd');
116126
```
117-
See **[DASH options](https://ffmpeg.org/ffmpeg-formats.html#dash-2)** for more information.
118127

119128
### HLS
120129
**[HTTP Live Streaming (also known as HLS)](https://developer.apple.com/streaming/)** is an HTTP-based adaptive bitrate streaming communications protocol implemented by Apple Inc. as part of its QuickTime, Safari, OS X, and iOS software. Client implementations are also available in Microsoft Edge, Firefox and some versions of Google Chrome. Support is widespread in streaming media servers.
@@ -128,20 +137,20 @@ $video->HLS()
128137
->autoGenerateRepresentations([720, 360]) // You can limit the numbers of representatons
129138
->save();
130139
```
131-
Generate `Representation` object(set bit-rate and size manually):
140+
Generate representations manually:
132141
``` php
133142
use Streaming\Representation;
134143

135-
$rep_1 = (new Representation())->setKiloBitrate(1000)->setResize(1280 , 720);
136-
$rep_2 = (new Representation())->setKiloBitrate(500)->setResize(854 , 480);
137-
$rep_3 = (new Representation())->setKiloBitrate(200)->setResize(640 , 360);
144+
$rep_360 = (new Representation)->setKiloBitrate(276)->setResize(640 , 360);
145+
$rep_480 = (new Representation)->setKiloBitrate(750)->setResize(854 , 480);
146+
$rep_720 = (new Representation)->setKiloBitrate(2048)->setResize(1280 , 720);
138147

139148
$video->HLS()
140149
->X264()
141150
->setHlsBaseUrl('https://bucket.s3-us-west-1.amazonaws.com/videos') // Add a base URL
142-
->addRepresentation($rep_1)
143-
->addRepresentation($rep_2)
144-
->addRepresentation($rep_3)
151+
->addRepresentation($rep_360)
152+
->addRepresentation($rep_480)
153+
->addRepresentation($rep_720)
145154
->setHlsTime(5) // Set Hls Time. Default value is 10
146155
->setHlsAllowCache(false) // Default value is true
147156
->save();
@@ -151,7 +160,7 @@ $video->HLS()
151160
#### Encrypted HLS
152161
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)
153162

154-
You need to pass both `URL to the key` and `path to save a random key` to the `generateRandomKeyInfo` method:
163+
You need to pass both `A URL to the key` and `A path to save a random key on your local machine` to the `generateRandomKeyInfo` method:
155164
``` php
156165
//A path you want to save a random key on your server
157166
$save_to = '/var/www/my_website_project/keys/enc.key';
@@ -168,16 +177,14 @@ $video->HLS()
168177
```
169178
**NOTE:** It is very important to protect your key on your website using a token or a session/cookie(****It is highly recommended****).
170179

171-
See **[HLS options](https://ffmpeg.org/ffmpeg-formats.html#hls-2)** for more information.
172180

173181
### Transcoding
174182
A format can also extend `FFMpeg\Format\ProgressableInterface` to get realtime information about the transcoding.
175183

176184
``` php
177185
$format = new Streaming\Format\HEVC();
178-
$start_time = time();
179-
180186
$start_time = 0;
187+
181188
$percentage_to_time_left = function ($percentage) use (&$start_time) {
182189
if($start_time === 0){
183190
$start_time = time();
@@ -206,7 +213,7 @@ $video->DASH()
206213
![transcoding](/docs/transcoding.gif?raw=true "transcoding" )
207214

208215
### Saving Files
209-
There are several options to save your files.
216+
There are two ways to save your files.
210217

211218
#### 1. To a Local Path
212219
You can pass a local path to the `save` method. If there was no directory in the path, then the package auto makes the directory.
@@ -231,7 +238,7 @@ $hls->save();
231238
#### 2. To Clouds
232239
You can save your files to a cloud by passing an array of cloud configuration to the `save` method.
233240

234-
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.
241+
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.
235242
``` php
236243
$dash->save(null, [$to_aws_cloud, $to_google_cloud, $to_microsoft_azure, $to_custom_cloud]);
237244
```
@@ -240,7 +247,7 @@ A path can also be passed to save a copy of files on your local machine.
240247
$hls->save('/var/www/media/videos/hls-stream.m3u8', [$to_google_cloud, $to_custom_cloud]);
241248
```
242249

243-
**NOTE:** You can open a file from your local machine(or a cloud) and save files to a local path or a cloud(or multiple clouds) or both.
250+
**Schema:** The relation is `one-to-many`
244251

245252
<p align="center"><img src="https://github.com/aminyazdanpanah/aminyazdanpanah.github.io/blob/master/video-streaming/video-streaming.gif?raw=true" width="100%"></p>
246253

@@ -253,7 +260,7 @@ $metadata = $hls->save();
253260
echo $metadata['filename']; // path to metadata.json
254261
var_dump($metadata['metadata']); // dump all metadata
255262
```
256-
**NOTE:** It won't save metadata to clouds because of some security concerns.
263+
**NOTE:** It won't save metadata to clouds because of some security reasons.
257264

258265
### Other Advanced Features
259266
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 the `open` method(or `openFromCloud`), it holds the Media object that belongs to the PHP-FFMpeg.
@@ -268,7 +275,7 @@ You can extract a frame at any timecode using the `FFMpeg\Media\Video::frame` me
268275
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(42));
269276
$frame->save('image.jpg');
270277
```
271-
To see more example, please go to the **[PHP-FFMpeg Documentation](https://github.com/PHP-FFMpeg/PHP-FFMpeg)**
278+
To see more example, please vist the **[PHP-FFMpeg Documentation](https://github.com/PHP-FFMpeg/PHP-FFMpeg)** page.
272279

273280
## Asynchronous Task Execution
274281
Packaging process will may take a while and it is recommended to run it in the background(or in a cloud e.g. Google Cloud). There are some libraries that you can use.

src/AutoRepresentations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function get(): array
8484
$reps[] = $this->addRep($k_bitrate, $w, $h);
8585

8686
foreach ($this->side_values as $key => $height) {
87-
$reps[] = $this->addRep($this->k_bitrate_values[$key], Helper::roundToEven($r * $height), $height);
87+
$reps[] = $this->addRep($this->k_bitrate_values[$key], Utilities::roundToEven($r * $height), $height);
8888
}
8989

9090
return array_reverse($reps);

src/Clouds/CloudManager.php

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CloudManager
2222
* @param array $clouds
2323
* @param string $tmp_dir
2424
*/
25-
public static function saveToClouds(?array $clouds, ?string $tmp_dir): void
25+
public static function uploadDirectory(?array $clouds, ?string $tmp_dir): void
2626
{
2727
if (!$clouds) {
2828
return;
@@ -32,20 +32,9 @@ public static function saveToClouds(?array $clouds, ?string $tmp_dir): void
3232
$clouds = [$clouds];
3333
}
3434

35-
sleep(1);
36-
3735
foreach ($clouds as $cloud) {
38-
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
39-
$cloud_obj = $cloud['cloud'];
40-
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
41-
42-
$cloud_obj->uploadDirectory($tmp_dir, $options);
43-
} else {
44-
throw new InvalidArgumentException('You must pass an array of clouds to the save method.
45-
and the cloud must be instance of CloudInterface');
46-
}
36+
static::transfer($cloud, __FUNCTION__, $tmp_dir);
4737
}
48-
4938
}
5039

5140
/**
@@ -54,38 +43,27 @@ public static function saveToClouds(?array $clouds, ?string $tmp_dir): void
5443
* @return array
5544
* @throws \Streaming\Exception\Exception
5645
*/
57-
public static function downloadFromCloud(array $cloud, string $save_to = null): array
46+
public static function download(array $cloud, string $save_to = null): array
5847
{
59-
list($is_tmp, $save_to) = static::isTmp($save_to);
60-
61-
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
62-
$cloud_obj = $cloud['cloud'];
63-
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
64-
65-
$cloud_obj->download($save_to, $options);
66-
} else {
67-
throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method.
68-
and the cloud must be instance of CloudInterface');
69-
}
48+
list($save_to, $is_tmp) = $save_to ? [$save_to, false] : [FileManager::tmpFile(), true];
49+
static::transfer($cloud, __FUNCTION__, $save_to);
7050

7151
return [$save_to, $is_tmp];
7252
}
7353

7454
/**
55+
* @param $cloud
56+
* @param $method
7557
* @param $path
76-
* @return array
77-
* @throws \Streaming\Exception\Exception
7858
*/
79-
private static function isTmp($path)
59+
private static function transfer($cloud, $method, $path): void
8060
{
81-
$is_tmp = false;
82-
83-
if (null === $path) {
84-
$is_tmp = true;
85-
$path = FileManager::tmpFile();
61+
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
62+
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
63+
call_user_func_array([$cloud['cloud'], $method], [$path, $options]);
64+
} else {
65+
throw new InvalidArgumentException('You must pass an array of clouds to the save method.
66+
and the cloud must be instance of CloudInterface');
8667
}
87-
88-
return [$is_tmp, $path];
8968
}
90-
9169
}

0 commit comments

Comments
 (0)