AudioEncoder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 EventResult;
  38. use FFMpeg\FFProbe as ffprobe;
  39. use SplFileInfo;
  40. class AudioEncoder extends Plugin
  41. {
  42. public static function version(): string
  43. {
  44. return '0.1.0';
  45. }
  46. public static function shouldHandle(string $mimetype): bool
  47. {
  48. return GSFile::mimetypeMajor($mimetype) === 'audio';
  49. }
  50. /**
  51. * @param array<string, callable> $event_map
  52. */
  53. public function onFileMetaAvailable(array &$event_map, string $mimetype): EventResult
  54. {
  55. if (!self::shouldHandle($mimetype)) {
  56. return Event::next;
  57. }
  58. $event_map['audio'][] = [$this, 'fileMeta'];
  59. return Event::next;
  60. }
  61. /**
  62. * Adds duration metadata to audios
  63. *
  64. * @param null|string $mimetype in/out
  65. * @param null|int $width out audio duration
  66. *
  67. * @return bool true if metadata filled
  68. */
  69. public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
  70. {
  71. // Create FFProbe instance
  72. // Need to explicitly tell the drivers' location, or it won't find them
  73. $ffprobe = ffprobe::create([
  74. 'ffmpeg.binaries' => exec('which ffmpeg'),
  75. 'ffprobe.binaries' => exec('which ffprobe'),
  76. ]);
  77. $metadata = $ffprobe->streams($file->getRealPath()) // extracts streams informations
  78. ->audios() // filters audios streams
  79. ->first(); // returns the first audio stream
  80. $width = (int) ceil((float) $metadata->get('duration'));
  81. return true;
  82. }
  83. /**
  84. * Generates the view for attachments of type Video
  85. *
  86. * @param (array{attachment: \Component\Attachment\Entity\Attachment, note: \App\Entity\Note, title: string} & array<string, mixed>) $vars
  87. * @param array<string> $res
  88. */
  89. public function onViewAttachment(array $vars, array &$res): EventResult
  90. {
  91. if (!self::shouldHandle($vars['attachment']->getMimetype())) {
  92. return Event::next;
  93. }
  94. $res[] = Formatting::twigRenderFile(
  95. 'audioEncoder/audioEncoderView.html.twig',
  96. [
  97. 'attachment' => $vars['attachment'],
  98. 'note' => $vars['note'],
  99. 'title' => $vars['title'],
  100. ],
  101. );
  102. return Event::stop;
  103. }
  104. /**
  105. * @param ModuleVersionType[] $versions
  106. *
  107. * @throws ServerException
  108. */
  109. public function onPluginVersion(array &$versions): EventResult
  110. {
  111. $versions[] = [
  112. 'name' => 'AudioEncoder',
  113. 'version' => self::version(),
  114. 'author' => 'Diogo Peralta Cordeiro',
  115. 'rawdescription' => _m('Use PHP-FFMpeg for some more audio support.'),
  116. ];
  117. return Event::next;
  118. }
  119. }