Add subtitles

This commit is contained in:
Dan Jones 2022-08-23 21:23:23 -05:00
commit d75d5c125b
2 changed files with 52 additions and 18 deletions

View file

@ -0,0 +1,12 @@
<?php
namespace Danjones\FFMpeg\Format;
use FFMpeg\Format\AudioInterface;
use FFMpeg\Format\FormatInterface;
use FFMpeg\Format\ProgressListener\AbstractProgressListener;
interface SubtitleInterface extends FormatInterface
{
public function getSubtitleCodec();
}

View file

@ -67,57 +67,79 @@ class Stream
return $this->parseAudio(); return $this->parseAudio();
} }
// @todo add subtitles if ($this->codec instanceof Format\SubtitleInterface) {
return $this->parseSubtitle();
}
$this->addFlag('c', 'copy'); $this->addFlag('c', 'copy');
$this->parseExtras($this->codec->getExtraParameters());
return $this;
}
protected function parseSubtitle(): static
{
if (null !== ($codec = $this->codec->getSubtitleCodec())) {
$this->addFlag('c', $codec);
}
$this->parseExtras($this->codec->getExtraParameters());
return $this; return $this;
} }
protected function parseAudio(): static protected function parseAudio(): static
{ {
if (null !== ($codec = $format->getAudioCodec())) { if (null !== ($codec = $this->codec->getAudioCodec())) {
$this->addFlag('c', $codec); $this->addFlag('c', $codec);
} }
if (null !== ($kb = $format->getAudioKiloBitrate())) { if (null !== ($kb = $this->codec->getAudioKiloBitrate())) {
$this->addFlag('b', "{$kb}k"); $this->addFlag('b', "{$kb}k");
} }
if (null !== ($channels = $format->getAudioChannels())) { if (null !== ($channels = $this->codec->getAudioChannels())) {
$this->addFlag('ac', $channels); $this->addFlag('ac', $channels);
} }
$this->parseExtras($this->codec->getExtraParameters());
return $this; return $this;
} }
protected function parseVideo(): static protected function parseVideo(): static
{ {
if (null !== ($codec = $format->getVideoCodec())) { if (null !== ($codec = $this->codec->getVideoCodec())) {
$this->addFlag('c', $codec); $this->addFlag('c', $codec);
} }
if (0 !== ($kb = $format->getKiloBitrate())) { if (0 !== ($kb = $this->codec->getKiloBitrate())) {
$this->addFlag('b', "{$kb}k"); $this->addFlag('b', "{$kb}k");
} }
// If the user passed some additional format parameters. // If the user passed some additional format parameters.
if (null !== ($params = $format->getAdditionalParameters())) { $this->parseExtras($this->codec->getAdditionalParameters());
reset($params); $this->parseExtras($this->codec->getExtraParameters());
while(null !== key($params)) {
$key = current($params);
$value = next($params);
if (null == key($params)) {
continue;
}
$this->addFlag(ltrim($key, '-k'), $value);
}
}
return $this; return $this;
} }
protected function parseExtras (?array $params): void
{
if (is_null($params)) {
return;
}
reset($params);
while(null !== key($params)) {
$key = current($params);
$value = next($params);
if (null == key($params)) {
continue;
}
$this->addFlag(ltrim($key, '-k'), $value);
}
}
public function saveStream(): Map public function saveStream(): Map
{ {
$this->map->saveStream($this); $this->map->saveStream($this);