2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-09-23 21:18:12 +00:00
|
|
|
|
2020-06-02 23:03:28 +00:00
|
|
|
abstract class VideoFileHandlerConfig
|
|
|
|
{
|
|
|
|
public const PLAYBACK_AUTOPLAY = "video_playback_autoplay";
|
|
|
|
public const PLAYBACK_LOOP = "video_playback_loop";
|
|
|
|
public const ENABLED_FORMATS = "video_enabled_formats";
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class VideoFileHandler extends DataHandlerExtension
|
|
|
|
{
|
2020-06-02 23:03:28 +00:00
|
|
|
public const SUPPORTED_MIME = [
|
|
|
|
MIME_TYPE_ASF,
|
|
|
|
MIME_TYPE_AVI,
|
|
|
|
MIME_TYPE_FLASH_VIDEO,
|
|
|
|
MIME_TYPE_MKV,
|
2020-05-28 15:05:20 +00:00
|
|
|
MIME_TYPE_MP4_VIDEO,
|
|
|
|
MIME_TYPE_OGG_VIDEO,
|
2020-06-02 23:03:28 +00:00
|
|
|
MIME_TYPE_QUICKTIME,
|
|
|
|
MIME_TYPE_WEBM,
|
2019-06-18 18:45:59 +00:00
|
|
|
];
|
2020-06-02 23:03:28 +00:00
|
|
|
protected $SUPPORTED_MIME = self::SUPPORTED_MIME;
|
2019-06-18 18:45:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2020-06-02 23:03:28 +00:00
|
|
|
$config->set_default_bool(VideoFileHandlerConfig::PLAYBACK_AUTOPLAY, true);
|
|
|
|
$config->set_default_bool(VideoFileHandlerConfig::PLAYBACK_LOOP, true);
|
|
|
|
$config->set_default_array(
|
|
|
|
VideoFileHandlerConfig::ENABLED_FORMATS,
|
|
|
|
[MIME_TYPE_FLASH_VIDEO, MIME_TYPE_MP4_VIDEO, MIME_TYPE_OGG_VIDEO, MIME_TYPE_WEBM]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_options(): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
foreach ($this->SUPPORTED_MIME as $format) {
|
|
|
|
$output[MIME_TYPE_MAP[$format][MIME_TYPE_MAP_NAME]] = $format;
|
|
|
|
}
|
|
|
|
return $output;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$sb = new SetupBlock("Video Options");
|
2020-06-02 23:03:28 +00:00
|
|
|
$sb->add_bool_option(VideoFileHandlerConfig::PLAYBACK_AUTOPLAY, "Autoplay: ");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("<br>");
|
2020-06-02 23:03:28 +00:00
|
|
|
$sb->add_bool_option(VideoFileHandlerConfig::PLAYBACK_LOOP, "Loop: ");
|
|
|
|
$sb->add_label("<br>Enabled Formats:");
|
|
|
|
$sb->add_multichoice_option(VideoFileHandlerConfig::ENABLED_FORMATS, $this->get_options());
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:37:22 +00:00
|
|
|
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
|
2019-06-25 20:17:13 +00:00
|
|
|
{
|
2020-02-23 18:37:22 +00:00
|
|
|
$event->image->video = true;
|
|
|
|
$event->image->image = false;
|
|
|
|
try {
|
|
|
|
$data = Media::get_ffprobe_data($event->file_name);
|
2019-06-25 20:17:13 +00:00
|
|
|
|
2020-02-23 18:37:22 +00:00
|
|
|
if (is_array($data)) {
|
|
|
|
if (array_key_exists("streams", $data)) {
|
|
|
|
$video = false;
|
|
|
|
$audio = true;
|
|
|
|
$streams = $data["streams"];
|
|
|
|
if (is_array($streams)) {
|
|
|
|
foreach ($streams as $stream) {
|
|
|
|
if (is_array($stream)) {
|
|
|
|
if (array_key_exists("codec_type", $stream)) {
|
|
|
|
$type = $stream["codec_type"];
|
|
|
|
switch ($type) {
|
|
|
|
case "audio":
|
|
|
|
$audio = true;
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$video = true;
|
|
|
|
break;
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-23 18:37:22 +00:00
|
|
|
if (array_key_exists("width", $stream) && !empty($stream["width"])
|
|
|
|
&& is_numeric($stream["width"]) && intval($stream["width"]) > ($event->image->width) ?? 0) {
|
|
|
|
$event->image->width = intval($stream["width"]);
|
|
|
|
}
|
|
|
|
if (array_key_exists("height", $stream) && !empty($stream["height"])
|
|
|
|
&& is_numeric($stream["height"]) && intval($stream["height"]) > ($event->image->height) ?? 0) {
|
|
|
|
$event->image->height = intval($stream["height"]);
|
|
|
|
}
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-23 18:37:22 +00:00
|
|
|
$event->image->video = $video;
|
|
|
|
$event->image->audio = $audio;
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
2020-02-23 18:37:22 +00:00
|
|
|
}
|
|
|
|
if (array_key_exists("format", $data)&& is_array($data["format"])) {
|
|
|
|
$format = $data["format"];
|
|
|
|
if (array_key_exists("duration", $format) && is_numeric($format["duration"])) {
|
|
|
|
$event->image->length = floor(floatval($format["duration"]) * 1000);
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 18:37:22 +00:00
|
|
|
} catch (MediaException $e) {
|
|
|
|
// a post with no metadata is better than no post
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 23:03:28 +00:00
|
|
|
protected function supported_ext(string $ext): bool
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$enabled_formats = $config->get_array(VideoFileHandlerConfig::ENABLED_FORMATS);
|
|
|
|
foreach ($enabled_formats as $format) {
|
|
|
|
if (in_array($ext, MIME_TYPE_MAP[$format][MIME_TYPE_MAP_EXT])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 check_contents(string $tmpname): bool
|
|
|
|
{
|
2020-06-02 23:03:28 +00:00
|
|
|
global $config;
|
|
|
|
|
|
|
|
if (file_exists($tmpname)) {
|
|
|
|
$mime = get_mime($tmpname);
|
|
|
|
|
|
|
|
$enabled_formats = $config->get_array(VideoFileHandlerConfig::ENABLED_FORMATS);
|
|
|
|
foreach ($enabled_formats as $format) {
|
|
|
|
if (in_array($mime, MIME_TYPE_MAP[$format][MIME_TYPE_MAP_MIME])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-09-23 21:18:12 +00:00
|
|
|
}
|