Skip to content

Commit 8c2386e

Browse files
- Bug fixes(support mkv) #12
- Add MediaInfo class - Fixes some minor bugs
1 parent e182312 commit 8c2386e

File tree

11 files changed

+349
-52
lines changed

11 files changed

+349
-52
lines changed

.appveyor.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ environment:
1717
ANSICON: 121x90 (121x90) # Console colors
1818

1919
ffmpeg_download: https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-20190725-923d5c4-win64-static.zip
20+
mediainfo_download: https://mediaarea.net/download/binary/mediainfo/19.07/MediaInfo_CLI_19.07_Windows_x64.zip
2021

2122
matrix:
2223
- PHP_VERSION: Latest_Version
@@ -28,9 +29,12 @@ install:
2829
- ps: Set-Service wuauserv -StartupType Manual # Chocolatey will try to install Windows updates when installing PHP.
2930
- ps: cinst php --params '""/InstallDir:C:\tools\php""' --ignore-checksums
3031
- ps: Start-FileDownload $env:ffmpeg_download
32+
- ps: Start-FileDownload $env:mediainfo_download
3133

3234
- 7z x ffmpeg-20190725-923d5c4-win64-static.zip
3335
- PATH=%PATH%;%cd%\ffmpeg-20190725-923d5c4-win64-static\bin
36+
- 7z x MediaInfo_CLI_19.07_Windows_x64.zip
37+
- PATH=%PATH%;%cd%\MediaInfo_CLI_19.07_Windows_x64
3438
- cd c:\tools\php
3539
- copy php.ini-production php.ini /Y
3640
- echo date.timezone="UTC" >> php.ini
@@ -47,4 +51,5 @@ install:
4751
test_script:
4852
- cd c:\projects\project-code
4953
- ffmpeg -version
54+
- mediainfo --version
5055
- vendor\bin\phpunit tests

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ before_install:
1616
install:
1717
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction
1818
- sudo apt-get install ffmpeg
19+
- sudo apt-get install mediainfo
1920

