diff --git a/src/Stream.php b/src/Stream.php index e8957e2..412d823 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -2,7 +2,11 @@ namespace Danjones\FFMpeg; +use FFMpeg\Format\AudioInterface; use FFMpeg\Format\FormatInterface; +use FFMpeg\Format\ProgressListener\AbstractProgressListener; +use FFMpeg\Format\ProgressableInterface; +use FFMpeg\Format\VideoInterface; class Stream { @@ -39,10 +43,77 @@ class Stream return $this; } + // @todo add hlper methods for setting codec + + public function getCodec(): FormatInterface + { + return $this->codec; + } + + public function editCodec(callable $callback): static + { + $callback($this->codec); + + return $this; + } + protected function parseCodec(): static { - // @todo - $this->addFlag('c', 'libx265'); + if ($this->codec instanceof VideoInterface) { + return $this->parseVideo(); + } + + if ($this->codec instanceof AudioInterface) { + return $this->parseAudio(); + } + + // @todo add subtitles + + $this->addFlag('c', 'copy'); + + return $this; + } + + protected function parseAudio(): static + { + if (null !== ($codec = $format->getAudioCodec())) { + $this->addFlag('c', $codec); + } + + if (null !== ($kb = $format->getAudioKiloBitrate())) { + $this->addFlag('b', "{$kb}k"); + } + + if (null !== ($channels = $format->getAudioChannels())) { + $this->addFlag('ac', $channels); + } + + return $this; + } + + protected function parseVideo(): static + { + if (null !== ($codec = $format->getVideoCodec())) { + $this->addFlag('c', $codec); + } + + if (0 !== ($kb = $format->getKiloBitrate())) { + $this->addFlag('b', "{$kb}k"); + } + + // If the user passed some additional format parameters. + if (null !== ($params = $format->getAdditionalParameters())) { + reset($params); + while(null !== key($params)) { + $key = current($params); + $value = next($params); + if (null == key($params)) { + continue; + } + + $this->addFlag(ltrim($key, '-k'), $value); + } + } return $this; }