2015-10-31 15:42:25 +01:00
|
|
|
<?php
|
2019-10-03 21:24:12 +02:00
|
|
|
|
2015-10-31 15:42:25 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Config class.
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2016-12-05 13:12:27 +01:00
|
|
|
|
2015-10-31 15:42:25 +01:00
|
|
|
namespace Alltube;
|
2016-03-30 01:49:08 +02:00
|
|
|
|
2018-02-05 16:48:58 +01:00
|
|
|
use Exception;
|
2015-10-31 15:42:25 +01:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2019-11-10 18:05:53 +01:00
|
|
|
use Jawira\CaseConverter\Convert;
|
2016-03-30 01:49:08 +02:00
|
|
|
|
2015-10-31 15:42:25 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Manage config parameters.
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2016-03-30 01:49:08 +02:00
|
|
|
class Config
|
2015-10-31 15:42:25 +01:00
|
|
|
{
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Singleton instance.
|
|
|
|
*
|
2019-03-30 18:21:45 +01:00
|
|
|
* @var Config|null
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2016-03-30 01:49:08 +02:00
|
|
|
private static $instance;
|
2015-10-31 15:42:25 +01:00
|
|
|
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* youtube-dl binary path.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-10-31 15:56:00 +01:00
|
|
|
public $youtubedl = 'vendor/rg3/youtube-dl/youtube_dl/__main__.py';
|
2016-08-01 13:29:13 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* python binary path.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-10-31 15:42:25 +01:00
|
|
|
public $python = '/usr/bin/python';
|
2016-08-01 13:29:13 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* youtube-dl parameters.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2018-05-23 21:43:34 +02:00
|
|
|
public $params = ['--no-warnings', '--ignore-errors', '--flat-playlist', '--restrict-filenames', '--no-playlist'];
|
2016-08-01 13:29:13 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Enable audio conversion.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2015-10-31 15:42:25 +01:00
|
|
|
public $convert = false;
|
2016-08-01 13:29:13 +02:00
|
|
|
|
2018-01-24 23:30:24 +01:00
|
|
|
/**
|
|
|
|
* Enable advanced conversion mode.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $convertAdvanced = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of formats available in advanced conversion mode.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $convertAdvancedFormats = ['mp3', 'avi', 'flv', 'wav'];
|
|
|
|
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* avconv or ffmpeg binary path.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-21 20:54:38 +01:00
|
|
|
public $avconv = 'vendor/bin/ffmpeg';
|
2016-08-01 13:29:13 +02:00
|
|
|
|
2018-01-25 15:10:11 +01:00
|
|
|
/**
|
|
|
|
* Path to the directory that contains the phantomjs binary.
|
2018-01-25 15:13:13 +01:00
|
|
|
*
|
2018-01-25 15:10:11 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $phantomjsDir = 'vendor/bin/';
|
|
|
|
|
2017-01-10 23:37:29 +01:00
|
|
|
/**
|
|
|
|
* Disable URL rewriting.
|
2017-01-10 23:39:58 +01:00
|
|
|
*
|
|
|
|
* @var bool
|
2017-01-10 23:37:29 +01:00
|
|
|
*/
|
|
|
|
public $uglyUrls = false;
|
|
|
|
|
2017-01-16 11:29:56 +01:00
|
|
|
/**
|
|
|
|
* Stream downloaded files trough server?
|
2017-01-16 12:11:37 +01:00
|
|
|
*
|
|
|
|
* @var bool
|
2017-01-16 11:29:56 +01:00
|
|
|
*/
|
|
|
|
public $stream = false;
|
|
|
|
|
2017-04-25 00:40:24 +02:00
|
|
|
/**
|
|
|
|
* Allow to remux video + audio?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $remux = false;
|
|
|
|
|
2017-11-10 23:50:17 +01:00
|
|
|
/**
|
2017-11-10 23:52:04 +01:00
|
|
|
* MP3 bitrate when converting (in kbit/s).
|
2017-11-10 23:50:17 +01:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $audioBitrate = 128;
|
|
|
|
|
2017-12-09 23:16:48 +01:00
|
|
|
/**
|
|
|
|
* avconv/ffmpeg logging level.
|
2017-12-09 23:57:21 +01:00
|
|
|
* Must be one of these: quiet, panic, fatal, error, warning, info, verbose, debug.
|
2017-12-09 23:16:48 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $avconvVerbosity = 'error';
|
|
|
|
|
2019-01-06 16:59:16 +01:00
|
|
|
/**
|
2019-01-06 17:00:12 +01:00
|
|
|
* App name.
|
2019-01-06 16:59:16 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $appName = 'AllTube Download';
|
|
|
|
|
2016-09-06 00:36:47 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* YAML config file path.
|
|
|
|
*
|
2016-09-06 00:36:47 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $file;
|
2016-08-19 01:07:51 +02:00
|
|
|
|
2019-05-08 19:46:37 +02:00
|
|
|
/**
|
|
|
|
* Generic formats supported by youtube-dl.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $genericFormats = [];
|
|
|
|
|
2019-11-27 23:15:49 +01:00
|
|
|
/**
|
|
|
|
* Enable debug mode.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2019-11-27 21:22:23 +01:00
|
|
|
public $debug = false;
|
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Config constructor.
|
2016-09-06 00:36:47 +02:00
|
|
|
*
|
2019-04-21 18:30:02 +02:00
|
|
|
* @param array $options Options
|
2015-10-31 15:50:32 +01:00
|
|
|
*/
|
2019-04-21 18:30:02 +02:00
|
|
|
private function __construct(array $options = [])
|
|
|
|
{
|
|
|
|
$this->applyOptions($options);
|
|
|
|
$this->getEnv();
|
2019-11-29 22:56:41 +01:00
|
|
|
$localeManager = LocaleManager::getInstance();
|
2019-05-08 19:46:37 +02:00
|
|
|
|
|
|
|
if (empty($this->genericFormats)) {
|
|
|
|
// We don't put this in the class definition so it can be detected by xgettext.
|
|
|
|
$this->genericFormats = [
|
2019-11-29 22:56:41 +01:00
|
|
|
'best' => $localeManager->t('Best'),
|
|
|
|
'bestvideo+bestaudio' => $localeManager->t('Remux best video with best audio'),
|
|
|
|
'worst' => $localeManager->t('Worst'),
|
2019-05-08 19:46:37 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2019-10-03 21:24:12 +02:00
|
|
|
$this->replaceGenericFormat($format, $format . '[protocol=https]/' . $format . '[protocol=http]');
|
2019-05-08 19:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace a format key.
|
|
|
|
*
|
|
|
|
* @param string $oldFormat Old format
|
|
|
|
* @param string $newFormat New format
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function replaceGenericFormat($oldFormat, $newFormat)
|
|
|
|
{
|
|
|
|
$keys = array_keys($this->genericFormats);
|
|
|
|
$keys[array_search($oldFormat, $keys)] = $newFormat;
|
2019-06-17 23:19:18 +02:00
|
|
|
if ($genericFormats = array_combine($keys, $this->genericFormats)) {
|
|
|
|
$this->genericFormats = $genericFormats;
|
|
|
|
}
|
2019-04-21 18:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an exception if some of the options are invalid.
|
|
|
|
*
|
|
|
|
* @throws Exception If youtube-dl is missing
|
|
|
|
* @throws Exception If Python is missing
|
2019-04-21 18:35:24 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2019-04-21 18:30:02 +02:00
|
|
|
*/
|
|
|
|
private function validateOptions()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We don't translate these exceptions because they usually occur before Slim can catch them
|
|
|
|
so they will go to the logs.
|
|
|
|
*/
|
|
|
|
if (!is_file($this->youtubedl)) {
|
2019-10-03 21:24:12 +02:00
|
|
|
throw new Exception("Can't find youtube-dl at " . $this->youtubedl);
|
2019-04-21 18:30:02 +02:00
|
|
|
} elseif (!Video::checkCommand([$this->python, '--version'])) {
|
2019-10-03 21:24:12 +02:00
|
|
|
throw new Exception("Can't find Python at " . $this->python);
|
2019-04-21 18:30:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the provided options.
|
|
|
|
*
|
|
|
|
* @param array $options Options
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function applyOptions(array $options)
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2019-03-30 18:21:45 +01:00
|
|
|
foreach ($options as $option => $value) {
|
|
|
|
if (isset($this->$option) && isset($value)) {
|
|
|
|
$this->$option = $value;
|
2015-10-31 15:42:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 18:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override options from environement variables.
|
2019-11-10 18:05:53 +01:00
|
|
|
* 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]'"
|
2018-01-06 18:03:06 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function getEnv()
|
|
|
|
{
|
2019-11-10 18:05:53 +01:00
|
|
|
foreach (get_object_vars($this) as $prop => $value) {
|
|
|
|
$convert = new Convert($prop);
|
2020-02-23 17:26:21 +01:00
|
|
|
$env = getenv($convert->toSnake());
|
2018-01-06 18:03:06 +01:00
|
|
|
if ($env) {
|
2019-11-10 18:05:53 +01:00
|
|
|
$this->$prop = Yaml::parse($env);
|
2018-01-06 18:03:06 +01:00
|
|
|
}
|
2016-12-22 13:46:31 +01:00
|
|
|
}
|
2015-10-31 15:42:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2019-04-21 18:30:02 +02:00
|
|
|
* Get Config singleton instance.
|
2016-09-06 00:36:47 +02:00
|
|
|
*
|
2015-10-31 15:50:32 +01:00
|
|
|
* @return Config
|
|
|
|
*/
|
2019-04-21 18:30:02 +02:00
|
|
|
public static function getInstance()
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2019-04-21 18:30:02 +02:00
|
|
|
if (!isset(self::$instance)) {
|
|
|
|
self::$instance = new self();
|
2015-10-31 15:42:25 +01:00
|
|
|
}
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-03-30 01:49:08 +02:00
|
|
|
return self::$instance;
|
2015-10-31 15:42:25 +01:00
|
|
|
}
|
2016-07-30 12:40:49 +02:00
|
|
|
|
2019-04-21 18:30:02 +02:00
|
|
|
/**
|
|
|
|
* Set options from a YAML file.
|
|
|
|
*
|
|
|
|
* @param string $file Path to the YAML file
|
|
|
|
*/
|
|
|
|
public static function setFile($file)
|
|
|
|
{
|
|
|
|
if (is_file($file)) {
|
|
|
|
$options = Yaml::parse(file_get_contents($file));
|
|
|
|
self::$instance = new self($options);
|
2019-04-28 15:52:01 +02:00
|
|
|
self::$instance->validateOptions();
|
2019-04-21 18:30:02 +02:00
|
|
|
} else {
|
2019-10-03 21:24:12 +02:00
|
|
|
throw new Exception("Can't find config file at " . $file);
|
2019-04-21 18:30:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually set some options.
|
|
|
|
*
|
2019-04-21 18:35:24 +02:00
|
|
|
* @param array $options Options (see `config/config.example.yml` for available options)
|
|
|
|
* @param bool $update True to update an existing instance
|
2019-04-21 18:30:02 +02:00
|
|
|
*/
|
|
|
|
public static function setOptions(array $options, $update = true)
|
|
|
|
{
|
|
|
|
if ($update) {
|
|
|
|
$config = self::getInstance();
|
|
|
|
$config->applyOptions($options);
|
|
|
|
$config->validateOptions();
|
|
|
|
} else {
|
|
|
|
self::$instance = new self($options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Destroy singleton instance.
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-30 12:40:49 +02:00
|
|
|
public static function destroyInstance()
|
|
|
|
{
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
2015-10-31 15:42:25 +01:00
|
|
|
}
|