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 $youtubedl = 'vendor/rg3/youtube-dl/youtube_dl/__main__.py';
public $python = '/usr/bin/python'; 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 $convert = false;
public $avconv = 'vendor/bin/ffmpeg'; public $avconv = 'vendor/bin/ffmpeg';
public $curl_params = ''; public $curl_params = '';

View File

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

View File

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