alltube/classes/Config.php

325 lines
7.7 KiB
PHP
Raw Normal View History

2015-10-31 14:42:25 +00:00
<?php
2015-10-31 14:42:25 +00:00
/**
2016-09-07 22:28:28 +00:00
* Config class.
2016-08-01 11:29:13 +00:00
*/
2016-12-05 12:12:27 +00:00
2015-10-31 14:42:25 +00:00
namespace Alltube;
2016-03-29 23:49:08 +00:00
use Alltube\Exception\ConfigException;
use Alltube\Library\Downloader;
2020-05-13 19:18:32 +00:00
use Jawira\CaseConverter\CaseConverterException;
use Jean85\PrettyVersions;
use Symfony\Component\ErrorHandler\Debug;
2015-10-31 14:42:25 +00:00
use Symfony\Component\Yaml\Yaml;
use Jawira\CaseConverter\Convert;
2016-03-29 23:49:08 +00:00
2015-10-31 14:42:25 +00:00
/**
2016-09-07 22:28:28 +00:00
* Manage config parameters.
2016-08-01 11:29:13 +00:00
*/
2016-03-29 23:49:08 +00:00
class Config
2015-10-31 14:42:25 +00:00
{
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* youtube-dl binary path.
*
2016-08-01 11:29:13 +00:00
* @var string
*/
public string $youtubedl = 'vendor/yt-dlp/yt-dlp/yt_dlp/__main__.py';
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* python binary path.
*
2016-08-01 11:29:13 +00:00
* @var string
*/
public string $python = '/usr/bin/python';
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* youtube-dl parameters.
*
2020-05-13 20:28:05 +00:00
* @var string[]
2016-08-01 11:29:13 +00:00
*/
public array $params = [
'--no-warnings',
'--ignore-errors',
'--flat-playlist',
'--restrict-filenames',
'--no-playlist',
'--use-extractors',
'default,-generic',
];
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* Enable audio conversion.
*
2016-08-01 11:29:13 +00:00
* @var bool
*/
public bool $convert = false;
2016-08-01 11:29:13 +00:00
/**
* Enable advanced conversion mode.
*
* @var bool
*/
public bool $convertAdvanced = false;
/**
* List of formats available in advanced conversion mode.
*
2020-05-13 20:28:05 +00:00
* @var string[]
*/
public array $convertAdvancedFormats = ['mp3', 'avi', 'flv', 'wav'];
2016-08-01 11:29:13 +00:00
/**
* ffmpeg binary path.
2016-09-07 22:28:28 +00:00
*
2016-08-01 11:29:13 +00:00
* @var string
*/
public string $ffmpeg = '/usr/bin/ffmpeg';
2016-08-01 11:29:13 +00:00
/**
* Path to the directory that contains the phantomjs binary.
*
* @var string
*/
public string $phantomjsDir = '/usr/bin/';
/**
* Disable URL rewriting.
2017-01-10 22:39:58 +00:00
*
* @var bool
*/
public bool $uglyUrls = false;
/**
* Stream downloaded files trough server?
2017-01-16 11:11:37 +00:00
*
* @var bool
*/
public bool $stream = false;
2017-04-24 22:40:24 +00:00
/**
* Allow to remux video + audio?
*
* @var bool
*/
public bool $remux = false;
2017-04-24 22:40:24 +00:00
/**
2017-11-10 22:52:04 +00:00
* MP3 bitrate when converting (in kbit/s).
*
* @var int
*/
public int $audioBitrate = 128;
/**
* ffmpeg logging level.
2017-12-09 22:57:21 +00:00
* Must be one of these: quiet, panic, fatal, error, warning, info, verbose, debug.
*
* @var string
*/
public string $ffmpegVerbosity = 'error';
/**
2019-01-06 16:00:12 +00:00
* App name.
*
* @var string
*/
public string $appName = 'AllTube Download';
/**
* Generic formats supported by youtube-dl.
*
2020-05-13 20:28:05 +00:00
* @var string[]
*/
public array $genericFormats = [
'best/bestvideo' => 'Best',
'bestvideo+bestaudio' => 'Remux best video with best audio',
'worst/worstvideo' => 'Worst',
];
/**
* Enable debug mode.
*
* @var bool
*/
public bool $debug = false;
2019-11-27 20:22:23 +00:00
/**
* Default to audio.
*
* @var bool
*/
public bool $defaultAudio = false;
/**
* Disable audio conversion from/to seeker.
*
* @var bool
*/
public bool $convertSeek = true;
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* Config constructor.
2016-09-05 22:36:47 +00:00
*
2022-02-03 19:21:25 +00:00
* @param scalar[]|scalar[][]|null[] $options Options
* @throws ConfigException
2015-10-31 14:50:32 +00:00
*/
public function __construct(array $options = [])
{
$this->applyOptions($options);
$this->getEnv();
$this->validateOptions();
foreach ($this->genericFormats as $format => $name) {
if (strpos($format, '+') !== false) {
if (!$this->remux) {
// Disable combined formats if remux mode is not enabled.
unset($this->genericFormats[$format]);
}
} elseif (!$this->stream) {
// Force HTTP if stream is not enabled.
$keys = array_keys($this->genericFormats);
$keys[array_search($format, $keys)] = $this->addHttpToFormat($format);
if ($genericFormats = array_combine($keys, $this->genericFormats)) {
$this->genericFormats = $genericFormats;
}
}
}
}
/**
* Add HTTP condition to a format.
*
* @param string $format Format
*
* @return string
*/
2020-12-17 21:43:05 +00:00
public static function addHttpToFormat(string $format): string
{
$newFormat = [];
foreach (explode('/', $format) as $subformat) {
$newFormat[] = $subformat . '[protocol=https]';
$newFormat[] = $subformat . '[protocol=http]';
}
return implode('/', $newFormat);
}
/**
* Throw an exception if some of the options are invalid.
*
2020-05-13 19:18:32 +00:00
* @return void
* @throws ConfigException If Python is missing
* @throws ConfigException If youtube-dl is missing
*/
2022-05-28 21:43:07 +00:00
private function validateOptions(): void
{
if (!is_file($this->youtubedl)) {
throw new ConfigException("Can't find youtube-dl at " . $this->youtubedl);
} elseif (!Downloader::checkCommand([$this->python, '--version'])) {
throw new ConfigException("Can't find Python at " . $this->python);
}
if (!class_exists(Debug::class)) {
// Dev dependencies are probably not installed.
$this->debug = false;
}
}
/**
* Apply the provided options.
*
2022-02-03 19:21:25 +00:00
* @param scalar[]|scalar[][]|null[] $options Options
*
* @return void
*/
2022-05-28 21:43:07 +00:00
private function applyOptions(array $options): void
2015-10-31 14:50:32 +00:00
{
foreach ($options as $option => $value) {
if (isset($this->$option) && isset($value)) {
$this->$option = $value;
2015-10-31 14:42:25 +00:00
}
}
}
/**
2020-05-15 19:18:01 +00:00
* Override options from environment variables.
* Environment variables should use screaming snake case: CONVERT, PYTHON, AUDIO_BITRATE, etc.
* If the value is an array, you should use the YAML format: "CONVERT_ADVANCED_FORMATS='[foo, bar]'"
*
* @return void
* @throws ConfigException
*/
2022-05-28 21:43:07 +00:00
private function getEnv(): void
{
foreach (get_object_vars($this) as $prop => $value) {
try {
$convert = new Convert($prop);
$env = getenv($convert->toMacro());
} catch (CaseConverterException $e) {
// This should not happen.
throw new ConfigException('Could not parse option name: ' . $prop, $e->getCode(), $e);
}
if ($env) {
$this->$prop = Yaml::parse($env);
}
}
2015-10-31 14:42:25 +00:00
}
/**
* Set options from a YAML file.
*
* @param string $file Path to the YAML file
* @return Config
* @throws ConfigException
*/
2020-12-17 21:43:05 +00:00
public static function fromFile(string $file): Config
{
if (is_file($file)) {
return new self(Yaml::parse(strval(file_get_contents($file))));
} else {
throw new ConfigException("Can't find config file at " . $file);
}
}
/**
* Manually set some options.
*
2022-02-03 19:21:25 +00:00
* @param scalar[]|scalar[][]|null[] $options Options (see `config/config.example.yml` for available options)
2020-05-13 20:28:05 +00:00
* @return void
* @throws ConfigException
*/
2022-05-28 21:43:07 +00:00
public function setOptions(array $options): void
{
$this->applyOptions($options);
$this->validateOptions();
}
/**
* Return a downloader object with the current config.
*
* @return Downloader
*/
2020-12-17 21:43:05 +00:00
public function getDownloader(): Downloader
{
return new Downloader(
$this->youtubedl,
$this->params,
$this->python,
$this->ffmpeg,
$this->phantomjsDir,
$this->ffmpegVerbosity
);
}
/**
* @return string
*/
2020-12-17 21:43:05 +00:00
public function getAppVersion(): string
{
$version = PrettyVersions::getRootPackageVersion();
return $version->getPrettyVersion();
}
2015-10-31 14:42:25 +00:00
}