2020-08-13 18:46:54 +01:00
|
|
|
<?php
|
2021-10-10 09:26:18 +01:00
|
|
|
|
|
|
|
declare(strict_types = 1);
|
2021-04-25 22:24:43 +01:00
|
|
|
// {{{ License
|
2020-08-13 18:46:54 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2021-04-25 22:24:43 +01:00
|
|
|
// }}}
|
2020-08-13 18:46:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Animated GIF resize support via PHP-FFMpeg
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2021-04-14 16:44:45 +01:00
|
|
|
*
|
2020-08-13 18:46:54 +01:00
|
|
|
* @author Bruno Casteleiro <up201505347@fc.up.pt>
|
2021-08-12 00:42:38 +01:00
|
|
|
* @author Diogo Peralta Cordeiro <mail@diogo.site>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-08-13 18:46:54 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2021-04-14 16:44:45 +01:00
|
|
|
*
|
|
|
|
* @see http://www.gnu.org/software/social/
|
2020-08-13 18:46:54 +01:00
|
|
|
*/
|
|
|
|
|
2021-04-18 05:47:16 +01:00
|
|
|
namespace Plugin\VideoEncoder;
|
2021-04-14 16:44:45 +01:00
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
use App\Core\Event;
|
2021-08-12 00:42:38 +01:00
|
|
|
use App\Core\GSFile;
|
|
|
|
use function App\Core\I18n\_m;
|
|
|
|
use App\Core\Log;
|
2021-04-18 05:47:16 +01:00
|
|
|
use App\Core\Modules\Plugin;
|
2021-08-12 00:42:38 +01:00
|
|
|
use App\Util\Exception\ServerException;
|
|
|
|
use App\Util\Exception\TemporaryFileException;
|
2021-07-22 20:49:12 +01:00
|
|
|
use App\Util\Formatting;
|
2021-08-12 00:42:38 +01:00
|
|
|
use App\Util\TemporaryFile;
|
|
|
|
use Exception;
|
|
|
|
use FFMpeg\FFMpeg as ffmpeg;
|
|
|
|
use FFMpeg\FFProbe as ffprobe;
|
|
|
|
use SplFileInfo;
|
2020-08-13 18:46:54 +01:00
|
|
|
|
2021-04-18 05:47:16 +01:00
|
|
|
class VideoEncoder extends Plugin
|
2020-08-13 18:46:54 +01:00
|
|
|
{
|
2021-08-12 00:42:38 +01:00
|
|
|
public function version(): string
|
|
|
|
{
|
|
|
|
return '1.0.0';
|
|
|
|
}
|
2020-08-13 18:46:54 +01:00
|
|
|
|
2021-09-22 15:04:45 +01:00
|
|
|
public function onFileMetaAvailable(array &$event_map, string $mimetype): bool
|
|
|
|
{
|
|
|
|
if (GSFile::mimetypeMajor($mimetype) !== 'video' && $mimetype !== 'image/gif') {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
$event_map['video'][] = [$this, 'fileMeta'];
|
|
|
|
$event_map['image/gif'][] = [$this, 'fileMeta'];
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
public function onFileSanitizerAvailable(array &$event_map, string $mimetype): bool
|
|
|
|
{
|
|
|
|
if (GSFile::mimetypeMajor($mimetype) !== 'video' && $mimetype !== 'image/gif') {
|
|
|
|
return Event::next;
|
2020-08-13 18:46:54 +01:00
|
|
|
}
|
2021-09-22 15:04:45 +01:00
|
|
|
$event_map['video'][] = [$this, 'fileMeta'];
|
|
|
|
$event_map['image/gif'][] = [$this, 'fileMeta'];
|
2021-08-12 00:42:38 +01:00
|
|
|
return Event::next;
|
2020-08-13 18:46:54 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
public function onFileResizerAvailable(array &$event_map, string $mimetype): bool
|
2021-07-22 21:17:23 +01:00
|
|
|
{
|
2021-08-12 00:42:38 +01:00
|
|
|
if (GSFile::mimetypeMajor($mimetype) !== 'video' && $mimetype !== 'image/gif') {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
$event_map['video'][] = [$this, 'resizeVideoPath'];
|
|
|
|
$event_map['image/gif'][] = [$this, 'resizeVideoPath'];
|
2021-07-22 21:17:23 +01:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
/**
|
|
|
|
* Adds width and height metadata to gifs
|
|
|
|
*
|
|
|
|
* @param null|string $mimetype in/out
|
|
|
|
* @param null|int $width out
|
|
|
|
* @param null|int $height out
|
|
|
|
*
|
|
|
|
* @return bool true if sanitized
|
|
|
|
*/
|
2021-09-22 15:04:45 +01:00
|
|
|
public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
2021-08-12 00:42:38 +01:00
|
|
|
{
|
|
|
|
if (//GSFile::mimetypeMajor($mimetype) !== 'video' &&
|
|
|
|
$mimetype !== 'image/gif') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create FFProbe instance
|
|
|
|
// Need to explicitly tell the drivers' location, or it won't find them
|
|
|
|
$ffprobe = ffprobe::create([
|
|
|
|
'ffmpeg.binaries' => exec('which ffmpeg'),
|
|
|
|
'ffprobe.binaries' => exec('which ffprobe'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$metadata = $ffprobe->streams($file->getRealPath()) // extracts streams informations
|
2021-10-10 09:26:18 +01:00
|
|
|
->videos() // filters video streams
|
|
|
|
->first(); // returns the first video stream
|
2021-08-12 00:42:38 +01:00
|
|
|
$width = $metadata->get('width');
|
|
|
|
$height = $metadata->get('height');
|
|
|
|
|
|
|
|
// Only one plugin can handle sanitization
|
|
|
|
$mimetype = 'image/gif';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes GIF files.
|
|
|
|
*
|
|
|
|
* @throws TemporaryFileException
|
|
|
|
*/
|
|
|
|
public function resizeVideoPath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
|
|
|
{
|
|
|
|
switch ($mimetype) {
|
|
|
|
case 'image/gif':
|
|
|
|
// resize only if an animated GIF
|
|
|
|
if ($this->isAnimatedGif($source)) {
|
|
|
|
return $this->resizeImageFileAnimatedGif($source, $destination, $width, $height, $smart_crop, $mimetype);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
/**
|
|
|
|
* Generates the view for attachments of type Video
|
|
|
|
*/
|
2021-09-25 13:12:32 +01:00
|
|
|
public function onViewAttachment(array $vars, array &$res): bool
|
2021-07-22 20:49:12 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$res[] = Formatting::twigRenderFile(
|
|
|
|
'videoEncoder/videoEncoderView.html.twig',
|
2021-09-25 13:12:32 +01:00
|
|
|
[
|
|
|
|
'attachment' => $vars['attachment'],
|
|
|
|
'note' => $vars['note'],
|
2021-10-10 09:26:18 +01:00
|
|
|
],
|
|
|
|
);
|
2021-07-22 20:49:12 +01:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
/**
|
|
|
|
* Animated GIF test, courtesy of frank at huddler dot com et al:
|
|
|
|
* http://php.net/manual/en/function.imagecreatefromgif.php#104473
|
|
|
|
* Modified so avoid landing inside of a header (and thus not matching our regexp).
|
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public function isAnimatedGif(string $filepath): bool
|
2021-08-12 00:42:38 +01:00
|
|
|
{
|
|
|
|
if (!($fh = @fopen($filepath, 'rb'))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
//an animated gif contains multiple "frames", with each frame having a
|
|
|
|
//header made up of:
|
|
|
|
// * a static 4-byte sequence (\x00\x21\xF9\x04)
|
|
|
|
// * 4 variable bytes
|
|
|
|
// * a static 2-byte sequence (\x00\x2C)
|
|
|
|
// In total the header is maximum 10 bytes.
|
|
|
|
|
|
|
|
// We read through the file til we reach the end of the file, or we've found
|
|
|
|
// at least 2 frame headers
|
|
|
|
while (!feof($fh) && $count < 2) {
|
|
|
|
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
|
|
|
|
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
|
|
|
|
// rewind in case we ended up in the middle of the header, but avoid
|
|
|
|
// infinite loop (i.e. don't rewind if we're already in the end).
|
|
|
|
if (!feof($fh) && ftell($fh) >= 9) {
|
2021-10-10 09:26:18 +01:00
|
|
|
fseek($fh, -9, \SEEK_CUR);
|
2021-08-12 00:42:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fh);
|
|
|
|
return $count >= 1; // number of animated frames apart from the original image
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:46:54 +01:00
|
|
|
/**
|
|
|
|
* High quality GIF conversion.
|
|
|
|
*
|
|
|
|
* @see http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
|
|
|
|
* @see https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/592
|
2021-08-12 00:42:38 +01:00
|
|
|
*
|
|
|
|
* @throws TemporaryFileException
|
2020-08-13 18:46:54 +01:00
|
|
|
*/
|
2021-08-12 00:42:38 +01:00
|
|
|
public function resizeImageFileAnimatedGif(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
2020-08-13 18:46:54 +01:00
|
|
|
{
|
|
|
|
// Create FFMpeg instance
|
2021-08-12 00:42:38 +01:00
|
|
|
// Need to explicitly tell the drivers' location, or it won't find them
|
|
|
|
$ffmpeg = ffmpeg::create([
|
2021-04-14 16:44:45 +01:00
|
|
|
'ffmpeg.binaries' => exec('which ffmpeg'),
|
|
|
|
'ffprobe.binaries' => exec('which ffprobe'),
|
2020-08-13 18:46:54 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
// FFmpeg can't edit existing files in place,
|
|
|
|
// generate temporary output file to avoid that
|
2021-10-10 09:26:18 +01:00
|
|
|
$destination ??= new TemporaryFile(['prefix' => 'video']);
|
2020-08-13 18:46:54 +01:00
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
// Generate palette file. FFmpeg explicitly needs to be told the
|
2020-08-13 18:46:54 +01:00
|
|
|
// extension for PNG files outputs
|
|
|
|
$palette = $this->tempnam_sfx(sys_get_temp_dir(), '.png');
|
|
|
|
|
|
|
|
// Build filters
|
|
|
|
$filters = 'fps=30';
|
2021-08-12 00:42:38 +01:00
|
|
|
// if ($crop) {
|
|
|
|
// $filters .= ",crop={$width}:{$height}:{$x}:{$y}";
|
|
|
|
// }
|
|
|
|
$filters .= ",scale={$width}:{$height}:flags=lanczos";
|
2020-08-13 18:46:54 +01:00
|
|
|
|
|
|
|
// Assemble commands for palette generation
|
|
|
|
$commands[] = $commands_2[] = '-f';
|
|
|
|
$commands[] = $commands_2[] = 'gif';
|
|
|
|
$commands[] = $commands_2[] = '-i';
|
2021-08-12 00:42:38 +01:00
|
|
|
$commands[] = $commands_2[] = $source;
|
2020-08-13 18:46:54 +01:00
|
|
|
$commands[] = '-vf';
|
|
|
|
$commands[] = $filters . ',palettegen';
|
|
|
|
$commands[] = '-y';
|
|
|
|
$commands[] = $palette;
|
|
|
|
|
|
|
|
// Assemble commands for GIF generation
|
|
|
|
$commands_2[] = '-i';
|
|
|
|
$commands_2[] = $palette;
|
|
|
|
$commands_2[] = '-lavfi';
|
|
|
|
$commands_2[] = $filters . ' [x]; [x][1:v] paletteuse';
|
|
|
|
$commands_2[] = '-f';
|
|
|
|
$commands_2[] = 'gif';
|
|
|
|
$commands_2[] = '-y';
|
2021-08-12 00:42:38 +01:00
|
|
|
$commands_2[] = $destination->getRealPath();
|
2020-08-13 18:46:54 +01:00
|
|
|
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
// Generate the palette image
|
|
|
|
try {
|
|
|
|
$ffmpeg->getFFMpegDriver()->command($commands);
|
|
|
|
} catch (Exception $e) {
|
2021-08-12 00:42:38 +01:00
|
|
|
Log::error('Unable to generate the palette image');
|
2020-08-13 18:46:54 +01:00
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate GIF
|
|
|
|
try {
|
|
|
|
if ($success) {
|
|
|
|
$ffmpeg->getFFMpegDriver()->command($commands_2);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2021-08-12 00:42:38 +01:00
|
|
|
Log::error('Unable to generate the GIF image');
|
2020-08-13 18:46:54 +01:00
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@unlink($palette);
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
$mimetype = 'image/gif';
|
2020-08-13 18:46:54 +01:00
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suffix version of tempnam.
|
|
|
|
* Courtesy of tomas at slax dot org:
|
2021-04-14 16:44:45 +01:00
|
|
|
*
|
2020-08-13 18:46:54 +01:00
|
|
|
* @see https://www.php.net/manual/en/function.tempnam.php#98232
|
|
|
|
*/
|
|
|
|
private function tempnam_sfx(string $dir, string $suffix): string
|
|
|
|
{
|
|
|
|
do {
|
2021-04-14 16:44:45 +01:00
|
|
|
$file = $dir . '/' . mt_rand() . $suffix;
|
|
|
|
$fp = @fopen($file, 'x');
|
2020-08-13 18:46:54 +01:00
|
|
|
} while (!$fp);
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:42:38 +01:00
|
|
|
/**
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
2020-08-13 18:46:54 +01:00
|
|
|
public function onPluginVersion(array &$versions): bool
|
|
|
|
{
|
|
|
|
$versions[] = ['name' => 'FFmpeg',
|
2021-08-12 00:42:38 +01:00
|
|
|
'version' => self::version(),
|
|
|
|
'author' => 'Bruno Casteleiro, Diogo Peralta Cordeiro',
|
2021-04-14 16:44:45 +01:00
|
|
|
'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/FFmpeg',
|
2021-10-10 09:26:18 +01:00
|
|
|
'rawdescription', // TRANS: Plugin description. => _m('Use PHP-FFMpeg for some more video support.'),
|
2021-08-12 00:42:38 +01:00
|
|
|
];
|
|
|
|
return Event::next;
|
2020-08-13 18:46:54 +01:00
|
|
|
}
|
|
|
|
}
|