1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Transcoder plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Transcode videos to various formats, and provide thumbnails of the video |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace nystudio107\transcoder\variables; |
12
|
|
|
|
13
|
|
|
use nystudio107\transcoder\Transcoder; |
14
|
|
|
|
15
|
|
|
use craft\helpers\UrlHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author nystudio107 |
19
|
|
|
* @package Transcode |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class TranscoderVariable |
23
|
|
|
{ |
24
|
|
|
// Public Methods |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns a URL to the transcoded video or "" if it doesn't exist (at which |
29
|
|
|
* time it will create it). |
30
|
|
|
* |
31
|
|
|
* @param $filePath |
32
|
|
|
* @param $videoOptions |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* @throws \yii\base\Exception |
36
|
|
|
*/ |
37
|
|
|
public function getVideoUrl($filePath, $videoOptions): string |
38
|
|
|
{ |
39
|
|
|
$result = Transcoder::$plugin->transcode->getVideoUrl($filePath, $videoOptions); |
40
|
|
|
|
41
|
|
|
return $result; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a URL to a video thumbnail |
46
|
|
|
* |
47
|
|
|
* @param $filePath |
48
|
|
|
* @param $thumbnailOptions |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
* @throws \yii\base\Exception |
52
|
|
|
*/ |
53
|
|
|
public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string |
54
|
|
|
{ |
55
|
|
|
$result = Transcoder::$plugin->transcode->getVideoThumbnailUrl($filePath, $thumbnailOptions); |
56
|
|
|
|
57
|
|
|
return $result; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a URL to the transcoded audio file or "" if it doesn't exist |
62
|
|
|
* (at which time it will create it). |
63
|
|
|
* |
64
|
|
|
* @param $filePath |
65
|
|
|
* @param $audioOptions |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
* @throws \yii\base\Exception |
69
|
|
|
*/ |
70
|
|
|
public function getAudioUrl($filePath, $audioOptions): string |
71
|
|
|
{ |
72
|
|
|
$result = Transcoder::$plugin->transcode->getAudioUrl($filePath, $audioOptions); |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Extract information from a video/audio file |
79
|
|
|
* |
80
|
|
|
* @param $filePath |
81
|
|
|
* @param bool $summary |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* @throws \yii\base\Exception |
85
|
|
|
*/ |
86
|
|
|
public function getFileInfo($filePath, $summary = false): array |
87
|
|
|
{ |
88
|
|
|
$result = Transcoder::$plugin->transcode->getFileInfo($filePath, $summary); |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a video progress URL |
95
|
|
|
* |
96
|
|
|
* @param $filePath |
97
|
|
|
* @param $videoOptions |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
* @throws \yii\base\Exception |
101
|
|
|
*/ |
102
|
|
|
public function getVideoProgressUrl($filePath, $videoOptions): string |
103
|
|
|
{ |
104
|
|
|
$result = ""; |
105
|
|
|
$filename = Transcoder::$plugin->transcode->getVideoFileName($filePath, $videoOptions); |
106
|
|
|
if (!empty($filename)) { |
107
|
|
|
$urlParams = [ |
108
|
|
|
'filename' => $filename, |
109
|
|
|
]; |
110
|
|
|
$result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get an audio progress URL |
118
|
|
|
* |
119
|
|
|
* @param $filePath |
120
|
|
|
* @param $audioOptions |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
* @throws \yii\base\Exception |
124
|
|
|
*/ |
125
|
|
|
public function getAudioProgressUrl($filePath, $audioOptions): string |
126
|
|
|
{ |
127
|
|
|
$result = ""; |
128
|
|
|
$filename = Transcoder::$plugin->transcode->getAudioFileName($filePath, $audioOptions); |
129
|
|
|
if (!empty($filename)) { |
130
|
|
|
$urlParams = [ |
131
|
|
|
'filename' => $filename, |
132
|
|
|
]; |
133
|
|
|
$result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get a GIF progress URL |
141
|
|
|
* |
142
|
|
|
* @param $filePath |
143
|
|
|
* @param $videoOptions |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
* @throws \yii\base\Exception |
147
|
|
|
*/ |
148
|
|
|
public function getGifProgressUrl($filePath, $gifOptions): string |
149
|
|
|
{ |
150
|
|
|
$result = ""; |
151
|
|
|
$filename = Transcoder::$plugin->transcode->getGifFilename($filePath, $gifOptions); |
152
|
|
|
if (!empty($filename)) { |
153
|
|
|
$urlParams = [ |
154
|
|
|
'filename' => $filename, |
155
|
|
|
]; |
156
|
|
|
$result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get a download URL |
164
|
|
|
* |
165
|
|
|
* @param $url |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getDownloadUrl($url): string |
170
|
|
|
{ |
171
|
|
|
$result = ""; |
172
|
|
|
$filePath = parse_url($url, PHP_URL_PATH); |
173
|
|
|
$filePath = $_SERVER['DOCUMENT_ROOT'] . $filePath; |
174
|
|
|
if (file_exists($filePath)) { |
175
|
|
|
$urlParams = [ |
176
|
|
|
'url' => $url, |
177
|
|
|
]; |
178
|
|
|
$result = UrlHelper::actionUrl('transcoder/default/download-file', $urlParams); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns a URL to a GIF file |
186
|
|
|
* |
187
|
|
|
* @param $filePath |
188
|
|
|
* @param $gifOptions |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
* @throws \yii\base\Exception |
192
|
|
|
*/ |
193
|
|
|
public function getGifUrl($filePath, $gifOptions): string |
194
|
|
|
{ |
195
|
|
|
$result = Transcoder::$plugin->transcode->getGifUrl($filePath, $gifOptions); |
196
|
|
|
|
197
|
|
|
return $result; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|