<?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;

/**
 * MP4 output format plugin.
 */
#[FFmpegOutput(
  id: 'mp4',
  label: new TranslatableMarkup('MP4'),
  description: new TranslatableMarkup('Most compatible container format for web video.'),
  extension: 'mp4',
  mimeType: 'video/mp4',
  supportedCodecs: ['h264', 'hevc', 'av1'],
  defaultCodec: 'h264',
  supportedAudioCodecs: ['aac', 'libopus'],
  weight: 0,
)]
final class Mp4Output extends FFmpegOutputBase {

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

  /**
   * {@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.mp4';
  }

}
