Skip to content

Commit

Permalink
Update DownloadOptions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Oct 31, 2023
1 parent ffc8a88 commit f8a6872
Showing 1 changed file with 10 additions and 39 deletions.
49 changes: 10 additions & 39 deletions src/DownloadOptions.php
Expand Up @@ -11,10 +11,10 @@
class DownloadOptions
{
/** @var StreamFormat[] $formats */
private $formats;
private array $formats = [];

/** @var VideoDetails|null */
private $info;
private ?VideoDetails $info;

public function __construct($formats, $info = null)
{
Expand All @@ -25,29 +25,29 @@ public function __construct($formats, $info = null)
/**
* @return StreamFormat[]
*/
public function getAllFormats()
public function getAllFormats(): array
{
return $this->formats;
}

/**
* @return VideoDetails|null
*/
public function getInfo()
public function getInfo(): ?VideoDetails
{
return $this->info;
}

// Will not include Videos with Audio
public function getVideoFormats()
public function getVideoFormats(): array
{
return Utils::arrayFilterReset($this->getAllFormats(), function ($format) {
/** @var $format StreamFormat */
return strpos($format->mimeType, 'video') === 0 && empty($format->audioQuality);
});
}

public function getAudioFormats()
public function getAudioFormats(): array
{
return Utils::arrayFilterReset($this->getAllFormats(), function ($format) {
/** @var $format StreamFormat */
Expand All @@ -58,7 +58,7 @@ public function getAudioFormats()
/**
* @return StreamFormat[]
*/
public function getCombinedFormats()
public function getCombinedFormats(): array
{
return Utils::arrayFilterReset($this->getAllFormats(), function ($format) {
/** @var $format StreamFormat */
Expand All @@ -69,13 +69,13 @@ public function getCombinedFormats()
/**
* @return StreamFormat|null
*/
public function getFirstCombinedFormat()
public function getFirstCombinedFormat(): ?StreamFormat
{
$combined = $this->getCombinedFormats();
return count($combined) ? $combined[0] : null;
}

protected function getLowToHighVideoFormats()
protected function getLowToHighVideoFormats(): array
{
$copy = array_values($this->getVideoFormats());

Expand All @@ -90,7 +90,7 @@ protected function getLowToHighVideoFormats()
return $copy;
}

protected function getLowToHighAudioFormats()
protected function getLowToHighAudioFormats(): array
{
$copy = array_values($this->getAudioFormats());

Expand All @@ -105,33 +105,4 @@ protected function getLowToHighAudioFormats()

return $copy;
}

// Combined using: ffmpeg -i video.mp4 -i audio.mp3 output.mp4
public function getSplitFormats($quality = null)
{
// sort formats by quality in desc, and high = first, medium = middle, low = last
$videos = $this->getLowToHighVideoFormats();
$audio = $this->getLowToHighAudioFormats();

if ($quality == 'high' || $quality == 'best') {

return new SplitStream([
'video' => $videos[count($videos) - 1],
'audio' => $audio[count($audio) - 1]
]);

} else if ($quality == 'low' || $quality == 'worst') {

return new SplitStream([
'video' => $videos[0],
'audio' => $audio[0]
]);
}

// something in between!
return new SplitStream([
'video' => $videos[floor(count($videos) / 2)],
'audio' => $audio[floor(count($audio) / 2)]
]);
}
}

0 comments on commit f8a6872

Please sign in to comment.