VideoEncoder.php 5.3 KB

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