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