FFmpegPlugin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Animated GIF resize support via PHP-FFMpeg
  18. *
  19. * @package GNUsocial
  20. * @author Bruno Casteleiro <up201505347@fc.up.pt>
  21. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class FFmpegPlugin extends Plugin
  27. {
  28. const PLUGIN_VERSION = '0.1.0';
  29. public function onStartResizeImageFile(
  30. ImageFile $imagefile,
  31. string $outpath,
  32. array $box
  33. ): bool {
  34. switch ($imagefile->mimetype) {
  35. case 'image/gif':
  36. // resize only if an animated GIF
  37. if ($imagefile->animated) {
  38. return !$this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
  39. }
  40. break;
  41. }
  42. return true;
  43. }
  44. /**
  45. * High quality GIF conversion.
  46. *
  47. * @see http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
  48. * @see https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/592
  49. */
  50. public function resizeImageFileAnimatedGif(ImageFile $imagefile, string $outpath, array $box): bool
  51. {
  52. // Create FFMpeg instance
  53. // Need to explictly tell the drivers location or it won't find them
  54. $ffmpeg = FFMpeg\FFMpeg::create([
  55. 'ffmpeg.binaries' => exec("which ffmpeg"),
  56. 'ffprobe.binaries' => exec("which ffprobe")
  57. ]);
  58. // FFmpeg can't edit existing files in place,
  59. // generate temporary output file to avoid that
  60. $tempfile = new TemporaryFile('gs-outpath');
  61. // Generate palette file. FFmpeg explictly needs to be told the
  62. // extension for PNG files outputs
  63. $palette = $this->tempnam_sfx(sys_get_temp_dir(), '.png');
  64. // Build filters
  65. $filters = 'fps=30';
  66. $filters .= ",crop={$box['w']}:{$box['h']}:{$box['x']}:{$box['y']}";
  67. $filters .= ",scale={$box['width']}:{$box['height']}:flags=lanczos";
  68. // Assemble commands for palette generation
  69. $commands[] = $commands_2[] = '-f';
  70. $commands[] = $commands_2[] = 'gif';
  71. $commands[] = $commands_2[] = '-i';
  72. $commands[] = $commands_2[] = $imagefile->filepath;
  73. $commands[] = '-vf';
  74. $commands[] = $filters . ',palettegen';
  75. $commands[] = '-y';
  76. $commands[] = $palette;
  77. // Assemble commands for GIF generation
  78. $commands_2[] = '-i';
  79. $commands_2[] = $palette;
  80. $commands_2[] = '-lavfi';
  81. $commands_2[] = $filters . ' [x]; [x][1:v] paletteuse';
  82. $commands_2[] = '-f';
  83. $commands_2[] = 'gif';
  84. $commands_2[] = '-y';
  85. $commands_2[] = $tempfile->getRealPath();
  86. $success = true;
  87. // Generate the palette image
  88. try {
  89. $ffmpeg->getFFMpegDriver()->command($commands);
  90. } catch (Exception $e) {
  91. $this->log(LOG_ERR, 'Unable to generate the palette image');
  92. $success = false;
  93. }
  94. // Generate GIF
  95. try {
  96. if ($success) {
  97. $ffmpeg->getFFMpegDriver()->command($commands_2);
  98. }
  99. } catch (Exception $e) {
  100. $this->log(LOG_ERR, 'Unable to generate the GIF image');
  101. $success = false;
  102. }
  103. if ($success) {
  104. $success = $tempfile->commit($outpath);
  105. }
  106. @unlink($palette);
  107. return $success;
  108. }
  109. /**
  110. * Suffix version of tempnam.
  111. * Courtesy of tomas at slax dot org:
  112. * @see https://www.php.net/manual/en/function.tempnam.php#98232
  113. */
  114. private function tempnam_sfx(string $dir, string $suffix): string
  115. {
  116. do {
  117. $file = $dir . "/" . mt_rand() . $suffix;
  118. $fp = @fopen($file, 'x');
  119. } while (!$fp);
  120. fclose($fp);
  121. return $file;
  122. }
  123. public function onPluginVersion(array &$versions): bool
  124. {
  125. $versions[] = ['name' => 'FFmpeg',
  126. 'version' => self::PLUGIN_VERSION,
  127. 'author' => 'Bruno Casteleiro',
  128. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/FFmpeg',
  129. 'rawdescription' =>
  130. // TRANS: Plugin description.
  131. _m('Use PHP-FFMpeg for resizing animated GIFs')];
  132. return true;
  133. }
  134. }