AudioEncoder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * Audio template and metadata support via PHP-FFMpeg
  21. *
  22. * @package GNUsocial
  23. *
  24. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  25. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. *
  28. * @see http://www.gnu.org/software/social/
  29. */
  30. namespace Plugin\AudioEncoder;
  31. use App\Core\Event;
  32. use App\Core\GSFile;
  33. use function App\Core\I18n\_m;
  34. use App\Core\Modules\Plugin;
  35. use App\Util\Exception\ServerException;
  36. use App\Util\Formatting;
  37. use FFMpeg\FFProbe as ffprobe;
  38. use SplFileInfo;
  39. class AudioEncoder extends Plugin
  40. {
  41. public function version(): string
  42. {
  43. return '0.1.0';
  44. }
  45. public static function shouldHandle(string $mimetype): bool
  46. {
  47. return GSFile::mimetypeMajor($mimetype) === 'audio';
  48. }
  49. public function onFileMetaAvailable(array &$event_map, string $mimetype): bool
  50. {
  51. if (!self::shouldHandle($mimetype)) {
  52. return Event::next;
  53. }
  54. $event_map['audio'][] = [$this, 'fileMeta'];
  55. return Event::next;
  56. }
  57. /**
  58. * Adds duration metadata to audios
  59. *
  60. * @param null|string $mimetype in/out
  61. * @param null|int $width out audio duration
  62. *
  63. * @return bool true if metadata filled
  64. */
  65. public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
  66. {
  67. // Create FFProbe instance
  68. // Need to explicitly tell the drivers' location, or it won't find them
  69. $ffprobe = ffprobe::create([
  70. 'ffmpeg.binaries' => exec('which ffmpeg'),
  71. 'ffprobe.binaries' => exec('which ffprobe'),
  72. ]);
  73. $metadata = $ffprobe->streams($file->getRealPath()) // extracts streams informations
  74. ->audios() // filters audios streams
  75. ->first(); // returns the first audio stream
  76. $width = (int) ceil((float) $metadata->get('duration'));
  77. return true;
  78. }
  79. /**
  80. * Generates the view for attachments of type Video
  81. */
  82. public function onViewAttachment(array $vars, array &$res): bool
  83. {
  84. if (!self::shouldHandle($vars['attachment']->getMimetype())) {
  85. return Event::next;
  86. }
  87. $res[] = Formatting::twigRenderFile(
  88. 'audioEncoder/audioEncoderView.html.twig',
  89. [
  90. 'attachment' => $vars['attachment'],
  91. 'note' => $vars['note'],
  92. 'title' => $vars['title'],
  93. ],
  94. );
  95. return Event::stop;
  96. }
  97. /**
  98. * @throws ServerException
  99. */
  100. public function onPluginVersion(array &$versions): bool
  101. {
  102. $versions[] = [
  103. 'name' => 'AudioEncoder',
  104. 'version' => self::version(),
  105. 'author' => 'Diogo Peralta Cordeiro',
  106. 'rawdescription' => _m('Use PHP-FFMpeg for some more audio support.'),
  107. ];
  108. return Event::next;
  109. }
  110. }