VideoThumbnail.php 5.3 KB

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