From b80b9c7b2eee38f9ae5949e5bb7e56a7b9e986c0 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Mon, 24 Apr 2017 19:16:38 +0200 Subject: [PATCH] Remove dependency on curl (fixes #105) avconv/ffmpeg now downloads the video directly when converting --- FAQ.md | 5 ++- README.md | 4 +-- classes/Config.php | 16 --------- classes/VideoDownload.php | 72 ++++++++++++++----------------------- config.example.yml | 2 -- tests/ConfigTest.php | 1 - tests/VideoDownloadTest.php | 6 ++-- 7 files changed, 34 insertions(+), 72 deletions(-) diff --git a/FAQ.md b/FAQ.md index c03462e..4ef649b 100644 --- a/FAQ.md +++ b/FAQ.md @@ -15,7 +15,6 @@ Here are the parameters that you can set: * `youtubedl`: path to your youtube-dl binary * `python`: path to your python binary * `params`: an array of parameters to pass to youtube-dl -* `curl_params`: an array of parameters to pass to curl * `convert`: true to enable audio conversion * `avconv`: path to your avconv or ffmpeg binary * `rtmpdump`: path to your rtmpdump binary @@ -31,10 +30,10 @@ convert: true avconv: path/to/avconv ``` -You will also need to install `avconv` and `curl` on your server: +You will also need to install `avconv` on your server: ```bash -sudo apt-get install libav-tools curl +sudo apt-get install libav-tools ``` ## How do I deploy Alltube on Heroku? diff --git a/README.md b/README.md index 5a3c08f..776aca9 100644 --- a/README.md +++ b/README.md @@ -122,13 +122,13 @@ server { ## Other dependencies -You need [avconv](https://libav.org/avconv.html), [rtmpdump](http://rtmpdump.mplayerhq.hu/) and [curl](https://curl.haxx.se/) in order to enable conversions. +You need [avconv](https://libav.org/avconv.html) and [rtmpdump](http://rtmpdump.mplayerhq.hu/) in order to enable conversions. If you don't want to enable conversions, you can disable it in `config.yml`. On Debian-based systems: ```bash -sudo apt-get install libav-tools rtmpdump curl +sudo apt-get install libav-tools rtmpdump ``` You also probably need to edit the `avconv` variable in `config.yml` so that it points to your ffmpeg/avconv binary (`/usr/bin/avconv` on Debian/Ubuntu). diff --git a/classes/Config.php b/classes/Config.php index a716524..d9ffcf0 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -61,20 +61,6 @@ class Config */ public $rtmpdump = 'vendor/bin/rtmpdump'; - /** - * curl binary path. - * - * @var string - */ - public $curl = '/usr/bin/curl'; - - /** - * curl parameters. - * - * @var array - */ - public $curl_params = []; - /** * Disable URL rewriting. * @@ -104,9 +90,7 @@ class Config * * python: Python binary path * * avconv: avconv or ffmpeg binary path * * rtmpdump: rtmpdump binary path - * * curl: curl binary path * * params: Array of youtube-dl parameters - * * curl_params: Array of curl parameters * * convert: Enable conversion? * * @param array $options Options diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 6543a85..41b0cc0 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -98,7 +98,7 @@ class VideoDownload throw new \Exception($errorOutput); } } else { - return $process->getOutput(); + return trim($process->getOutput()); } } @@ -222,31 +222,30 @@ class VideoDownload } /** - * Get a process that runs curl in order to download a video. + * Get a process that runs avconv in order to convert a video to MP3. * - * @param object $video Video object returned by youtube-dl + * @param string $url URL of the video file * * @return \Symfony\Component\Process\Process Process */ - private function getCurlProcess($video) + private function getAvconvMp3Process($url) { - if (!shell_exec('which '.$this->config->curl)) { - throw(new \Exception('Can\'t find curl')); + if (!shell_exec('which '.$this->config->avconv)) { + throw(new \Exception('Can\'t find avconv or ffmpeg')); } - $builder = ProcessBuilder::create( - array_merge( - [ - $this->config->curl, - '--silent', - '--location', - '--user-agent', $video->http_headers->{'User-Agent'}, - $video->url, - ], - $this->config->curl_params - ) - ); - return $builder->getProcess(); + return ProcessBuilder::create( + [ + $this->config->avconv, + '-v', 'quiet', + //Vimeo needs a correct user-agent + '-user-agent', $this->getProp(null, null, 'dump-user-agent'), + '-i', $url, + '-f', 'mp3', + '-vn', + 'pipe:1', + ] + ); } /** @@ -260,40 +259,23 @@ class VideoDownload */ public function getAudioStream($url, $format, $password = null) { - if (!shell_exec('which '.$this->config->avconv)) { - throw(new \Exception('Can\'t find avconv or ffmpeg')); - } - $video = $this->getJSON($url, $format, $password); if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) { throw(new \Exception('Conversion of M3U8 files is not supported.')); } - //Vimeo needs a correct user-agent - ini_set( - 'user_agent', - $video->http_headers->{'User-Agent'} - ); - $avconvProc = ProcessBuilder::create( - [ - $this->config->avconv, - '-v', 'quiet', - '-i', '-', - '-f', 'mp3', - '-vn', - 'pipe:1', - ] - ); - if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') { $process = $this->getRtmpProcess($video); - } else { - $process = $this->getCurlProcess($video); - } - $chain = new Chain($process); - $chain->add('|', $avconvProc); + $chain = new Chain($process); + $chain->add('|', $this->getAvconvMp3Process('-')); - return popen($chain->getProcess()->getCommandLine(), 'r'); + return popen($chain->getProcess()->getCommandLine(), 'r'); + } else { + $avconvProc = $this->getAvconvMp3Process($video->url); + //dump($avconvProc->getProcess()); die; + + return popen($avconvProc->getProcess()->getCommandLine(), 'r'); + } } /** diff --git a/config.example.yml b/config.example.yml index 6bb93e4..2bdfc78 100644 --- a/config.example.yml +++ b/config.example.yml @@ -5,10 +5,8 @@ params: - --no-warnings - --playlist-end - 1 -curl_params: convert: false avconv: vendor/bin/ffmpeg rtmpdump: vendor/bin/rtmpdump -curl: /usr/bin/curl uglyUrls: false stream: false diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index a11d766..63675af 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -45,7 +45,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testGetInstance() { $this->assertEquals($this->config->convert, false); - $this->assertInternalType('array', $this->config->curl_params); $this->assertInternalType('array', $this->config->params); $this->assertInternalType('string', $this->config->youtubedl); $this->assertInternalType('string', $this->config->python); diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index ce0426e..bf15cfe 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -350,11 +350,11 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase * * @return void * @expectedException Exception - * @dataProvider urlProvider + * @dataProvider rtmpUrlProvider */ - public function testGetAudioStreamCurlError($url, $format) + public function testGetAudioStreamRtmpError($url, $format) { - $download = new VideoDownload(new Config(['curl'=>'foobar', 'rtmpdump'=>'foobar'])); + $download = new VideoDownload(new Config(['rtmpdump'=>'foobar'])); $download->getAudioStream($url, $format); }