From b787b6d3a28228d4ae62ce6acc1b8c3081a3959e Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Tue, 12 Apr 2016 11:49:50 +0200 Subject: [PATCH] Use ProcessBuilder to create commands Fix rtmp audio --- classes/Config.php | 4 +- composer.json | 1 + composer.lock | 40 ++++++++++++++++++- controllers/FrontController.php | 69 ++++++++++++++++++++++++++------- 4 files changed, 97 insertions(+), 17 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index cb57e67..2b31571 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -31,10 +31,10 @@ class Config public $youtubedl = 'vendor/rg3/youtube-dl/youtube_dl/__main__.py'; public $python = '/usr/bin/python'; - public $params = array('--no-playlist', '--no-warnings', '-f best', '--playlist-end', 1); + public $params = array('--no-playlist', '--no-warnings', '-f best[protocol^=http]', '--playlist-end', 1); public $convert = false; public $avconv = 'vendor/bin/ffmpeg'; - public $curl_params = ''; + public $curl_params = array(); /** * Config constructor diff --git a/composer.json b/composer.json index 22f0f25..702e132 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "mathmarques/smarty-view": "~1.1.0", "symfony/yaml": "~3.0.0", "symfony/process": "~3.0.0", + "ptachoire/process-builder-chain": "~1.2.0", "ffmpeg/ffmpeg": "~2.8.2", "rudloff/smarty-plugin-noscheme": "~0.1.0" }, diff --git a/composer.lock b/composer.lock index 7984870..367538b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "0a09fa0b589ef92192be06b640a2e7e5", - "content-hash": "7e43db00a5d5e02e3bcd909198e0f98f", + "hash": "b8327e8b80a073db8e2a9f5fdd0c750e", + "content-hash": "b1a553391862fc80a83960f27c5fd530", "packages": [ { "name": "container-interop/container-interop", @@ -362,6 +362,42 @@ ], "time": "2015-05-04 20:22:00" }, + { + "name": "ptachoire/process-builder-chain", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/krichprollsch/process-builder-chain.git", + "reference": "465055dbcc3b5ef792a768df935571551de4781a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/krichprollsch/process-builder-chain/zipball/465055dbcc3b5ef792a768df935571551de4781a", + "reference": "465055dbcc3b5ef792a768df935571551de4781a", + "shasum": "" + }, + "require": { + "symfony/process": "~2.5 || ~3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "Chain": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Pierre Tachoire", + "email": "pierre.tachoire@gmail.com" + } + ], + "description": "Add ability to chain symfony processes", + "time": "2016-04-10 08:33:20" + }, { "name": "rg3/youtube-dl", "version": "2016.04.06", diff --git a/controllers/FrontController.php b/controllers/FrontController.php index f99d19d..5e75bf8 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -14,6 +14,8 @@ namespace Alltube\Controller; use Alltube\VideoDownload; use Alltube\Config; +use Symfony\Component\Process\ProcessBuilder; +use Chain\Chain; /** * Main controller @@ -116,7 +118,18 @@ class FrontController $url = $this->download->getURL($params["url"], 'bestaudio[protocol^=http]'); return $response->withRedirect($url); } catch (\Exception $e) { - $video = $this->download->getJSON($params["url"]); + $video = $this->download->getJSON($params["url"], 'best'); + + $avconvProc = ProcessBuilder::create( + array( + $this->config->avconv, + '-v', 'quiet', + '-i', '-', + '-f', 'mp3', + '-vn', + 'pipe:1' + ) + ); //Vimeo needs a correct user-agent ini_set( @@ -124,6 +137,33 @@ class FrontController $video->http_headers->{'User-Agent'} ); if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') { + $builder = new ProcessBuilder( + array( + '/usr/bin/rtmpdump', + //'-q', + '-r', + $video->url, + '--pageUrl', $video->webpage_url + ) + ); + if (isset($video->player_url)) { + $builder->add('--swfVfy'); + $builder->add($video->player_url); + } + if (isset($video->flash_version)) { + $builder->add('--flashVer'); + $builder->add($video->flash_version); + } + if (isset($video->play_path)) { + $builder->add('--playpath'); + $builder->add($video->play_path); + } + foreach ($video->rtmp_conn as $conn) { + $builder->add('--conn'); + $builder->add($conn); + } + $chain = new Chain($builder->getProcess()); + $chain->add('|', $avconvProc); ob_end_flush(); header( 'Content-Disposition: attachment; filename="'. @@ -137,13 +177,22 @@ class FrontController ).'"' ); header("Content-Type: audio/mpeg"); - passthru( - '/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url). - ' | '.$this->config->avconv. - ' -v quiet -i - -f mp3 -vn pipe:1' - ); + passthru($chain->getProcess()->getCommandLine()); exit; } else { + $chain = new Chain( + ProcessBuilder::create( + array_merge( + array('curl'), + $this->config->curl_params, + array( + '--user-agent', $video->http_headers->{'User-Agent'}, + $video->url + ) + ) + ) + ); + $chain->add('|', $avconvProc); ob_end_flush(); header( 'Content-Disposition: attachment; filename="'. @@ -157,13 +206,7 @@ class FrontController ).'"' ); header("Content-Type: audio/mpeg"); - passthru( - 'curl '.$this->config->curl_params. - ' --user-agent '.escapeshellarg($video->http_headers->{'User-Agent'}). - ' '.escapeshellarg($video->url). - ' | '.$this->config->avconv. - ' -v quiet -i - -f mp3 -vn pipe:1' - ); + passthru($chain->getProcess()->getCommandLine()); exit; } }