From 2ed5a4f4f4b1d7731c4113c469b0a727cd21cc84 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 21 Aug 2022 20:47:05 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Add=20Map=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Map.php | 28 +++++++++++++++++ src/MappableMedia.php | 73 +++++++++++++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 src/Map.php diff --git a/src/Map.php b/src/Map.php new file mode 100644 index 0000000..787a2f6 --- /dev/null +++ b/src/Map.php @@ -0,0 +1,28 @@ +media = $media; + } + + public function saveMap(): MappableMedia + { + $this->media->saveMap($this); + + return $this->media; + } + + public function saveAs(string $path): static + { + $this->path = $path; + + return $this; + } +} diff --git a/src/MappableMedia.php b/src/MappableMedia.php index 8835174..5863a2a 100644 --- a/src/MappableMedia.php +++ b/src/MappableMedia.php @@ -5,6 +5,7 @@ namespace Danjones\FFMpeg; use Alchemy\BinaryDriver\Exception\ExecutionFailureException; use FFMpeg\Driver\FFMpegDriver; use FFMpeg\Exception\RuntimeException; +use FFMpeg\FFMpeg; use FFMpeg\FFProbe; use FFMpeg\Filters\AdvancedMedia\ComplexCompatibleFilter; use FFMpeg\Filters\AdvancedMedia\ComplexFilterContainer; @@ -32,6 +33,9 @@ class MappableMedia extends AbstractMediaType */ protected array $inputs = []; + /** @var Map[] */ + protected array $maps = []; + /** * @var string[] */ @@ -42,21 +46,11 @@ class MappableMedia extends AbstractMediaType */ protected array $additionalParameters = []; - /** - * @var string[] - */ - protected array $mapCommands = []; - /** * @var AbstractProgressListener[] */ protected array $listeners = []; - /** - * AdvancedMedia constructor. - * - * @param string[] $inputs array of files to be opened - */ public function __construct(FFMpegDriver $driver, FFProbe $ffprobe) { // In case of error user will see this text in the error log. @@ -67,11 +61,27 @@ class MappableMedia extends AbstractMediaType parent::__construct($pathfile, $driver, $ffprobe); } + public static function make(FFMpeg $ffmpeg): static + { + return new static($ffmpeg->getFFMpegDriver(), $ffmpeg->getFFProbe()); + } + public function filters() { return $this->filters; } + public function addInput(string $path): static + { + if (empty($this->inputs)) { + $this->pathfile = $path; + } + + $this->inputs[] = $path; + + return $this; + } + /** * @return string[] */ @@ -83,7 +93,7 @@ class MappableMedia extends AbstractMediaType /** * @param string[] $initialParameters */ - public function setInitialParameters(array $initialParameters): self + public function setInitialParameters(array $initialParameters): static { $this->initialParameters = $initialParameters; @@ -101,7 +111,7 @@ class MappableMedia extends AbstractMediaType /** * @param string[] $additionalParameters */ - public function setAdditionalParameters(array $additionalParameters): self + public function setAdditionalParameters(array $additionalParameters): static { $this->additionalParameters = $additionalParameters; @@ -126,6 +136,26 @@ class MappableMedia extends AbstractMediaType return implode(' ', $this->buildCommand()); } + public function map(callable $callback = null): Map|static + { + $map = new Map($this); + if (!$callback) { + return $map; + } + + $callback($map); + $this->addMap($map); + + return $this; + } + + public function saveMap($map): static + { + $this->maps[] = $map; + + return $this; + } + /** * Select the streams for output. * @@ -139,7 +169,7 @@ class MappableMedia extends AbstractMediaType * @todo Redo all of this. * @see https://ffmpeg.org/ffmpeg.html#Manual-stream-selection */ - public function map( + private function map2( array $outs, FormatInterface $format, $outputFilename, @@ -246,7 +276,7 @@ class MappableMedia extends AbstractMediaType $this->buildConfiguredGlobalOptions($globalOptions), $this->getInitialParameters(), $this->buildInputsPart($this->inputs), - $this->mapCommands, + $this->buildMaps($this->maps), $this->getAdditionalParameters() ); } @@ -275,10 +305,8 @@ class MappableMedia extends AbstractMediaType * Build inputs part of the ffmpeg command. * * @param string[] $inputs - * - * @return array */ - protected function buildInputsPart(array $inputs) + protected function buildInputsPart(array $inputs): array { $commands = []; foreach ($inputs as $input) { @@ -288,4 +316,15 @@ class MappableMedia extends AbstractMediaType return $commands; } + + /** + * @param Map[] $maps + */ + protected function buildMaps(array $maps): array + { + $out = []; + // @todo + + return $out; + } }