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