🚧 Add stream to map

Still need to handle the codecs. But almost there.
This commit is contained in:
Dan Jones 2022-08-23 15:25:27 -05:00
commit de5cf914da
4 changed files with 113 additions and 8 deletions

View file

@ -4,11 +4,12 @@ namespace Danjones\FFMpeg;
class Map class Map
{ {
use Traits\HasMetadata;
protected MappableMedia $media; protected MappableMedia $media;
protected string $path; protected string $path;
/** @var Stream[] */ /** @var Stream[] */
protected array $streams = []; protected array $streams = [];
protected array $metadata = [];
public function __construct(MappableMedia $media) public function __construct(MappableMedia $media)
{ {
@ -29,15 +30,22 @@ class Map
return $this; return $this;
} }
public function addMetadata(array|string $keyOrData, string $value = null): static public function stream(callable $callback = null): Stream|static
{ {
if (is_array($keyOrData)) { $stream = new Stream($this);
array_walk($keyOrData, fn($v, $k) => $this->addMetadata($k, $v)); if (!$callback) {
return $stream;
return $this;
} }
$this->metadata[$keyOrData] = $value; $callback($stream);
$this->saveStream($stream);
return $this;
}
public function saveStream(Stream $stream): static
{
$this->streams[] = $stream;
return $this; return $this;
} }

View file

@ -149,7 +149,7 @@ class MappableMedia extends AbstractMediaType
return $this; return $this;
} }
public function saveMap($map): static public function saveMap(Map $map): static
{ {
$this->maps[] = $map; $this->maps[] = $map;

76
src/Stream.php Normal file
View file

@ -0,0 +1,76 @@
<?php
namespace Danjones\FFMpeg;
use FFMpeg\Format\FormatInterface;
class Stream
{
use Traits\HasMetadata;
protected Map $map;
protected FormatInterface $codec;
protected string $input = '0:0';
protected array $flags = [];
public function __construct(Map $map)
{
$this->map = $map;
}
public function setInput(string $input): static
{
$this->input = $input;
return $this;
}
public function addFlag(string $key, string $value): static
{
$this->flags[$key] = $value;
return $this;
}
public function setCodec(FormatInterface $codec): static
{
$this->codec = $codec;
return $this;
}
protected function parseCodec(): static
{
// @todo
$this->addFlag('c', 'libx265');
return $this;
}
public function saveStream(): Map
{
$this->map->saveStream($this);
return $this->map;
}
public function buildCommand(int $idx = 0): array
{
$this->parseCodec();
$commands = [];
$commands[] = '-map';
$commands[] = $this->input;
foreach ($this->flags as $k => $v) {
array_push($commands, "-{$k}:{$idx}", $v);
}
foreach ($this->metadata as $k => $v) {
array_push($commands, "-metadata:s:{$idx}", "{$k}={$v}");
}
return $commands;
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Danjones\FFMpeg\Traits;
trait HasMetadata
{
protected array $metadata = [];
public function addMetadata(array|string $keyOrData, string $value = null): static
{
if (is_array($keyOrData)) {
array_walk($keyOrData, fn($v, $k) => $this->addMetadata($k, $v));
return $this;
}
$this->metadata[$keyOrData] = $value;
return $this;
}
}