FFmpegPlugin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(ImageFile $imagefile, string $outpath, array $box): bool
  30. {
  31. switch ($imagefile->mimetype) {
  32. case 'image/gif':
  33. // resize only if an animated GIF
  34. if ($imagefile->animated) {
  35. return !$this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
  36. }
  37. break;
  38. }
  39. return true;
  40. }
  41. /**
  42. * High quality GIF conversion.
  43. *
  44. * @see http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
  45. * @see https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/592
  46. */
  47. public function resizeImageFileAnimatedGif(ImageFile $imagefile, string $outpath, array $box): bool
  48. {
  49. // Create FFMpeg instance
  50. // Need to explictly tell the drivers location or it won't find them
  51. $ffmpeg = FFMpeg\FFMpeg::create([
  52. 'ffmpeg.binaries' => exec("which ffmpeg"),
  53. 'ffprobe.binaries' => exec("which ffprobe")
  54. ]);
  55. // FFmpeg can't edit existing files in place,
  56. // generate temporary output file to avoid that
  57. $tmp_outpath = tempnam(sys_get_temp_dir(), 'outpath-');
  58. // Generate palette file. FFmpeg explictly needs to be told the
  59. // extension for PNG files outputs
  60. $palette = $this->tempnam_sfx(sys_get_temp_dir(), '.png');
  61. // Build filters
  62. $filters = 'fps=30';
  63. $filters .= ",crop={$box['w']}:{$box['h']}:{$box['x']}:{$box['y']}";
  64. $filters .= ",scale={$box['width']}:{$box['height']}:flags=lanczos";
  65. // Assemble commands for palette generation
  66. $commands[] = $commands_2[] = '-f';
  67. $commands[] = $commands_2[] = 'gif';
  68. $commands[] = $commands_2[] = '-i';
  69. $commands[] = $commands_2[] = $imagefile->filepath;
  70. $commands[] = '-vf';
  71. $commands[] = $filters . ',palettegen';
  72. $commands[] = '-y';
  73. $commands[] = $palette;
  74. // Assemble commands for GIF generation
  75. $commands_2[] = '-i';
  76. $commands_2[] = $palette;
  77. $commands_2[] = '-lavfi';
  78. $commands_2[] = $filters . ' [x]; [x][1:v] paletteuse';
  79. $commands_2[] = '-f';
  80. $commands_2[] = 'gif';
  81. $commands_2[] = '-y';
  82. $commands_2[] = $tmp_outpath;
  83. $success = true;
  84. // Generate the palette image
  85. try {
  86. $ffmpeg->getFFMpegDriver()->command($commands);
  87. } catch (Exception $e) {
  88. $this->log(LOG_ERR, 'Unable to generate the palette image');
  89. $success = false;
  90. }
  91. // Generate GIF
  92. try {
  93. if ($success) {
  94. $ffmpeg->getFFMpegDriver()->command($commands_2);
  95. }
  96. } catch (Exception $e) {
  97. $this->log(LOG_ERR, 'Unable to generate the GIF image');
  98. $success = false;
  99. }
  100. if ($success) {
  101. $success = @rename($tmp_outpath, $outpath);
  102. }
  103. @unlink($tmp_outpath);
  104. @unlink($palette);
  105. return $success;
  106. }
  107. /**
  108. * Suffix version of tempnam.
  109. * Courtesy of tomas at slax dot org:
  110. * @see https://www.php.net/manual/en/function.tempnam.php#98232
  111. */
  112. private function tempnam_sfx(string $dir, string $suffix): string
  113. {
  114. do {
  115. $file = $dir . "/" . mt_rand() . $suffix;
  116. $fp = @fopen($file, 'x');
  117. } while (!$fp);
  118. fclose($fp);
  119. return $file;
  120. }
  121. public function onPluginVersion(array &$versions): bool
  122. {
  123. $versions[] = ['name' => 'FFmpeg',
  124. 'version' => self::PLUGIN_VERSION,
  125. 'author' => 'Bruno Casteleiro',
  126. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/FFmpeg',
  127. 'rawdescription' =>
  128. // TRANS: Plugin description.
  129. _m('Use PHP-FFMpeg for resizing animated GIFs')];
  130. return true;
  131. }
  132. }