From a89b3380603625a59b94a7659b7a6b7c12efc94c Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Sun, 14 May 2017 00:52:59 +0200 Subject: [PATCH] Improve the way we test if a command can be ran (see #109) --- classes/VideoDownload.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 04db621..0be13d4 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -220,7 +220,7 @@ class VideoDownload */ private function getRtmpProcess(\stdClass $video) { - if (!shell_exec('which '.$this->config->rtmpdump)) { + if (!$this->checkCommand([$this->config->rtmpdump, '--help'])) { throw(new \Exception('Can\'t find rtmpdump')); } $builder = new ProcessBuilder( @@ -240,6 +240,22 @@ class VideoDownload return $builder->getProcess(); } + /** + * Check if a command runs successfully + * + * @param array $command Command and arguments + * + * @return bool False if the command returns an error, true otherwise + */ + private function checkCommand(array $command) + { + $builder = ProcessBuilder::create($command); + $process = $builder->getProcess(); + $process->run(); + + return $process->isSuccessful(); + } + /** * Get a process that runs avconv in order to convert a video to MP3. * @@ -249,7 +265,7 @@ class VideoDownload */ private function getAvconvMp3Process($url) { - if (!shell_exec('which '.$this->config->avconv)) { + if (!$this->checkCommand([$this->config->avconv, '-version'])) { throw(new \Exception('Can\'t find avconv or ffmpeg')); } @@ -307,7 +323,7 @@ class VideoDownload */ public function getM3uStream(\stdClass $video) { - if (!shell_exec('which '.$this->config->avconv)) { + if (!$this->checkCommand([$this->config->avconv, '-version'])) { throw(new \Exception('Can\'t find avconv or ffmpeg')); }