diff --git a/plugins/AudioEncoder/AudioEncoder.php b/plugins/AudioEncoder/AudioEncoder.php new file mode 100644 index 0000000000..9ecca1cfda --- /dev/null +++ b/plugins/AudioEncoder/AudioEncoder.php @@ -0,0 +1,122 @@ +. +// }}} + +/** + * Audio template and metadata support via PHP-FFMpeg + * + * @package GNUsocial + * + * @author Diogo Peralta Cordeiro + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + * + * @see http://www.gnu.org/software/social/ + */ + +namespace Plugin\AudioEncoder; + +use App\Core\Event; +use App\Core\GSFile; +use App\Core\Modules\Plugin; +use App\Util\Exception\ServerException; +use App\Util\Formatting; +use FFMpeg\FFProbe as ffprobe; +use SplFileInfo; +use function App\Core\I18n\_m; + +class AudioEncoder extends Plugin +{ + public function version(): string + { + return '0.1.0'; + } + + public static function shouldHandle(string $mimetype): bool + { + return GSFile::mimetypeMajor($mimetype) === 'audio'; + } + + public function onFileMetaAvailable(array &$event_map, string $mimetype): bool + { + if (!self::shouldHandle($mimetype)) { + return Event::next; + } + $event_map['audio'][] = [$this, 'fileMeta']; + return Event::next; + } + + /** + * Adds duration metadata to audios + * + * @param null|string $mimetype in/out + * @param null|int $width out audio duration + * + * @return bool true if metadata filled + */ + public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool + { + // Create FFProbe instance + // Need to explicitly tell the drivers' location, or it won't find them + $ffprobe = ffprobe::create([ + 'ffmpeg.binaries' => exec('which ffmpeg'), + 'ffprobe.binaries' => exec('which ffprobe'), + ]); + + $metadata = $ffprobe->streams($file->getRealPath()) // extracts streams informations + ->audios() // filters audios streams + ->first(); // returns the first audio stream + $width = (int)ceil((float)$metadata->get('duration')); + + return true; + } + + /** + * Generates the view for attachments of type Video + */ + public function onViewAttachment(array $vars, array &$res): bool + { + if (!self::shouldHandle($vars['attachment']->getMimetype())) { + return Event::next; + } + + $res[] = Formatting::twigRenderFile( + 'audioEncoder/audioEncoderView.html.twig', + [ + 'attachment' => $vars['attachment'], + 'note' => $vars['note'], + ], + ); + return Event::stop; + } + + /** + * @throws ServerException + */ + public function onPluginVersion(array &$versions): bool + { + $versions[] = [ + 'name' => 'AudioEncoder', + 'version' => self::version(), + 'author' => 'Diogo Peralta Cordeiro', + 'rawdescription' => _m('Use PHP-FFMpeg for some more audio support.'), + ]; + return Event::next; + } +} diff --git a/plugins/AudioEncoder/README.md b/plugins/AudioEncoder/README.md new file mode 100644 index 0000000000..c3ad89eb78 --- /dev/null +++ b/plugins/AudioEncoder/README.md @@ -0,0 +1,9 @@ +# Audio Encoder plugin for GNU social + +(c) 2021 Free Software Foundation, Inc + +This is the README file for GNU social's AudioEncoder plugin. It includes general information about the plugin. + +## About + +This plugin at the moment only provides the audio player template and mimetype information for audios using ffprobe. \ No newline at end of file diff --git a/plugins/AudioEncoder/composer.json b/plugins/AudioEncoder/composer.json new file mode 100644 index 0000000000..9203b8ee9d --- /dev/null +++ b/plugins/AudioEncoder/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "php-ffmpeg/php-ffmpeg": "^0.18.0" + } +} \ No newline at end of file diff --git a/plugins/AudioEncoder/templates/audioEncoder/audioEncoderView.html.twig b/plugins/AudioEncoder/templates/audioEncoder/audioEncoderView.html.twig new file mode 100644 index 0000000000..fd6651ce65 --- /dev/null +++ b/plugins/AudioEncoder/templates/audioEncoder/audioEncoderView.html.twig @@ -0,0 +1,9 @@ +
+
+ +
{{ attachment.getBestTitle(note) }} +
+
+