Skip to content

Commit e1ec1ea

Browse files
- Add AWS library
- Fixes some minor bugs - Performance is improved
1 parent 63c9eab commit e1ec1ea

File tree

8 files changed

+329
-85
lines changed

8 files changed

+329
-85
lines changed

README.md

Lines changed: 108 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
This package provides an integration with [PHP-FFmpeg](https://github.com/PHP-FFMpeg/PHP-FFMpeg) and packages well-known live streaming techniques such as DASH and HLS. Also you can use DRM for HLS packaging.
1313

1414
- Before you get started, please read the FFMpeg Document found **[here](https://ffmpeg.org/ffmpeg-formats.html)**.
15-
- **[Full API Documentation](https://video.aminyazdanpanah.com/)** is available describing all features and components.
15+
- **[Full Documentation](https://video.aminyazdanpanah.com/)** is available describing all features and components.
1616
- For DRM and encryption(DASH and HLS), I **strongly recommend** to try **[Shaka PHP](https://github.com/aminyazdanpanah/shaka-php)**, which is a great tool for this use case.
1717

1818
**Contents**
@@ -22,18 +22,13 @@ This package provides an integration with [PHP-FFmpeg](https://github.com/PHP-FF
2222
- [Usage](#usage)
2323
- [Configuration](#configuration)
2424
- [Opening a File](#opening-a-file)
25-
- [From Local](#1-from-local)
26-
- [From Cloud](#2-from-cloud)
2725
- [DASH](#dash)
28-
- [Create DASH Files](#create-dash-files)
29-
- [Transcoding(DASH)](#transcodingdash)
3026
- [HLS](#hls)
3127
- [Create HLS Files](#create-hls-files)
32-
- [Transcoding(HLS)](#transcodinghls)
3328
- [Encrypted HLS](#encrypted-hls)
29+
- [Transcoding](#transcoding)
30+
- [Save to Amazon S3](#)
3431
- [Other Advanced Features](#other-advanced-features)
35-
- [Extracting image](#extracting-image)
36-
- [Watermark](#watermark)
3732
- [Several Open Source Players](#several-open-source-players)
3833
- [Contributing](#contributing)
3934
- [Security](#security)
@@ -76,14 +71,14 @@ $config = [
7671
'ffprobe.binaries' => '/usr/bin/ffprobe',
7772
'timeout' => 3600, // The timeout for the underlying process
7873
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
79-
];
74+
];
8075

8176
$ffmpeg = Streaming\FFMpeg::create($config);
8277
```
8378

8479
### Opening a File
8580

86-
There are two ways to open a file:
81+
There are three ways to open a file:
8782

8883
#### 1. From Local
8984
You can pass a local path of video to the `open` method:
@@ -101,6 +96,32 @@ $video = $ffmpeg->fromURL("https://www.aminyazdanpanah.com/my_sweetie.mp4");
10196

10297
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.
10398

99+
#### 3. From Amazon S3
100+
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)
101+
102+
- For getting credentials, you need to have an AWS account or you can [create one](https://portal.aws.amazon.com/billing/signup#/start).
103+
- Before you get started, please read the "AWS SDK for PHP" Document found **[here](https://aws.amazon.com/sdk-for-php/)**.
104+
105+
For downloading a file from Amazon S3, you need to pass an array as configuration, name of the bucket, and the key of your bucket to `fromS3` method:
106+
107+
``` php
108+
$config = [
109+
'version' => 'latest',
110+
'region' => 'us-west-1',
111+
'credentials' => [
112+
'key' => 'my-access-key-id',
113+
'secret' => 'my-secret-access-key',
114+
]
115+
];
116+
117+
$bucket = "my-bucket-name";
118+
$key = "/videos/my_sweetie.mp4";
119+
120+
$video = $ffmpeg->fromS3($config, $bucket, $key);
121+
```
122+
123+
A path can also be passed to save the file on your local computer/server.
124+
104125
### DASH
105126
**[Dynamic Adaptive Streaming over HTTP (DASH)](https://en.wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP)**, 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.
106127

@@ -109,7 +130,7 @@ Similar to Apple's HTTP Live Streaming (HLS) solution, MPEG-DASH works by breaki
109130
#### Create DASH Files
110131
``` php
111132
$video->DASH()
112-
->HEVC() // Format of the video. For Using another format, see Traits\Formats
133+
->HEVC() // Format of the video. Alternatives: X264() and VP9()
113134
->autoGenerateRepresentations() // Auto generate representations
114135
->setAdaption('id=0,streams=v id=1,streams=a') // Set the adaption.
115136
->save(); // It can be passed a path to the method or it can be null
@@ -122,38 +143,13 @@ $rep_1 = (new Representation())->setKiloBitrate(800)->setResize(1080 , 720);
122143
$rep_2 = (new Representation())->setKiloBitrate(300)->setResize(640 , 360);
123144

124145
$video->DASH()
125-
->HEVC() // Format of the video.For Using another format, see Traits\Formats
146+
->HEVC()
126147
->addRepresentation($rep_1) // Add a representation
127148
->addRepresentation($rep_2) // Add a representation
128149
->setAdaption('id=0,streams=v id=1,streams=a') // Set a adaption.
129-
->save(); // It can be passed a path to the method or it can be null
130-
131-
```
132-
133-
134-
#### Transcoding(DASH)
135-
136-
You can transcode videos using the `on` method in the format class.
137-
138-
Transcoding progress can be monitored in realtime, see Format documentation in [FFMpeg documentation](https://github.com/PHP-FFMpeg/PHP-FFMpeg#documentation) for more information.
139-
140-
``` php
141-
$format = new Streaming\Format\HEVC();
142-
143-
$format->on('progress', function ($video, $format, $percentage) {
144-
echo "$percentage% is transcoded\n";
145-
});
146-
147-
$video->DASH()
148-
->setFormat($format)
149-
->autoGenerateRepresentations()
150-
->setAdaption('id=0,streams=v id=1,streams=a')
151-
->save('/var/www/media/videos/dash/test.mpd');
152-
150+
->save('/var/www/media/videos/dash/test.mpd'); // It can be passed a path to the method or it can be null
153151
```
154152

155-
156-
157153
For more information about **[FFMpeg](https://ffmpeg.org/)** and its **[dash options](https://ffmpeg.org/ffmpeg-formats.html#dash-2)** please visit its website.
158154

159155
### HLS
@@ -187,22 +183,10 @@ $video->HLS()
187183
->setHlsAllowCache(false) // Default value is true
188184
->save();
189185
```
190-
See [HLS options](https://ffmpeg.org/ffmpeg-formats.html#hls-2) for more information.
191186

192-
#### Transcoding(HLS)
193-
194-
``` php
195-
$format = new Streaming\Format\X264();
196-
197-
$format->on('progress', function ($video, $format, $percentage) {
198-
echo "$percentage% is transcoded\n";
199-
});
187+
**NOTE:** You cannot use HEVC and VP9 formats for HLS packaging.
200188

201-
$video->HLS()
202-
->setFormat($format)
203-
->autoGenerateRepresentations()
204-
->save('/var/www/media/videos/dash/test.m3u8');
205-
```
189+
See [HLS options](https://ffmpeg.org/ffmpeg-formats.html#hls-2) for more information.
206190

207191
#### Encrypted HLS
208192

@@ -234,6 +218,76 @@ $video->HLS()
234218
- **NOTE:** It is very important to protect your key on your website using a token or a session/cookie(****It is highly recommended****).
235219
- **NOTE:** For getting the benefit of the OpenSSL binary detection in windows, you need to add it to your system path otherwise, you have to pass the path to OpenSSL binary to the `generateRandomKeyInfo` method explicitly.
236220

221+
### Transcoding
222+
223+
You can transcode videos using the `on` method in the format class.
224+
225+
Transcoding progress can be monitored in realtime, see Format documentation in [FFMpeg documentation](https://github.com/PHP-FFMpeg/PHP-FFMpeg#documentation) for more information.
226+
227+
``` php
228+
$format = new Streaming\Format\HEVC();
229+
230+
$format->on('progress', function ($video, $format, $percentage) {
231+
echo "$percentage% is transcoded\n";
232+
});
233+
234+
$video->DASH()
235+
->setFormat($format)
236+
->autoGenerateRepresentations()
237+
->setAdaption('id=0,streams=v id=1,streams=a')
238+
->save();
239+
```
240+
241+
HLS Transcoding:
242+
243+
``` php
244+
$format = new Streaming\Format\X264();
245+
246+
$format->on('progress', function ($video, $format, $percentage) {
247+
echo "$percentage% is transcoded\n";
248+
});
249+
250+
$video->HLS()
251+
->setFormat($format)
252+
->autoGenerateRepresentations()
253+
->save('/var/www/media/videos/dash/test.m3u8');
254+
```
255+
256+
### Save to Amazon S3
257+
You can save and upload entire packaged video files to [Amazon S3](https://aws.amazon.com/). For uploading files, you need to have credentials.
258+
259+
``` php
260+
$config = [
261+
'version' => 'latest',
262+
'region' => 'us-west-1',
263+
'credentials' => [
264+
'key' => 'my-access-key-id',
265+
'secret' => 'my-secret-access-key',
266+
]
267+
];
268+
269+
$dest = 's3://bucket';
270+
```
271+
272+
Upload DASH files to Amazon Simple Storage Service:
273+
``` php
274+
$video->DASH()
275+
->HEVC()
276+
->autoGenerateRepresentations()
277+
->setAdaption('id=0,streams=v id=1,streams=a')
278+
->saveToS3($config, $dest);
279+
```
280+
A filename can also be passed to save files on your local computer/server.
281+
282+
``` php
283+
$video->HLS()
284+
->X264()
285+
->autoGenerateRepresentations()
286+
->saveToS3($config, $dest, '/var/www/media/videos/dash/test.m3u8');
287+
```
288+
289+
For more information, please read [AWS SDK for PHP](https://aws.amazon.com/sdk-for-php/) document.
290+
237291
### Other Advanced Features
238292
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.
239293

@@ -244,7 +298,7 @@ $ffmpeg = Streaming\FFMpeg::create()
244298
$video = $$ffmpeg->fromURL("https://www.aminyazdanpanah.com/my_sweetie.mp4", "/var/wwww/media/my/new/video.mp4");
245299
```
246300

247-
#### Extracting image
301+
#### Example(Extracting image)
248302
You can extract a frame at any timecode using the `FFMpeg\Media\Video::frame` method.
249303

250304
``` php
@@ -257,21 +311,8 @@ $video
257311
->save(new FFMpeg\Format\Video\X264(), '/path/to/new/file');
258312
```
259313

260-
#### Watermark
261-
Watermark a video with a given image.
262-
263-
``` php
264-
$video
265-
->filters()
266-
->watermark($watermarkPath, array(
267-
'position' => 'absolute',
268-
'x' => 1180,
269-
'y' => 620,
270-
));
271-
```
272-
273314
## Several Open Source Players
274-
You can use these player to play your packaged videos
315+
You can use these players to play your packaged videos
275316
- **WEB**
276317
- DASH and HLS: [Plyr](https://github.com/sampotts/plyr)
277318
- DASH and HLS: [MediaElement.js](https://github.com/mediaelement/mediaelement)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"require": {
5050
"php": "^7.1",
5151
"php-ffmpeg/php-ffmpeg": "^0.14",
52-
"guzzlehttp/guzzle": "~6.0"
52+
"guzzlehttp/guzzle": "~6.0",
53+
"aws/aws-sdk-php": "~3.0"
5354
},
5455
"scripts": {
5556
"test": "vendor/bin/phpunit"

src/AWS.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP-FFmpeg-video-streaming package.
5+
*
6+
* (c) Amin Yazdanpanah <contact@aminyazdanpanah.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Streaming;
14+
15+
16+
use Aws\S3\Exception\S3Exception;
17+
use Aws\S3\S3Client;
18+
use Aws\S3\Transfer;
19+
use Streaming\Exception\Exception;
20+
21+
class AWS
22+
{
23+
private $s3;
24+
25+
/**
26+
* AWS constructor.
27+
* @param $config
28+
*/
29+
public function __construct(array $config)
30+
{
31+
$this->s3 = new S3Client($config);;
32+
}
33+
34+
/**
35+
* @param $bucket
36+
* @param $key
37+
* @param $filename
38+
* @param string $acl
39+
* @return mixed
40+
* @throws Exception
41+
*/
42+
public function uploadFile(string $bucket, string $key, string $filename, string $acl = 'public-read'): string
43+
{
44+
try {
45+
$result = $this->s3->putObject([
46+
'Bucket' => $bucket,
47+
'Key' => $key,
48+
'Body' => fopen($filename, 'r'),
49+
'ACL' => $acl,
50+
]);
51+
52+
return isset($result['ObjectURL']) ? $result['ObjectURL'] : "It is private";
53+
} catch (S3Exception $e) {
54+
throw new Exception("There was an error uploading the file.\n error: " . $e->getMessage());
55+
}
56+
}
57+
58+
/**
59+
* @param $bucket
60+
* @param $key
61+
* @param $filename
62+
* @throws Exception
63+
*/
64+
public function downloadFile(string $bucket, string $key, string $filename): void
65+
{
66+
try {
67+
$file = $this->s3->getObject([
68+
'Bucket' => $bucket,
69+
'Key' => $key
70+
]);
71+
72+
if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) {
73+
$body = $file->get('Body');
74+
file_put_contents($filename, $body);
75+
} else {
76+
throw new Exception("There is no file in the bucket");
77+
}
78+
} catch (S3Exception $e) {
79+
throw new Exception("There was an error downloading the file.\n error: " . $e->getMessage());
80+
}
81+
}
82+
83+
/**
84+
* @param $source
85+
* @param $dest
86+
*/
87+
public function uploadAndDownloadDirectory(string $source, string $dest): void
88+
{
89+
$manager = new Transfer($this->s3, $source, $dest);
90+
$manager->transfer();
91+
}
92+
}

0 commit comments

Comments
 (0)