|
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 Streaming\Exception\Exception; |
|
15
|
|
|
use Streaming\Exception\InvalidArgumentException; |
|
16
|
|
|
use Streaming\Filters\Filter; |
|
17
|
|
|
use Streaming\Traits\Formats; |
|
18
|
|
|
|
|
19
|
|
|
abstract class Export |
|
20
|
|
|
{ |
|
21
|
|
|
use Formats; |
|
22
|
|
|
|
|
23
|
|
|
/** @var object */ |
|
24
|
|
|
protected $media; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Filter */ |
|
27
|
|
|
protected $filter; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
protected $path_info; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $strict = "-2"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Export constructor. |
|
37
|
|
|
* @param Media $media |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Media $media) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->media = $media; |
|
42
|
|
|
$this->path_info = pathinfo($media->getPath()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @param bool $analyse |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
* @throws Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function save(string $path = null, $analyse = true) |
|
52
|
|
|
{ |
|
53
|
|
|
$path = $this->getPath($path); |
|
54
|
|
|
|
|
55
|
|
|
$this->setFilter(); |
|
56
|
|
|
|
|
57
|
|
|
$this->media->addFilter( |
|
58
|
|
|
$this->getFilter() |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$this->media->save( |
|
62
|
|
|
$this->getFormat(), |
|
63
|
|
|
$path |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$response = ($analyse) ? (new StreamingAnalytics($this))->analyse() : $this; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->media->isTmp()) { |
|
69
|
|
|
$this->deleteOriginalFile(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $response; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Filter |
|
77
|
|
|
*/ |
|
78
|
|
|
abstract protected function getFilter(): Filter; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract protected function setFilter(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $path |
|
87
|
|
|
* @return string |
|
88
|
|
|
* @throws Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getPath($path): string |
|
91
|
|
|
{ |
|
92
|
|
|
if (null !== $path) { |
|
93
|
|
|
$this->path_info = pathinfo($path); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (null === $path && $this->media->isTmp()) { |
|
97
|
|
|
$this->deleteOriginalFile(); |
|
98
|
|
|
throw new InvalidArgumentException("You need to specify a path. It is not possible to save to the tmp directory"); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$dirname = str_replace("\\", "/", $this->path_info["dirname"]); |
|
102
|
|
|
$filename = substr($this->path_info["filename"], -50); |
|
103
|
|
|
|
|
104
|
|
|
FileManager::makeDir($dirname); |
|
105
|
|
|
|
|
106
|
|
|
if ($this instanceof DASH) { |
|
107
|
|
|
$path = $dirname . "/" . $filename . ".mpd"; |
|
108
|
|
|
} elseif ($this instanceof HLS) { |
|
109
|
|
|
$representations = $this->getRepresentations(); |
|
110
|
|
|
$path = $dirname . "/" . $filename . "_" . end($representations)->getHeight() . "p.m3u8"; |
|
111
|
|
|
ExportHLSPlaylist::savePlayList($dirname . DIRECTORY_SEPARATOR . $filename . ".m3u8", $this->getRepresentations(), $filename); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $path; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $url |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
* @param string|null $path |
|
121
|
|
|
* @param string $method |
|
122
|
|
|
* @param array $headers |
|
123
|
|
|
* @param array $options |
|
124
|
|
|
* @param bool $analyse |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
* @throws Exception |
|
127
|
|
|
*/ |
|
128
|
|
|
public function saveToCloud( |
|
129
|
|
|
string $url, |
|
130
|
|
|
string $name, |
|
131
|
|
|
string $path = null, |
|
132
|
|
|
string $method = 'GET', |
|
133
|
|
|
array $headers = [], |
|
134
|
|
|
array $options = [], |
|
135
|
|
|
bool $analyse = true |
|
136
|
|
|
) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($this instanceof HLS && $this->getTsSubDirectory()) { |
|
139
|
|
|
throw new InvalidArgumentException("It is not possible to create subdirectory in cloud saving"); |
|
140
|
|
|
} |
|
141
|
|
|
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse); |
|
142
|
|
|
sleep(1); |
|
143
|
|
|
|
|
144
|
|
|
$file_manager = new FileManager($url, $method, $options); |
|
145
|
|
|
$file_manager->uploadDirectory($tmp_dir, $name, $headers); |
|
146
|
|
|
|
|
147
|
|
|
$this->moveTmpFolder($path, $tmp_dir); |
|
148
|
|
|
|
|
149
|
|
|
return $results; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param array $config |
|
154
|
|
|
* @param string $dest |
|
155
|
|
|
* @param string|null $path |
|
156
|
|
|
* @param bool $analyse |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
* @throws Exception |
|
159
|
|
|
*/ |
|
160
|
|
|
public function saveToS3(array $config, string $dest, string $path = null, bool $analyse = true) |
|
161
|
|
|
{ |
|
162
|
|
|
list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse); |
|
163
|
|
|
sleep(1); |
|
164
|
|
|
|
|
165
|
|
|
$aws = new AWS($config); |
|
166
|
|
|
$aws->uploadAndDownloadDirectory($tmp_dir, $dest); |
|
167
|
|
|
|
|
168
|
|
|
$this->moveTmpFolder($path, $tmp_dir); |
|
169
|
|
|
|
|
170
|
|
|
return $results; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getPathInfo(): array |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->path_info; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return object|Media |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getMedia(): Media |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->media; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
private function deleteOriginalFile() |
|
190
|
|
|
{ |
|
191
|
|
|
sleep(1); |
|
192
|
|
|
@unlink($this->media->getPath()); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param $path |
|
197
|
|
|
* @param $analyse |
|
198
|
|
|
* @return array |
|
199
|
|
|
* @throws Exception |
|
200
|
|
|
*/ |
|
201
|
|
|
private function saveToTemporaryFolder($path, $analyse) |
|
202
|
|
|
{ |
|
203
|
|
|
$basename = Helper::randomString(); |
|
204
|
|
|
|
|
205
|
|
|
if (null !== $path) { |
|
206
|
|
|
$basename = pathinfo($path)["basename"]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$tmp_dir = FileManager::tmpDir(); |
|
210
|
|
|
$tmp_file = $tmp_dir . $basename; |
|
211
|
|
|
|
|
212
|
|
|
return [$this->save($tmp_file, $analyse), $tmp_dir]; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param string|null $path |
|
217
|
|
|
* @param $tmp_dir |
|
218
|
|
|
* @throws Exception |
|
219
|
|
|
*/ |
|
220
|
|
|
private function moveTmpFolder(?string $path, $tmp_dir) |
|
221
|
|
|
{ |
|
222
|
|
|
if (null !== $path) { |
|
223
|
|
|
$destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR; |
|
224
|
|
|
FileManager::makeDir($destination); |
|
225
|
|
|
FileManager::moveDir($tmp_dir, $destination); |
|
226
|
|
|
} else { |
|
227
|
|
|
FileManager::deleteDirectory($tmp_dir); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $strict |
|
233
|
|
|
* @return Export |
|
234
|
|
|
*/ |
|
235
|
|
|
public function setStrict(string $strict): Export |
|
236
|
|
|
{ |
|
237
|
|
|
$this->strict = $strict; |
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getStrict(): string |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->strict; |
|
247
|
|
|
} |
|
248
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: