<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Plugin\FFmpegOutput;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ffmpeg_media\Attribute\FFmpegOutput;
use Drupal\ffmpeg_media\ValueObject\ConversionOptions;

/**
 * WebM output format plugin.
 */
#[FFmpegOutput(
  id: 'webm',
  label: new TranslatableMarkup('WebM'),
  description: new TranslatableMarkup('Open web media format with VP9 or AV1 codec.'),
  extension: 'webm',
  mimeType: 'video/webm',
  supportedCodecs: ['vp9', 'av1'],
  defaultCodec: 'vp9',
  supportedAudioCodecs: ['libopus', 'libvorbis'],
  weight: 10,
)]
final class WebmOutput extends FFmpegOutputBase {

  /**
   * {@inheritdoc}
   */
  #[\Override]
  #[\NoDiscard]
  public function buildArguments(ConversionOptions $options): array {
    return [
      '-f', 'webm',
    ];
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  #[\NoDiscard]
  public function generateOutputPath(string $inputPath): string {
    $baseName = pathinfo($inputPath, PATHINFO_FILENAME) ?: 'video';
    $directory = pathinfo($inputPath, PATHINFO_DIRNAME) ?: '';

    return $directory . DIRECTORY_SEPARATOR . $baseName . '_converted.webm';
  }

}
