1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
5
|
|
|
* |
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
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
|
|
|
namespace Streaming; |
13
|
|
|
|
14
|
|
|
use FFMpeg\FFMpeg as BFFMpeg; |
15
|
|
|
use FFMpeg\FFProbe; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Streaming\Exception\Exception; |
18
|
|
|
|
19
|
|
|
class FFMpeg |
20
|
|
|
{ |
21
|
|
|
/** @var BFFMpeg */ |
22
|
|
|
protected $ffmpeg; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $ffmpeg |
26
|
|
|
*/ |
27
|
|
|
public function __construct(BFFMpeg $ffmpeg) |
28
|
|
|
{ |
29
|
|
|
$this->ffmpeg = $ffmpeg; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $path |
34
|
|
|
* @param bool $is_tmp |
35
|
|
|
* @return Media |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function open(string $path, bool $is_tmp = false): Media |
39
|
|
|
{ |
40
|
|
|
if (!is_file($path)) { |
41
|
|
|
throw new Exception("There is no file in this path: " . $path); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new Media($this->ffmpeg->open($path), $path, $is_tmp); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $url |
49
|
|
|
* @param string|null $save_to |
50
|
|
|
* @param string $method |
51
|
|
|
* @param $request_options |
52
|
|
|
* @return Media |
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media |
56
|
|
|
{ |
57
|
|
|
$is_tmp = false; |
58
|
|
|
|
59
|
|
|
if (null === $save_to) { |
60
|
|
|
$is_tmp = true; |
61
|
|
|
$ext = ""; |
62
|
|
|
if (isset(pathinfo($url)["extension"])) { |
63
|
|
|
$ext = substr(explode("?", pathinfo($url)["extension"])[0], 0, 10); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$save_to = Helper::tmpFile($ext); |
67
|
|
|
} |
68
|
|
|
Helper::downloadFile($url, $save_to, $method, $request_options); |
69
|
|
|
|
70
|
|
|
return $this->open($save_to, $is_tmp); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $config |
76
|
|
|
* @param string $bucket |
77
|
|
|
* @param string $key |
78
|
|
|
* @param string|null $save_to |
79
|
|
|
* @return Media |
80
|
|
|
* @throws Exception |
81
|
|
|
*/ |
82
|
|
|
public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media |
83
|
|
|
{ |
84
|
|
|
$is_tmp = false; |
85
|
|
|
|
86
|
|
|
if (null === $save_to) { |
87
|
|
|
$is_tmp = true; |
88
|
|
|
$save_to = Helper::tmpFile(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$aws = new AWS($config); |
92
|
|
|
$aws->downloadFile($bucket, $key, $save_to); |
93
|
|
|
|
94
|
|
|
return $this->open($save_to, $is_tmp); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $method |
99
|
|
|
* @param $parameters |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function __call($method, $parameters) |
103
|
|
|
{ |
104
|
|
|
return call_user_func_array([$this->ffmpeg, $method], $parameters); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $config |
109
|
|
|
* @param LoggerInterface $logger |
110
|
|
|
* @param FFProbe|null $probe |
111
|
|
|
* @return FFMpeg |
112
|
|
|
*/ |
113
|
|
|
public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null) |
114
|
|
|
{ |
115
|
|
|
return new static(BFFMpeg::create($config, $logger, $probe)); |
116
|
|
|
} |
117
|
|
|
} |