2012-09-23 21:18:12 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class VideoFileHandler extends DataHandlerExtension
|
|
|
|
{
|
2019-06-18 18:45:59 +00:00
|
|
|
const SUPPORTED_MIME = [
|
|
|
|
'video/webm',
|
|
|
|
'video/mp4',
|
|
|
|
'video/ogg',
|
|
|
|
'video/flv',
|
|
|
|
'video/x-flv'
|
|
|
|
];
|
|
|
|
const SUPPORTED_EXT = ["flv", "mp4", "m4v", "ogv", "webm"];
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$config->set_default_bool('video_playback_autoplay', true);
|
|
|
|
$config->set_default_bool('video_playback_loop', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$sb = new SetupBlock("Video Options");
|
|
|
|
$sb->add_bool_option("video_playback_autoplay", "Autoplay: ");
|
|
|
|
$sb->add_label("<br>");
|
|
|
|
$sb->add_bool_option("video_playback_loop", "Loop: ");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:17:13 +00:00
|
|
|
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event)
|
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
if (in_array($event->ext, self::SUPPORTED_EXT)) {
|
2019-06-25 20:17:13 +00:00
|
|
|
$event->video = true;
|
2019-08-16 14:40:42 +00:00
|
|
|
$event->image = false;
|
2019-06-25 20:17:13 +00:00
|
|
|
try {
|
|
|
|
$data = Media::get_ffprobe_data($event->file_name);
|
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
if (is_array($data)) {
|
|
|
|
if (array_key_exists("streams", $data)) {
|
2019-06-25 20:17:13 +00:00
|
|
|
$video = false;
|
|
|
|
$audio = true;
|
|
|
|
$streams = $data["streams"];
|
|
|
|
if (is_array($streams)) {
|
|
|
|
foreach ($streams as $stream) {
|
2019-09-29 13:30:55 +00:00
|
|
|
if (is_array($stream)) {
|
2019-06-25 20:17:13 +00:00
|
|
|
if (array_key_exists("codec_type", $stream)) {
|
|
|
|
$type = $stream["codec_type"];
|
|
|
|
switch ($type) {
|
|
|
|
case "audio":
|
|
|
|
$audio = true;
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$video = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array_key_exists("width", $stream) && !empty($stream["width"])
|
|
|
|
&& is_numeric($stream["width"]) && intval($stream["width"]) > ($event->width) ?? 0) {
|
|
|
|
$event->width = intval($stream["width"]);
|
|
|
|
}
|
|
|
|
if (array_key_exists("height", $stream) && !empty($stream["height"])
|
|
|
|
&& is_numeric($stream["height"]) && intval($stream["height"]) > ($event->height) ?? 0) {
|
|
|
|
$event->height = intval($stream["height"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$event->video = $video;
|
|
|
|
$event->audio = $audio;
|
|
|
|
}
|
|
|
|
}
|
2019-09-29 13:30:55 +00:00
|
|
|
if (array_key_exists("format", $data)&& is_array($data["format"])) {
|
2019-06-25 20:17:13 +00:00
|
|
|
$format = $data["format"];
|
2019-09-29 13:30:55 +00:00
|
|
|
if (array_key_exists("duration", $format) && is_numeric($format["duration"])) {
|
|
|
|
$event->length = floor(floatval($format["duration"]) * 1000);
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-29 13:30:55 +00:00
|
|
|
} catch (MediaException $e) {
|
2019-10-02 09:10:47 +00:00
|
|
|
// a post with no metadata is better than no post
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Generate the Thumbnail image for particular file.
|
|
|
|
*/
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function create_thumb(string $hash, string $type): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-24 15:05:16 +00:00
|
|
|
return Media::create_thumbnail_ffmpeg($hash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function supported_ext(string $ext): bool
|
|
|
|
{
|
2019-06-18 18:45:59 +00:00
|
|
|
return in_array(strtolower($ext), self::SUPPORTED_EXT);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function create_image_from_data(string $filename, array $metadata): Image
|
|
|
|
{
|
|
|
|
$image = new Image();
|
|
|
|
|
|
|
|
switch (getMimeType($filename)) {
|
|
|
|
case "video/webm":
|
|
|
|
$image->ext = "webm";
|
|
|
|
break;
|
|
|
|
case "video/mp4":
|
|
|
|
$image->ext = "mp4";
|
|
|
|
break;
|
|
|
|
case "video/ogg":
|
|
|
|
$image->ext = "ogv";
|
|
|
|
break;
|
|
|
|
case "video/flv":
|
|
|
|
case "video/x-flv":
|
|
|
|
$image->ext = "flv";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image->filesize = $metadata['size'];
|
|
|
|
$image->hash = $metadata['hash'];
|
|
|
|
$image->filename = $metadata['filename'];
|
|
|
|
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
|
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function check_contents(string $tmpname): bool
|
|
|
|
{
|
2019-06-14 12:52:27 +00:00
|
|
|
return (
|
|
|
|
file_exists($tmpname) &&
|
2019-06-18 18:45:59 +00:00
|
|
|
in_array(getMimeType($tmpname), self::SUPPORTED_MIME)
|
2019-06-14 12:52:27 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-09-23 21:18:12 +00:00
|
|
|
}
|