Set stream codecs properly

This commit is contained in:
Dan Jones 2022-08-29 10:02:04 -05:00
commit e869ff2a59
4 changed files with 149 additions and 11 deletions

View file

@ -0,0 +1,27 @@
<?php
namespace Danjones\FFMpeg\Format;
use FFMpeg\Format\Audio\DefaultAudio as BaseAudio;
class DefaultAudio extends BaseAudio
{
public function __construct($codec)
{
$this->setAudioCodec($codec);
$this->audioKiloBitrate = null;
$this->audioChannels = null;
}
public function setAudioCodec($audioCodec)
{
$this->audioCodec = $audioCodec;
return $this;
}
public function getAvailableAudioCodecs()
{
return [];
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Danjones\FFMpeg\Format;
class DefaultSubtitle implements SubtitleInterface
{
public function __construct($codec)
{
$this->setSubtitleCodec($codec);
}
public function setSubtitleCodec($subtitleCodec)
{
$this->subtitleCodec = $subtitleCodec;
return $this;
}
public function getSubtitleCodec()
{
return $this->subtitleCodec;
}
public function getPasses()
{
return 1;
}
public function getExtraParams()
{
return [];
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Danjones\FFMpeg\Format;
use FFMpeg\Format\Video\DefaultVideo as BaseVideo;
class DefaultVideo extends BaseVideo
{
protected bool $supportsBFrames = false;
public function __construct($codec)
{
$this->setVideoCodec($codec);
$this->kiloBitrate = 0;
$this->modulus = 0;
}
public function setVideoCodec($videoCodec)
{
$this->videoCodec = $videoCodec;
return $this;
}
public function getAvailableVideoCodecs()
{
return [];
}
public function getAvailableAudioCodecs()
{
return [];
}
public function supportBFrames()
{
return $this->supportsBFrames;
}
public function setSupportBFrames(bool $supports = true): static
{
$this->supportsBFrames = $supports;
return $this;
}
}

View file

@ -43,7 +43,45 @@ class Stream
return $this; return $this;
} }
// @todo add hlper methods for setting codec public function video(string $codec, callable $callback = null): static
{
$this->setCodec(new Format\DefaultVideo($codec));
if ($callback) {
return $this->editCodec($callback);
}
return $this;
}
public function audio(string $codec, callable $callback = null): static
{
$this->setCodec(new Format\DefaultAudio($codec));
if ($callback) {
return $this->editCodec($callback);
}
return $this;
}
public function subtitle(string $codec, callable $callback = null): static
{
$this->setCodec(new Format\DefaultSubtitle($codec));
if ($callback) {
return $this->editCodec($callback);
}
return $this;
}
public function copy(): static
{
return $this->setCodec(new Format\Copy());
}
public function getCodec(): FormatInterface public function getCodec(): FormatInterface
{ {
@ -61,21 +99,15 @@ class Stream
{ {
if ($this->codec instanceof VideoInterface) { if ($this->codec instanceof VideoInterface) {
$this->parseVideo(); $this->parseVideo();
} } elseif ($this->codec instanceof AudioInterface) {
if ($this->codec instanceof AudioInterface) {
$this->parseAudio(); $this->parseAudio();
} } elseif ($this->codec instanceof Format\SubtitleInterface) {
if ($this->codec instanceof Format\SubtitleInterface) {
$this->parseSubtitle(); $this->parseSubtitle();
} } elseif ($this->codec instanceof Format\Copy) {
if ($this->codec instanceof Format\Copy) {
$this->addFlag('c', 'copy'); $this->addFlag('c', 'copy');
} }
$this->parseExtras($this->codec->getExtraParameters()); $this->parseExtras($this->codec->getExtraParams());
return $this; return $this;
} }