2021
script:
22+
- ffmpeg -version
23+
- mediainfo --version
2124
- vendor/bin/phpunit tests

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ This package provides an integration with [PHP-FFmpeg](https://github.com/PHP-FF
3939

4040
### Required Libraries
4141

42+
#### 1. FFMpeg
4243
This library requires a working FFMpeg. You will need both FFMpeg and FFProbe binaries to use it.
4344
- Getting FFmpeg: https://ffmpeg.org/download.html
4445

45-
Also, for HLS encryption you will need a working OpenSSL:
46+
#### 2. MediaInfo
47+
For auto generating representations and also for digital media analysis, this package needs a working MediaInfo.
48+
- Getting MediaInfo: https://mediaarea.net/en/MediaInfo
49+
50+
**NOTE:** You must download MediaInfo CLI. If you want this package autodetect the MediaInfo binary, you need to add the path of CLI to your system path. Otherwise, You have to pass the full path of binary to `setMediaInfoBinary` method.
51+
52+
#### 3. OpenSSL
53+
For HLS encryption you will need a working OpenSSL:
4654
- Getting OpenSSL: https://www.openssl.org/source/
4755
- Getting OpenSSL(Windows): https://slproweb.com/products/Win32OpenSSL.html
4856

57+
**NOTE:** Add the path of OpenSSL bin directory to system path to get the benefit of binary autodetect.
4958

5059
### Installing Package
5160
This version of the package is only compatible with PHP 7.1.0 and later.
@@ -232,7 +241,7 @@ Getting OpenSSL: https://www.openssl.org/source/
232241

233242
Getting OpenSSL(Windows): https://slproweb.com/products/Win32OpenSSL.html
234243

235-
You need to pass both 'URL to the key' and 'path to save a random key' to `generateRandomKeyInfo` method:
244+
You need to pass both 'URL to the key' and 'path to save a random key' to the `generateRandomKeyInfo` method:
236245
``` php
237246
//A path you want to save a random key on your server
238247
$save_to = "/var/www/my_website_project/keys/enc.key";
@@ -346,7 +355,7 @@ $dash->saveToCloud($api, $field_name, null, $method, $headers, $options);
346355
It can also be passed a path to save a copy files on the local path:
347356

348357
``` php
349-
$save_to = '/var/www/media/videos/my_sweetie.mp4';
358+
$save_to = '/var/www/media/videos/hls/test.m3u8';
350359
$hls->saveToCloud($api, $field_name, $save_to, $method, $headers, $options);
351360
```
352361

@@ -373,7 +382,7 @@ $dash->saveToS3($config, $dest);
373382
A path can also be passed to save a copy files on your local computer/server.
374383

375384
``` php
376-
$hls->saveToS3($config, $dest, '/var/www/media/videos/dash/test.m3u8');
385+
$hls->saveToS3($config, $dest, '/var/www/media/videos/hls/test.m3u8');
377386
```
378387

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

src/AutoRepresentations.php

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

1414
use Streaming\Exception\Exception;
15-
use FFMpeg\FFProbe\DataMapping\Stream;
15+
use Streaming\MediaInfo\Streams\Stream;
16+
use Streaming\MediaInfo\Streams\StreamCollection;
1617

1718
class AutoRepresentations
1819
{
19-
/** @var Stream $stream */
20-
private $stream;
20+
/** @var Stream $video */
21+
private $video;
22+
23+
/** @var Stream $general */
24+
private $general;
2125

2226
/** @var array side_values
2327
* regular video's heights
@@ -26,25 +30,26 @@ class AutoRepresentations
2630

2731
/**
2832
* AutoRepresentations constructor.
29-
* @param Stream $stream
33+
* @param StreamCollection $streamCollection
3034
* @param null | array $side_values
3135
*/
32-
public function __construct(Stream $stream, $side_values)
36+
public function __construct(StreamCollection $streamCollection, $side_values)
3337
{
3438
if (null !== $side_values) {
3539
$this->side_values = $side_values;
3640
}
3741

38-
$this->stream = $stream;
42+
$this->video = $streamCollection->videos()->first();
43+
$this->general = $streamCollection->general();
3944
}
4045

4146
/**
4247
* @return array
4348
*/
4449
private function getDimensions(): array
4550
{
46-
$width = $this->stream->get('width');
47-
$height = $this->stream->get('height');
51+
$width = $this->video->get('Width');
52+
$height = $this->video->get('Height');
4853

4954
return [$width, $height, $width / $height];
5055
}
@@ -55,10 +60,14 @@ private function getDimensions(): array
5560
*/
5661
private function getKiloBitRate(): int
5762
{
58-
if (!$this->stream->has('bit_rate')) {
59-
throw new Exception("Invalid stream");
63+
if (!$this->video->has('BitRate')) {
64+
if (!$this->general->has('OverallBitRate')) {
65+
throw new Exception("Invalid stream");
66+
}
67+
68+
return (int)($this->general->get('OverallBitRate') / 1024) * .9;
6069
}
61-
return (int)$this->stream->get('bit_rate') / 1024;
70+
return (int)$this->video->get('BitRate') / 1024;
6271
}
6372

6473
/**

src/Export.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ abstract class Export
2828
/** @var array */
2929
protected $path_info;
3030

31+
/** @var string */
32+
protected $mediaInfoBinary = 'mediainfo';
33+
3134
/**
3235
* Export constructor.
3336
* @param Media $media
@@ -59,7 +62,7 @@ public function save(string $path = null, $analyse = true)
5962
$path
6063
);
6164

62-
$response = ($analyse) ? (new StreamingAnalytics($this))->analyse() : $path;
65+
$response = ($analyse) ? (new StreamingAnalytics($this, $this->mediaInfoBinary))->analyse() : $this;
6366

6467
if ($this->media->isTmp()) {
6568
$this->deleteOriginalFile();
@@ -113,10 +116,11 @@ private function getPath($path): string
113116
/**
114117
* @param string $url
115118
* @param string $name
116-
* @param array $headers
117119
* @param string|null $path
118120
* @param string $method
121+
* @param array $headers
119122
* @param array $options
123+
* @param bool $analyse
120124
* @return mixed
121125
* @throws Exception
122126
*/
@@ -126,10 +130,11 @@ public function saveToCloud(
126130
string $path = null,
127131
string $method = 'GET',
128132
array $headers = [],
129-
array $options = []
133+
array $options = [],
134+
bool $analyse = true
130135
)
131136
{
132-
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path);
137+
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse);
133138
sleep(1);
134139

135140
$file_manager = new FileManager($url, $method, $options);
@@ -144,12 +149,13 @@ public function saveToCloud(
144149
* @param array $config
145150
* @param string $dest
146151
* @param string|null $path
152+
* @param bool $analyse
147153
* @return mixed
148154
* @throws Exception
149155
*/
150-
public function saveToS3(array $config, string $dest, string $path = null)
156+
public function saveToS3(array $config, string $dest, string $path = null, bool $analyse =true)
151157
{
152-
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path);
158+
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse);
153159
sleep(1);
154160

155161
$aws = new AWS($config);
@@ -184,10 +190,11 @@ private function deleteOriginalFile()
184190

185191
/**
186192
* @param $path
193+
* @param $analyse
187194
* @return array
188195
* @throws Exception
189196
*/
190-
private function saveToTemporaryFolder($path)
197+
private function saveToTemporaryFolder($path, $analyse)
191198
{
192199
$basename = Helper::randomString();
193200

@@ -198,7 +205,7 @@ private function saveToTemporaryFolder($path)
198205
$tmp_dir = Helper::tmpDir();
199206
$tmp_file = $tmp_dir . $basename;
200207

201-
return [$this->save($tmp_file), $tmp_dir];
208+
return [$this->save($tmp_file, $analyse), $tmp_dir];
202209
}
203210

204211
/**
@@ -216,4 +223,14 @@ private function moveTmpFolder(?string $path, $tmp_dir)
216223
Helper::deleteDirectory($tmp_dir);
217224
}
218225
}
226+
227+
/**
228+
* @param string $mediaInfoBinary
229+
* @return Export
230+
*/
231+
public function setMediaInfoBinary(string $mediaInfoBinary)
232+
{
233+
$this->mediaInfoBinary = $mediaInfoBinary;
234+
return $this;
235+
}
219236
}

src/Media.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,6 @@ public function __call($method, $parameters)
8383
);
8484
}
8585

86-
/**
87-
* @return mixed
88-
*/
89-
public function getVideoStream(): Stream
90-
{
91-
return $this->media->getStreams()->videos()->first();
92-
}
93-
94-
/**
95-
* @return mixed
96-
*/
97-
public function getAllStreams()
98-
{
99-
return $this->media->getStreams()->all();
100-
}
101-
10286
/**
10387
* @return string
10488
*/

src/MediaInfo/MediaInfo.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\MediaInfo;
14+
15+
16+
use Streaming\MediaInfo\Streams\Stream;
17+
use Streaming\MediaInfo\Streams\StreamCollection;
18+
use Streaming\Process\Process;
19+
20+
class MediaInfo
21+
{
22+
/**
23+
* @param string|null $binary
24+
* @param string $path
25+
* @return StreamCollection
26+
* @throws \Streaming\Exception\Exception
27+
*/
28+
public static function initialize(string $path, string $binary = 'mediainfo'): StreamCollection
29+
{
30+
$media_info = json_decode(static::getJsonOutPut(new Process($binary), $path), true);
31+
32+
$streams = $media_info["media"]["track"];
33+
$stream_collection = [];
34+
35+
foreach ($streams as $stream){
36+
$stream_collection[] = new Stream($stream);
37+
}
38+
39+
return new StreamCollection($stream_collection);
40+
}
41+
42+
/**
43+
* @param Process $media_info
44+
* @param string $path
45+
* @return string
46+
* @throws \Streaming\Exception\Exception
47+
*/
48+
private static function getJsonOutPut(Process $media_info, string $path): string
49+
{
50+
return $media_info->addCommand(['--Output=JSON', $path])->run();
51+
}
52+
}

0 commit comments

Comments
 (0)