Skip to content

Commit 705d939

Browse files
add from cloud and fixes some minor bugs
1 parent 176514d commit 705d939

File tree

11 files changed

+254
-130
lines changed

11 files changed

+254
-130
lines changed

README.md

Lines changed: 91 additions & 91 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
},
4949
"require": {
5050
"php": "^7.1",
51-
"php-ffmpeg/php-ffmpeg": "^0.14"
51+
"php-ffmpeg/php-ffmpeg": "^0.14",
52+
"guzzlehttp/guzzle": "~6.0"
5253
},
5354
"scripts": {
5455
"test": "vendor/bin/phpunit"

src/AutoRepresentations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AutoRepresentations
2222
/** @var array side_values
2323
* regular video's heights
2424
*/
25-
private $side_values = [2160, 1080, 720, 480, 240, 144];
25+
private $side_values = [2160, 1080, 720, 480, 360, 240, 144];
2626

2727
/**
2828
* AutoRepresentations constructor.
@@ -31,7 +31,7 @@ class AutoRepresentations
3131
*/
3232
public function __construct(Stream $stream, $side_values)
3333
{
34-
if(null !== $side_values){
34+
if (null !== $side_values) {
3535
$this->side_values = $side_values;
3636
}
3737

src/Export.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Streaming;
1313

14+
use Streaming\Exception\Exception;
1415
use Streaming\Filters\Filter;
1516
use Streaming\Traits\Formats;
1617

@@ -40,10 +41,10 @@ public function __construct(Media $media)
4041
/**
4142
* @param string $path
4243
* @param bool $analyse
43-
* @param bool $delete_original_video
4444
* @return mixed
45+
* @throws Exception
4546
*/
46-
public function save(string $path = null, $analyse = true, $delete_original_video = false)
47+
public function save(string $path = null, $analyse = true)
4748
{
4849
$path = $this->getPath($path);
4950

@@ -58,12 +59,13 @@ public function save(string $path = null, $analyse = true, $delete_original_vide
5859
$path
5960
);
6061

61-
if ($delete_original_video) {
62-
sleep(1);
63-
@unlink($this->media->getPath());
62+
$response = ($analyse) ? (new StreamingAnalytics($this))->analyse() : $path;
63+
64+
if ($this->media->isTmp()) {
65+
$this->deleteOriginalFile();
6466
}
6567

66-
return ($analyse) ? (new StreamingAnalytics($this))->analyse() : $path;
68+
return $response;
6769
}
6870

6971
/**
@@ -76,12 +78,22 @@ abstract protected function getFilter(): Filter;
7678
*/
7779
abstract protected function setFilter();
7880

81+
/**
82+
* @param $path
83+
* @return string
84+
* @throws Exception
85+
*/
7986
private function getPath($path): string
8087
{
8188
if (null !== $path) {
8289
$this->path_info = pathinfo($path);
8390
}
8491

92+
if (null === $path && $this->media->isTmp()) {
93+
$this->deleteOriginalFile();
94+
throw new Exception("You need to specify a path. It is not possible to save to a tmp directory");
95+
}
96+
8597
$dirname = str_replace("\\", "/", $this->path_info["dirname"]);
8698
$filename = substr($this->path_info["filename"], -50);
8799

@@ -113,4 +125,10 @@ public function getMedia()
113125
{
114126
return $this->media;
115127
}
116-
}
128+
129+
private function deleteOriginalFile()
130+
{
131+
sleep(1);
132+
@unlink($this->media->getPath());
133+
}
134+
}

src/FFMpeg.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use FFMpeg\FFMpeg as BFFMpeg;
1515
use FFMpeg\FFProbe;
1616
use Psr\Log\LoggerInterface;
17+
use Streaming\Exception\Exception;
18+
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
19+
use Symfony\Component\Filesystem\Filesystem;
1720

1821
class FFMpeg
1922
{
@@ -29,12 +32,49 @@ public function __construct(BFFMpeg $ffmpeg)
2932
}
3033

3134
/**
32-
* @param $path
35+
* @param string $path
36+
* @param bool $is_tmp
3337
* @return Media
38+
* @throws Exception
3439
*/
35-
public function open($path): Media
40+
public function open(string $path, bool $is_tmp = false): Media
3641
{
37-
return new Media($this->ffmpeg->open($path), $path);
42+
if (!is_file($path)) {
43+
throw new Exception("There is no file in this path: " . $path);
44+
}
45+
46+
return new Media($this->ffmpeg->open($path), $path, $is_tmp);
47+
}
48+
49+
/**
50+
* @param string $url
51+
* @param string|null $save_to
52+
* @param string $method
53+
* @param $request_options
54+
* @return Media
55+
* @throws Exception
56+
*/
57+
public function fromURL(string $url, string $save_to = null, string $method = "GET", $request_options = []): Media
58+
{
59+
$is_tmp = false;
60+
61+
if (null === $save_to) {
62+
$is_tmp = true;
63+
$tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming";
64+
65+
Helper::makeDir($tmp_path);
66+
67+
$ext = "";
68+
if (isset(pathinfo($url)["extension"])) {
69+
$ext = "." . substr(explode("?", pathinfo($url)["extension"])[0], 0, 10);
70+
}
71+
72+
$save_to = $tmp_path . DIRECTORY_SEPARATOR . Helper::randomString() . $ext;
73+
}
74+
75+
Helper::downloadFile($url, $save_to, $method, $request_options);
76+
77+
return $this->open($save_to, $is_tmp);
3878
}
3979

4080
/**
@@ -57,4 +97,4 @@ public static function create($config = array(), LoggerInterface $logger = null,
5797
{
5898
return new static(BFFMpeg::create($config, $logger, $probe));
5999
}
60-
}
100+
}

src/HLS.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class HLS extends Export
2020
use Representations;
2121

2222
/** @var string */
23-
private $hls_time = 5;
23+
private $hls_time = 10;
2424

2525
/** @var bool */
2626
private $hls_allow_cache = true;
@@ -83,12 +83,12 @@ public function setHlsKeyInfoFile(string $hls_key_info_file): HLS
8383
*/
8484
public function generateRandomKeyInfo(string $url = null, string $path = null, string $binary = "openssl"): HLS
8585
{
86-
if (null === $url && null === $path){
86+
if (null === $url && null === $path) {
8787
$key_name = $url = Helper::randomString() . ".key";
8888
$path = $this->path_info["dirname"] . DIRECTORY_SEPARATOR . $key_name;
8989
}
9090

91-
$this->hls_key_info_file = (string) new KeyInfo($url, $path, $binary);
91+
$this->hls_key_info_file = (string)new KeyInfo($url, $path, $binary);
9292
return $this;
9393
}
9494

src/Helper.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
namespace Streaming;
1313

1414

15+
use GuzzleHttp\Client;
16+
use GuzzleHttp\Exception\GuzzleException;
17+
use Streaming\Exception\Exception;
18+
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
19+
use Symfony\Component\Filesystem\Filesystem;
20+
1521
class Helper
1622
{
1723
/**
@@ -25,15 +31,47 @@ public static function roundToEven(float $number): int
2531
return (($number = intval($number)) % 2 == 0) ? $number : $number + 1;
2632
}
2733

28-
2934
/**
3035
* @param $dirname
36+
* @param int $mode
37+
* @throws Exception
38+
*/
39+
public static function makeDir($dirname, $mode = 0777): void
40+
{
41+
$filesystem = new Filesystem();
42+
43+
try {
44+
$filesystem->mkdir($dirname, $mode);
45+
} catch (IOExceptionInterface $exception) {
46+
throw new Exception("An error occurred while creating your directory at " . $exception->getPath());
47+
}
48+
}
49+
50+
/**
51+
* @param string $url
52+
* @param string|null $save_to
53+
* @param string $method
54+
* @param array $request_options
55+
* @throws Exception
3156
*/
32-
public static function makeDir($dirname): void
57+
public static function downloadFile(string $url, string $save_to = null, string $method = "GET", $request_options = []): void
3358
{
34-
if (!is_dir($dirname)) {
35-
mkdir($dirname, 0777, true);
59+
$request_options = array_merge($request_options, ['sink' => $save_to]);
60+
$client = new Client();
61+
try {
62+
$client->request($method, $url, $request_options);
63+
} catch (GuzzleException $e) {
64+
65+
$error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s",
66+
$url,
67+
$e->getCode(),
68+
$e->getMessage(),
69+
$e->getResponse()->getBody()->getContents()
70+
);
71+
72+
throw new Exception($error);
3673
}
74+
3775
}
3876

3977
/**

src/Media.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,23 @@ class Media
2727
* @var string
2828
*/
2929
private $path;
30+
/**
31+
* @var bool
32+
*/
33+
private $is_tmp;
34+
3035

3136
/**
3237
* Media constructor.
3338
* @param MediaTypeInterface $media
3439
* @param string $path
40+
* @param bool $is_tmp
3541
*/
36-
public function __construct(MediaTypeInterface $media, string $path)
42+
public function __construct(MediaTypeInterface $media, string $path, bool $is_tmp)
3743
{
3844
$this->media = $media;
3945
$this->path = $path;
46+
$this->is_tmp = $is_tmp;
4047
}
4148

4249
/**
@@ -99,4 +106,12 @@ public function getPath(): string
99106
{
100107
return $this->path;
101108
}
109+
110+
/**
111+
* @return bool
112+
*/
113+
public function isTmp(): bool
114+
{
115+
return $this->is_tmp;
116+
}
102117
}

src/Process/Process.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Process
2323
protected $commands = [];
2424

2525
/**
26-
* ShakaProcess constructor.
26+
* Process constructor.
2727
* @param $binary
2828
* @throws Exception
2929
*/
@@ -47,7 +47,7 @@ private function getBinary($binary)
4747
if ($binary = $finder->find($binary)) {
4848
return $binary;
4949
} else {
50-
throw new Exception("We could not find 'Shaka Packager' binary.\nPlease check the path to the shaka binary");
50+
throw new Exception("We could not find the binary.\nPlease check the path to the binary");
5151
}
5252
}
5353
}

src/Traits/Representation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public function getRepresentations(): array
4343
}
4444

4545
/**
46-
* @param null $side_values
46+
* @param array $side_values
4747
* @return $this
4848
* @throws Exception
4949
*/
50-
public function autoGenerateRepresentations($side_values = null)
50+
public function autoGenerateRepresentations(array $side_values = null)
5151
{
5252
if (!$this->format) {
5353
throw new Exception('Format has not been set');

0 commit comments

Comments
 (0)