-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputfieldVideo.module.php
More file actions
421 lines (335 loc) · 18.8 KB
/
InputfieldVideo.module.php
File metadata and controls
421 lines (335 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php namespace ProcessWire;
/**
* ProcessWire Video Inputfieldtype
* by Adrian Jones
*
* Copyright (C) 2020 by Adrian Jones
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
*/
use \Char0n\FFMpegPHP\Adapters\FFMpegMovie as ffmpeg_movie;
class InputfieldVideo extends InputfieldFile {
public static function getModuleInfo() {
return array(
'title' => __('Video Inputfield', __FILE__),
'summary' => __('Inputfield for uploading video files and creating poster images.', __FILE__),
'version' => '0.2.3',
'author' => 'Adrian Jones',
'href' => 'https://processwire.com/talk/topic/4580-video-fieldtype/',
'icon' => 'file-video-o',
'requires' => array("FieldtypeVideo")
);
}
public function init() {
parent::init();
$this->set('extensions', 'mp4');
$this->set('copyToImageField', 0);
$this->set('numPosterImages', 1);
$this->set('adminThumbs', false);
$this->set('imageField', '');
$this->set('maxWidth', '');
$this->set('maxHeight', '');
$this->set('adminThumbHeight', 100);
$this->set('itemClass', 'InputfieldFile InputfieldImage InputfieldVideo ui-widget');
}
public function ___render() {
// version number
$moduleInfo = $this->getModuleInfo();
$v = $moduleInfo['version'];
$modUrl = $this->wire('config')->urls->siteModules . 'FieldtypeVideo/';
// add styles and scripts
$this->wire('config')->scripts->add($modUrl . 'image-picker/image-picker.min.js?v=' . $v);
$this->wire('config')->styles->add($modUrl . 'image-picker/image-picker.css?v=' . $v);
$this->wire('config')->scripts->add($modUrl . 'InputfieldVideo.js?v=' . $v);
$this->wire('config')->styles->add($modUrl . 'InputfieldVideo.css?v=' . $v);
$this->wire('config')->scripts->add($this->wire('config')->urls->InputfieldFile . "InputfieldFile.js");
$this->wire('config')->styles->add($this->wire('config')->urls->InputfieldFile . "InputfieldFile.css");
return parent::___render();
}
/**
* Create poster image(s)
*
*/
protected function ___fileAdded(Pagefile $pagefile) {
if(pathinfo($pagefile->filename, PATHINFO_EXTENSION) == 'mp4') {
if(!extension_loaded('gd')) {
$this->error($this->_('GD extension is required to create poster images from videos.'));
return parent::___fileAdded($pagefile);
}
$this->loadPhpFfmpeg();
try {
$media = new ffmpeg_movie($pagefile->filename);
$frame_count = $media->getFrameCount();
} catch(\UnexpectedValueException $e) {
$this->error(sprintf($this->_('Video file not found: %s'), $pagefile->basename));
return parent::___fileAdded($pagefile);
} catch(\RuntimeException $e) {
if($e->getCode() === 334560) {
$this->error($this->_('FFmpeg is not installed on the server. Install FFmpeg to enable video poster generation.'));
} else {
$this->error(sprintf($this->_('Unable to read video file (%s): %s'), $pagefile->basename, $e->getMessage()));
}
return parent::___fileAdded($pagefile);
} catch(\Exception $e) {
$this->error(sprintf($this->_('Unable to read video file (%s): %s'), $pagefile->basename, $e->getMessage()));
return parent::___fileAdded($pagefile);
}
$frame_number = 0;
$frames = array();
$fraction = (1/$this->numPosterImages);
$frame_number = $frame_number + $fraction;
for ($frame_number = 1; $frame_number <= $this->numPosterImages; $frame_number++) {
$frames[] = round($frame_count * $frame_number / 10);
}
$i=1;
foreach($frames as $frame) {
$img = $this->appendFilename(htmlspecialchars(str_replace('.mp4', '.jpg', $pagefile->filename)), '-'.$i);
if(!file_exists($img)) {
try {
$selected_frame = $media->getFrame($frame);
$gd_image = $selected_frame ? $selected_frame->toGDImage() : false;
if($gd_image) {
imagejpeg($gd_image, $img, 83);
if(PHP_VERSION_ID < 80000) imagedestroy($gd_image);
}
} catch(\Exception $e) {
$this->error(sprintf($this->_('Unable to extract frame %d from %s: %s'), $frame, $pagefile->basename, $e->getMessage()));
}
}
$i++;
}
}
return parent::___fileAdded($pagefile);
}
protected function ___renderItem($pagefile, $id, $n) {
$this->loadPhpFfmpeg();
$sanitizer = $this->wire('sanitizer');
$video = $pagefile;
$duration = '';
try {
$media = new ffmpeg_movie($video->filename);
$duration = gmdate("H:i:s", intval($media->getDuration()));
} catch(\UnexpectedValueException $e) {
$duration = '—';
$this->warning(sprintf($this->_('Video file not found: %s'), $video->basename));
} catch(\RuntimeException $e) {
$duration = '—';
if($e->getCode() === 334560) {
$this->error($this->_('FFmpeg is not installed on the server. Video poster generation and duration display are unavailable.'));
} else {
$this->warning(sprintf($this->_('Unable to read video duration for %s: %s'), $video->basename, $e->getMessage()));
}
} catch(\Exception $e) {
$duration = '—';
$this->warning(sprintf($this->_('Unable to read video duration for %s: %s'), $video->basename, $e->getMessage()));
}
$basenameAttr = $sanitizer->entities($pagefile->basename);
$urlAttr = $sanitizer->entities($pagefile->url);
$displayName = $this->getDisplayBasename($pagefile); // already safe (PW-sanitized basename, plus …)
$deleteLabel = $sanitizer->entities($this->labels['delete']);
$out =
"<p class='InputfieldFileInfo InputfieldItemHeader ui-state-default ui-widget-header'>" .
wireIconMarkupFile($pagefile->basename, "fa-fw HideIfEmpty") . ' ' .
"<a class='InputfieldFileName' title='$basenameAttr' target='_blank' href='$urlAttr'>$displayName</a> " .
"<span class='InputfieldFileStats'>• " . str_replace(' ', ' ', $sanitizer->entities($pagefile->filesizeStr)) . " • " . $sanitizer->entities($duration) . "</span>
<label class='InputfieldFileDelete'>" .
"<input type='checkbox' name='delete_$id' value='1' title='$deleteLabel' />" .
"<i class='fa fa-fw fa-trash'></i>" .
"</label>";
$out .= "\n\t\t\n\t\t<p class='InputfieldFileData ui-widget ui-widget-content'>" . ($this->numPosterImages > 1 ? "Select the image you want to use for the video poster / cover" : "") .
"\n\t\t" . ($this->wire('user')->isSuperuser() ? 'In templates, you can access ' . ($this->numPosterImages > 1 ? "the selected" : "this") . ' poster image ('.$sanitizer->entities(pathinfo(str_replace('mp4', 'jpg', $pagefile->filename), PATHINFO_BASENAME)).') using: <code>$page->'.$sanitizer->entities($this->name).'->'.($this->maxFiles == 1 ? '' : 'eq('.(int)$n.')->').'poster</code>' : '') . '<br /><br />';
$multiplePosters = $this->numPosterImages > 1;
if($multiplePosters) $out .= '<select id="poster_'.$id.'" name="poster_'.$id.'" class="required image-picker show-labels show-html">';
for ($i = 1; $i <= $this->numPosterImages; $i++) {
$poster_path = $this->appendFilename(str_replace('mp4', 'jpg', $video->filename), '-'.$i);
$poster_url = $this->appendFilename(str_replace('mp4', 'jpg', $video->url), '-'.$i);
$thumb_path = $this->appendFilename($poster_path, '-thumb');
$thumb_url = $this->appendFilename($poster_url, '-thumb');
$imgInfo = file_exists($poster_path) ? @getimagesize($poster_path) : false;
if($imgInfo === false) {
// poster image not yet generated or unreadable - skip
continue;
}
list($width, $height) = $imgInfo;
if($this->adminThumbs && $height > $this->adminThumbHeight && !file_exists($thumb_path)) {
// create a variation for display with this inputfield
$this->make_thumb($poster_path, $thumb_path, $this->adminThumbHeight);
}
if($this->copyToImageField == 1 && $this->imageField != '') { //if checked and images field supplied in field's Input tab settings, add this image to the defined images field
$original_image = $this->appendFilename(str_replace('mp4', 'jpg', $video->filename), '-'.$i);
$image_field_version = $this->appendFilename($this->appendFilename(str_replace('mp4', 'jpg', $video->filename), '-'.$i), '-imagefield');
copy($original_image, $image_field_version);
$pageId = $this->wire('input')->get->int('id');
$current_page = $this->wire('pages')->get($pageId);
if($current_page->id) {
$current_page->{$this->imageField}->add($image_field_version);
$current_page->save();
}
}
$displayUrl = $sanitizer->entities(($this->adminThumbs && ($height > $this->adminThumbHeight)) ? $thumb_url : $poster_url);
$posterBase = $sanitizer->entities(pathinfo($poster_path, PATHINFO_BASENAME));
if($multiplePosters) {
$selected = ($pagefile->poster == pathinfo($poster_path, PATHINFO_BASENAME)) ? " selected" : "";
$out .= "<option data-img-src='$displayUrl' id='poster_$id' value='$posterBase'$selected></option>";
} else {
$out .= "<input type='hidden' name='poster_$id' id='poster_$id' value='$posterBase' />"
. "<img class='image_picker_image' src='$displayUrl' alt='$basenameAttr' />";
}
}
if($multiplePosters) $out .= '</select>';
$subtitles = $sanitizer->entities($pagefile->subtitles);
$vttBase = $sanitizer->entities(pathinfo(str_replace('mp4', 'vtt', $pagefile->filename), PATHINFO_BASENAME));
$fieldName = $sanitizer->entities($this->name);
$eqSegment = $this->maxFiles == 1 ? '' : 'eq('.(int)$n.')->';
$out .= "\n\t\t\t" . $this->renderItemDescriptionField($pagefile, $id, $n) .
"\n\t\t<br /><label class='InputfieldFileDescription'><span class='detail'>Subtitles</span>" .
"\n\t\t" . ($this->wire('user')->isSuperuser() ? '<br /><br />In templates, you can access this subtitles file ('.$vttBase.') using: <code>$page->'.$fieldName.'->'.$eqSegment.'subtitles</code>' : '') .
'<br />In templates you can access a formatted transcript (converted from subtitles entered in VTT format), by using: <code>$page->'.$fieldName.'->'.$eqSegment.'transcript</code>' .
"\n\t\t<br /><br /><textarea rows='10' name='subtitles_$id'>$subtitles</textarea>" .
"\n\t\t\t<input class='InputfieldFileSort' type='text' name='sort_$id' value='".(int)$n."' />" .
"\n\t\t</p>";
return $out;
}
/**
* Get a basename for the file, possibly shortened, suitable for display in InputfieldFileList
*
* @param Pagefile $pagefile
* @param int $maxLength
* @return string
*
*/
public function getDisplayBasename(Pagefile $pagefile, $maxLength = 25) {
$displayName = $pagefile->basename;
if($this->noShortName) return $displayName;
if(strlen($displayName) > $maxLength) {
$ext = ".$pagefile->ext";
$maxLength -= (strlen($ext) + 1);
$displayName = basename($displayName, $ext);
$displayName = substr($displayName, 0, $maxLength);
$displayName .= "…" . ltrim($ext, '.');
}
return $displayName;
}
protected function ___processInputFile(WireInputData $input, Pagefile $pagefile, $n) {
$id = $this->name . '_' . $pagefile->hash;
$changed = false;
foreach(array('description', 'tags', 'poster', 'subtitles') as $key) {
if(isset($input[$key . '_' . $id])) {
$value = trim($input[$key . '_' . $id]);
if($value != $pagefile->$key) {
$pagefile->$key = $value;
$changed = true;
}
}
}
// write subtitles to a file that can be used by video player - currently only supporting vtt. Should make format optional, as well as add multi-language files
if($pagefile->subtitles != '') {
$vttPath = str_replace('mp4', 'vtt', $pagefile->filename);
if(@file_put_contents($vttPath, $pagefile->subtitles) === false) {
$this->error(sprintf($this->_('Unable to write subtitles file: %s'), basename($vttPath)));
}
}
if(isset($input['delete_' . $id])) {
$this->processInputDeleteFile($pagefile);
$changed = true;
}
$key = "sort_$id";
$val = (int) $input->$key;
if($val !== NULL) {
$pagefile->sort = $val;
if($n !== $val) $changed = true;
}
return $changed;
}
private function loadPhpFfmpeg() {
static $registered = false;
if($registered) return;
$baseDir = __DIR__ . '/ffmpeg-php/';
spl_autoload_register(function($class) use ($baseDir) {
$prefix = 'Char0n\\FFMpegPHP\\';
if(strpos($class, $prefix) !== 0) return;
$relative = substr($class, strlen($prefix));
$file = $baseDir . str_replace('\\', '/', $relative) . '.php';
if(is_file($file)) require_once $file;
});
$registered = true;
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$field = $this->wire('modules')->get('InputfieldCheckbox');
$field->attr('name', 'adminThumbs');
$field->attr('value', 1);
$field->attr('checked', $this->adminThumbs ? 'checked' : '');
$field->label = $this->_('Display thumbnails in page editor?');
$field->description = $this->_('Thumbnails take up less space and make it easier to sort multiple images. If unchecked, the full (original) size image will be shown in the page editor.'); // Display thumbnails description
$inputfields->add($field);
$field = $this->wire('modules')->get("InputfieldText");
$field->attr('name', 'numPosterImages');
$field->attr('value', $this->numPosterImages);
$field->label = $this->_("Number of poster images to generate");
$field->description = $this->_('Images will be captured from throughout the video. This determines how many will be created. The user can choose which one is available to templates via $page->'.$this->name.'->poster');
$field->notes = $this->_("It is not recommended to make more than 10 poster images.");
$inputfields->add($field);
$field = $this->wire('modules')->get('InputfieldCheckbox');
$field->attr('name', 'copyToImageField');
$field->attr('value', 0);
$field->attr('checked', $this->copyToImageField ? 'checked' : '');
$field->label = $this->_('Copy poster image to dedicated image field?');
$field->description = $this->_('This will create a copy of the poster images in an image field of your choice. NB This is not necessary for accessing the image.');
$inputfields->add($field);
$field = $this->wire('modules')->get("InputfieldSelect");
$field->attr('name', 'imageField');
$field->attr('value', $this->imageField);
$field->required = true;
$field->showIf="copyToImageField=1";
$field->requiredIf="copyToImageField=1";
$field->label = $this->_("Field that you want to have the poster image copied into");
$field->addOption('');
foreach($this->wire('fields') as $fieldoption) {
if($fieldoption->type == "FieldtypeImage") $field->addOption($fieldoption->name);
}
$inputfields->add($field);
$fieldset = $this->wire('modules')->get('InputfieldFieldset');
$fieldset->label = $this->_("Max Image Dimensions");
$fieldset->collapsed = $this->maxWidth || $this->maxHeight ? Inputfield::collapsedNo : Inputfield::collapsedYes;
$fieldset->description = $this->_("Optionally enter the max width and/or height of uploaded images. If specified, images will be resized at upload time when they exceed either the max width or height. The resize is performed at upload time, and thus does not affect any images in the system, or images added via the API."); // Max image dimensions description
$field = $this->wire('modules')->get("InputfieldInteger");
$field->attr('name', 'maxWidth');
$field->attr('value', $this->maxWidth ? (int) $this->maxWidth : '');
$field->label = $this->_("Max width for uploaded images");
$field->description = $this->_("Enter the value in number of pixels or leave blank for no max.");
$fieldset->add($field);
$field = $this->wire('modules')->get("InputfieldInteger");
$field->attr('name', 'maxHeight');
$field->attr('value', $this->maxHeight ? (int) $this->maxHeight : '');
$field->label = $this->_("Max height for uploaded images");
$field->description = $this->_("Enter the value in number of pixels or leave blank for no max.");
$fieldset->add($field);
$inputfields->add($fieldset);
return $inputfields;
}
public function make_thumb($src, $dest, $desired_height) {
if(!extension_loaded('gd')) return false;
$source_image = @imagecreatefromjpeg($src);
if(!$source_image) return false;
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_width = floor($width * ($desired_height / $height));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
if(!$virtual_image) {
if(PHP_VERSION_ID < 80000) imagedestroy($source_image);
return false;
}
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
$result = imagejpeg($virtual_image, $dest, 83);
if(PHP_VERSION_ID < 80000) {
imagedestroy($source_image);
imagedestroy($virtual_image);
}
return $result;
}
public function appendFilename($file, $suffix) {
$path_parts = pathinfo($file);
return $path_parts['dirname'] . '/' . $path_parts['filename'] . $suffix . '.' . $path_parts['extension'];
}
}