Use ProcessBuilder to build commands (fixes #51)

This commit is contained in:
Pierre Rudloff 2016-04-08 19:37:59 +02:00
parent 11e8243443
commit f7f0a7b7f4
3 changed files with 46 additions and 30 deletions

View file

@ -31,7 +31,7 @@ class Config
public $youtubedl = 'vendor/rg3/youtube-dl/youtube_dl/__main__.py';
public $python = '/usr/bin/python';
public $params = '--no-playlist --no-warnings -f best';
public $params = array('--no-playlist', '--no-warnings', '-f best');
public $convert = false;
public $avconv = 'vendor/bin/ffmpeg';
public $curl_params = '';

View file

@ -13,6 +13,7 @@
namespace Alltube;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
/**
* Main class
@ -30,6 +31,13 @@ class VideoDownload
public function __construct()
{
$this->config = Config::getInstance();
$this->procBuilder = new ProcessBuilder();
$this->procBuilder->setPrefix(
array_merge(
array($this->config->python, $this->config->youtubedl),
$this->config->params
)
);
}
/**
@ -39,11 +47,12 @@ class VideoDownload
* */
public function getUA()
{
$cmd = escapeshellcmd(
$this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$this->config->params
$this->procBuilder->setArguments(
array(
'--dump-user-agent'
)
);
$process = new Process($cmd.' --dump-user-agent');
$process = $this->procBuilder->getProcess();
$process->run();
return trim($process->getOutput());
}
@ -55,11 +64,12 @@ class VideoDownload
* */
public function listExtractors()
{
$cmd = escapeshellcmd(
$this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$this->config->params
$this->procBuilder->setArguments(
array(
'--list-extractors'
)
);
$process = new Process($cmd.' --list-extractors');
$process = $this->procBuilder->getProcess();
$process->run();
return explode(PHP_EOL, $process->getOutput());
}
@ -74,15 +84,16 @@ class VideoDownload
* */
public function getFilename($url, $format = null)
{
$cmd = escapeshellcmd(
$this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$this->config->params
$this->procBuilder->setArguments(
array(
'--get-filename',
$url
)
);
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
$this->procBuilder->add('-f '.$format);
}
$cmd .=' --get-filename '.escapeshellarg($url)." 2>&1";
$process = new Process($cmd);
$process = $this->procBuilder->getProcess();
$process->run();
return trim($process->getOutput());
}
@ -97,18 +108,19 @@ class VideoDownload
* */
public function getJSON($url, $format = null)
{
$cmd = escapeshellcmd(
$this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$this->config->params
$this->procBuilder->setArguments(
array(
'--dump-json',
$url
)
);
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
$this->procBuilder->add('-f '.$format);
}
$cmd .=' --dump-json '.escapeshellarg($url)." 2>&1";
$process = new Process($cmd);
$process = $this->procBuilder->getProcess();
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getOutput());
throw new \Exception($process->getErrorOutput());
} else {
return json_decode($process->getOutput());
}
@ -124,18 +136,19 @@ class VideoDownload
* */
public function getURL($url, $format = null)
{
$cmd = escapeshellcmd(
$this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$this->config->params
$this->procBuilder->setArguments(
array(
'--get-url',
$url
)
);
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
$this->procBuilder->add('-f '.$format);
}
$cmd .=' -g '.escapeshellarg($url)." 2>&1";
$process = new Process($cmd);
$process = $this->procBuilder->getProcess();
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getOutput());
throw new \Exception($process->getErrorOutput());
} else {
return array('success'=>true, 'url'=>$process->getOutput());
}

View file

@ -1,6 +1,9 @@
youtubedl: vendor/rg3/youtube-dl/youtube_dl/__main__.py
python: /usr/bin/python
params: --no-playlist --no-warnings -f best
params:
- --no-playlist
- --no-warnings
- -f best
curl_params:
convert: false
avconv: vendor/bin/ffmpeg