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\Exception\ExceptionInterface; |
15
|
|
|
use FFMpeg\FFMpeg as BFFMpeg; |
16
|
|
|
use FFMpeg\FFProbe; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Streaming\Clouds\AWS; |
19
|
|
|
use Streaming\Clouds\Cloud; |
20
|
|
|
use Streaming\Clouds\CloudInterface; |
21
|
|
|
use Streaming\Clouds\GoogleCloudStorage; |
22
|
|
|
use Streaming\Clouds\MicrosoftAzure; |
23
|
|
|
use Streaming\Exception\Exception; |
24
|
|
|
use Streaming\Exception\InvalidArgumentException; |
25
|
|
|
use Streaming\Exception\RuntimeException; |
26
|
|
|
|
27
|
|
|
class FFMpeg |
28
|
|
|
{ |
29
|
|
|
/** @var BFFMpeg */ |
30
|
|
|
protected $ffmpeg; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $tmp_file; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $ffmpeg |
37
|
|
|
*/ |
38
|
|
|
public function __construct(BFFMpeg $ffmpeg) |
39
|
|
|
{ |
40
|
|
|
$this->ffmpeg = $ffmpeg; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $path |
45
|
|
|
* @param bool $is_tmp |
46
|
|
|
* @return Media |
47
|
|
|
*/ |
48
|
|
|
public function open(string $path, bool $is_tmp = false): Media |
49
|
|
|
{ |
50
|
|
|
if (!is_file($path)) { |
51
|
|
|
throw new InvalidArgumentException("There is no file in this path: " . $path); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
return new Media($this->ffmpeg->open($path), $path, $is_tmp); |
56
|
|
|
} catch (ExceptionInterface $e) { |
57
|
|
|
throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $cloud |
63
|
|
|
* @param string|null $save_to |
64
|
|
|
* @return Media |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function openFromCloud(array $cloud, string $save_to = null): Media |
68
|
|
|
{ |
69
|
|
|
list($is_tmp, $save_to) = $this->isTmp($save_to); |
70
|
|
|
|
71
|
|
|
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) { |
72
|
|
|
$cloud_obj = $cloud['cloud']; |
73
|
|
|
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : []; |
74
|
|
|
|
75
|
|
|
$cloud_obj->download($save_to, $options); |
76
|
|
|
} else { |
77
|
|
|
throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method. |
78
|
|
|
and the cloud must be instance of CloudInterface'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->open($save_to, $is_tmp); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $path |
86
|
|
|
* @return array |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
private function isTmp($path) |
90
|
|
|
{ |
91
|
|
|
$is_tmp = false; |
92
|
|
|
|
93
|
|
|
if (null === $path) { |
94
|
|
|
$is_tmp = true; |
95
|
|
|
$this->tmp_file = $path = FileManager::tmpFile(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [$is_tmp, $path]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $method |
103
|
|
|
* @param $parameters |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function __call($method, $parameters) |
107
|
|
|
{ |
108
|
|
|
return call_user_func_array([$this->ffmpeg, $method], $parameters); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* if there is tmp file, then delete it |
113
|
|
|
*/ |
114
|
|
|
public function __destruct() |
115
|
|
|
{ |
116
|
|
|
if($this->tmp_file){ |
117
|
|
|
@unlink($this->tmp_file); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $config |
123
|
|
|
* @param LoggerInterface $logger |
124
|
|
|
* @param FFProbe|null $probe |
125
|
|
|
* @return FFMpeg |
126
|
|
|
*/ |
127
|
|
|
public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null) |
128
|
|
|
{ |
129
|
|
|
return new static(BFFMpeg::create($config, $logger, $probe)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $url |
134
|
|
|
* @param string|null $save_to |
135
|
|
|
* @param string $method |
136
|
|
|
* @param $request_options |
137
|
|
|
* @return Media |
138
|
|
|
* @throws Exception |
139
|
|
|
* @deprecated this method is deprecated |
140
|
|
|
*/ |
141
|
|
|
// @TODO: should be removed in the next releases. |
142
|
|
|
public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media |
143
|
|
|
{ |
144
|
|
|
@trigger_error('fromURL method is deprecated and will be removed in a future release. Use Cloud instead', E_USER_DEPRECATED); |
145
|
|
|
|
146
|
|
|
Helper::isURL($url); |
|
|
|
|
147
|
|
|
list($is_tmp, $save_to) = $this->isTmp($save_to); |
148
|
|
|
|
149
|
|
|
$cloud = new Cloud($url, $method, $request_options); |
150
|
|
|
$cloud->download($save_to); |
151
|
|
|
|
152
|
|
|
return $this->open($save_to, $is_tmp); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $config |
157
|
|
|
* @param string $bucket |
158
|
|
|
* @param string $key |
159
|
|
|
* @param string|null $save_to |
160
|
|
|
* @return Media |
161
|
|
|
* @throws Exception |
162
|
|
|
* @deprecated this method is deprecated |
163
|
|
|
*/ |
164
|
|
|
// @TODO: should be removed in the next releases. |
165
|
|
|
public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media |
166
|
|
|
{ |
167
|
|
|
@trigger_error('fromS3 method is deprecated and will be removed in a future release. Use AWS instead', E_USER_DEPRECATED); |
168
|
|
|
|
169
|
|
|
list($is_tmp, $save_to) = $this->isTmp($save_to); |
170
|
|
|
|
171
|
|
|
$aws = new AWS($config); |
172
|
|
|
$aws->download($save_to, ['bucket' => $bucket, 'key' => $key]); |
173
|
|
|
|
174
|
|
|
return $this->open($save_to, $is_tmp); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param array $config |
179
|
|
|
* @param string $bucket |
180
|
|
|
* @param string $name |
181
|
|
|
* @param string|null $save_to |
182
|
|
|
* @param bool $userProject |
183
|
|
|
* @return Media |
184
|
|
|
* @throws Exception |
185
|
|
|
* @deprecated this method is deprecated |
186
|
|
|
*/ |
187
|
|
|
// @TODO: should be removed in the next releases. |
188
|
|
|
public function fromGCS(array $config, string $bucket, string $name, string $save_to = null, $userProject = false): Media |
189
|
|
|
{ |
190
|
|
|
@trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED); |
191
|
|
|
list($is_tmp, $save_to) = $this->isTmp($save_to); |
192
|
|
|
|
193
|
|
|
$google_cloud = new GoogleCloudStorage($config, $bucket, $userProject); |
194
|
|
|
$google_cloud->download($save_to, ['name' => $name]); |
195
|
|
|
|
196
|
|
|
return $this->open($save_to, $is_tmp); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $connectionString |
201
|
|
|
* @param string $container |
202
|
|
|
* @param string $blob |
203
|
|
|
* @param string|null $save_to |
204
|
|
|
* @return Media |
205
|
|
|
* @throws Exception |
206
|
|
|
* @deprecated this method is deprecated |
207
|
|
|
*/ |
208
|
|
|
// @TODO: should be removed in the next releases. |
209
|
|
|
public function fromMAS(string $connectionString, string $container, string $blob, string $save_to = null): Media |
210
|
|
|
{ |
211
|
|
|
@trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED); |
212
|
|
|
list($is_tmp, $save_to) = $this->isTmp($save_to); |
213
|
|
|
|
214
|
|
|
$google_cloud = new MicrosoftAzure($connectionString); |
215
|
|
|
$google_cloud->download($save_to, ['container' => $container, 'blob' => $blob]); |
216
|
|
|
|
217
|
|
|
return $this->open($save_to, $is_tmp); |
218
|
|
|
} |
219
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: