From a5e8106b7aedbc1160dc39e01fb12d6d250796e1 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Sat, 9 Dec 2023 09:52:46 +0100 Subject: [PATCH] Initial Video Thumbnail Support --- README.md | 2 +- lib/thumbs.class.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2f9962d..423a90f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ DISCLAIMER: This was just quickly hacked together, it might not be up to any qua Copy conf/config.sample.php to config.php and edit accordingly. -A webserver with PHP (tested with 7.4 and 8.0) support and the php-gd and php-exif extensions (for thumbnails) is required. An example config for nginx with php-fpm might look something like this: +A webserver with PHP (tested with 7.4 and 8.0) support and the php-gd and php-exif extensions (for image thumbnails) and installed ffmpeg (for video thumbnails) is required. An example config for nginx with php-fpm might look something like this: worker_processes 4; diff --git a/lib/thumbs.class.php b/lib/thumbs.class.php index c8b3bee..804514a 100644 --- a/lib/thumbs.class.php +++ b/lib/thumbs.class.php @@ -8,6 +8,9 @@ class thumb { protected int $w = 200; protected int $h = 200; + protected string $ffmpeg_cmd = '/usr/bin/ffmpeg' + protected string $ffprobe_cmd = '/usr/bin/ffprobe'; + const IMAGE_HANDLERS = [ IMAGETYPE_JPEG => [ 'load' => 'imagecreatefromjpeg', @@ -39,7 +42,18 @@ class thumb { if (!file_exists($dstpath)) { if(str_starts_with(mime_content_type($src), 'video/')) { - return "img/video.png"; + $cmd = $this->ffprobe_cmd.' -i "'.$src.'" -show_entries format=duration -v quiet -of csv="p=0"'; + exec($cmd, $output, $retval); + $seconds = ($output[0]*10) / 100; + + $cmd = $this->ffmpeg_cmd.' -i "'.$src.'" -an -ss '.$seconds.' -t 00:00:01 -vf scale=w='.$this->w.':h='.$this->h.':force_original_aspect_ratio=decrease -r 1 -y -vcodec mjpeg -f mjpeg '.$dstpath.' 2>&1'; + exec($cmd, $output, $retval); + + if ($retval) { + return "img/video.png"; + } else { + return $this->thumburl.$dstname.'.jpg'; + } } $type = exif_imagetype($src);