This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/handle_video/main.php

147 lines
5.4 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
abstract class VideoFileHandlerConfig
{
public const PLAYBACK_AUTOPLAY = "video_playback_autoplay";
public const PLAYBACK_LOOP = "video_playback_loop";
2020-06-24 13:14:24 +00:00
public const PLAYBACK_MUTE = "video_playback_mute";
public const ENABLED_FORMATS = "video_enabled_formats";
}
class VideoFileHandler extends DataHandlerExtension
{
public const SUPPORTED_MIME = [
2020-06-14 16:05:55 +00:00
MimeType::ASF,
MimeType::AVI,
MimeType::FLASH_VIDEO,
MimeType::MKV,
MimeType::MP4_VIDEO,
MimeType::OGG_VIDEO,
MimeType::QUICKTIME,
MimeType::WEBM,
];
protected array $SUPPORTED_MIME = self::SUPPORTED_MIME;
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_bool(VideoFileHandlerConfig::PLAYBACK_AUTOPLAY, true);
$config->set_default_bool(VideoFileHandlerConfig::PLAYBACK_LOOP, true);
2020-06-24 13:14:24 +00:00
$config->set_default_bool(VideoFileHandlerConfig::PLAYBACK_MUTE, false);
$config->set_default_array(
VideoFileHandlerConfig::ENABLED_FORMATS,
2020-06-14 16:05:55 +00:00
[MimeType::FLASH_VIDEO, MimeType::MP4_VIDEO, MimeType::OGG_VIDEO, MimeType::WEBM]
);
}
/**
* @return array<string, string>
*/
private function get_options(): array
{
$output = [];
2020-06-14 16:05:55 +00:00
foreach ($this->SUPPORTED_MIME as $mime) {
$output[MimeMap::get_name_for_mime($mime)] = $mime;
}
return $output;
}
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Video Options");
2020-06-24 13:14:24 +00:00
$sb->start_table();
$sb->add_bool_option(VideoFileHandlerConfig::PLAYBACK_AUTOPLAY, "Autoplay", true);
$sb->add_bool_option(VideoFileHandlerConfig::PLAYBACK_LOOP, "Loop", true);
$sb->add_bool_option(VideoFileHandlerConfig::PLAYBACK_MUTE, "Mute", true);
$sb->add_multichoice_option(VideoFileHandlerConfig::ENABLED_FORMATS, $this->get_options(), "Enabled Formats", true);
$sb->end_table();
}
2020-02-23 18:37:22 +00:00
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
2020-02-23 18:37:22 +00:00
$event->image->video = true;
$event->image->image = false;
try {
$data = Media::get_ffprobe_data($event->image->get_image_filename());
2023-06-27 16:30:09 +00:00
if (array_key_exists("streams", $data)) {
$video = false;
$audio = false;
$video_codec = null;
$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;
$video_codec = $stream["codec_name"];
break;
2020-02-23 18:37:22 +00:00
}
}
2023-12-30 13:26:49 +00:00
$event->image->width = max($event->image->width, @$stream["width"]);
$event->image->height = max($event->image->height, @$stream["height"]);
}
}
2023-06-27 16:30:09 +00:00
$event->image->video = $video;
$event->image->video_codec = $video_codec;
$event->image->audio = $audio;
2023-11-11 21:49:12 +00:00
if ($event->image->get_mime() == MimeType::MKV &&
2023-06-27 16:30:09 +00:00
VideoContainers::is_video_codec_supported(VideoContainers::WEBM, $event->image->video_codec)) {
// WEBMs are MKVs with the VP9 or VP8 codec
// For browser-friendliness, we'll just change the mime type
$event->image->set_mime(MimeType::WEBM);
}
}
}
2023-11-11 21:49:12 +00:00
if (array_key_exists("format", $data) && is_array($data["format"])) {
2023-06-27 16:30:09 +00:00
$format = $data["format"];
if (array_key_exists("duration", $format) && is_numeric($format["duration"])) {
$event->image->length = (int)floor(floatval($format["duration"]) * 1000);
}
}
2020-02-23 18:37:22 +00:00
} catch (MediaException $e) {
// a post with no metadata is better than no post
}
}
2020-06-14 16:05:55 +00:00
protected function supported_mime(string $mime): bool
{
global $config;
$enabled_formats = $config->get_array(VideoFileHandlerConfig::ENABLED_FORMATS);
2020-06-14 16:05:55 +00:00
return MimeType::matches_array($mime, $enabled_formats, true);
}
protected function create_thumb(Image $image): bool
{
return Media::create_thumbnail_ffmpeg($image);
}
protected function check_contents(string $tmpname): bool
{
global $config;
if (file_exists($tmpname)) {
2020-06-14 16:05:55 +00:00
$mime = MimeType::get_for_file($tmpname);
$enabled_formats = $config->get_array(VideoFileHandlerConfig::ENABLED_FORMATS);
2020-06-14 16:05:55 +00:00
if (MimeType::matches_array($mime, $enabled_formats)) {
return true;
}
}
return false;
}
}