123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- defined('GNUSOCIAL') || die();
- class FFmpegPlugin extends Plugin
- {
- const PLUGIN_VERSION = '0.1.0';
- public function onStartResizeImageFile(
- ImageFile $imagefile,
- string $outpath,
- array $box
- ): bool {
- switch ($imagefile->mimetype) {
- case 'image/gif':
-
- if ($imagefile->animated) {
- return !$this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
- }
- break;
- }
- return true;
- }
-
- public function resizeImageFileAnimatedGif(ImageFile $imagefile, string $outpath, array $box): bool
- {
-
-
- $ffmpeg = FFMpeg\FFMpeg::create([
- 'ffmpeg.binaries' => exec("which ffmpeg"),
- 'ffprobe.binaries' => exec("which ffprobe")
- ]);
-
-
- $tempfile = new TemporaryFile('gs-outpath');
-
-
- $palette = $this->tempnam_sfx(sys_get_temp_dir(), '.png');
-
- $filters = 'fps=30';
- $filters .= ",crop={$box['w']}:{$box['h']}:{$box['x']}:{$box['y']}";
- $filters .= ",scale={$box['width']}:{$box['height']}:flags=lanczos";
-
- $commands[] = $commands_2[] = '-f';
- $commands[] = $commands_2[] = 'gif';
- $commands[] = $commands_2[] = '-i';
- $commands[] = $commands_2[] = $imagefile->filepath;
- $commands[] = '-vf';
- $commands[] = $filters . ',palettegen';
- $commands[] = '-y';
- $commands[] = $palette;
-
- $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';
- $commands_2[] = $tempfile->getRealPath();
- $success = true;
-
- try {
- $ffmpeg->getFFMpegDriver()->command($commands);
- } catch (Exception $e) {
- $this->log(LOG_ERR, 'Unable to generate the palette image');
- $success = false;
- }
-
- try {
- if ($success) {
- $ffmpeg->getFFMpegDriver()->command($commands_2);
- }
- } catch (Exception $e) {
- $this->log(LOG_ERR, 'Unable to generate the GIF image');
- $success = false;
- }
- if ($success) {
- try {
- $tempfile->commit($outpath);
- } catch (TemporaryFileException $e) {
- $this->log(LOG_ERR, 'Unable to save the GIF image');
- $success = false;
- }
- }
- @unlink($palette);
- return $success;
- }
-
- private function tempnam_sfx(string $dir, string $suffix): string
- {
- do {
- $file = $dir . "/" . mt_rand() . $suffix;
- $fp = @fopen($file, 'x');
- } while (!$fp);
- fclose($fp);
- return $file;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = ['name' => 'FFmpeg',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Bruno Casteleiro',
- 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/FFmpeg',
- 'rawdescription' =>
-
- _m('Use PHP-FFMpeg for resizing animated GIFs')];
- return true;
- }
- }
|