diff --git a/classes/Config.php b/classes/Config.php index 0482e36..72779b8 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -38,12 +38,7 @@ class Config * * @var array */ - public $params = [ - '--no-playlist', '--no-warnings', - //We can allow non-HTTP URLs on the feature/stream branch - '-f best', - '--playlist-end', 1, - ]; + public $params = ['--no-playlist', '--no-warnings', '--playlist-end', 1]; /** * Enable audio conversion. @@ -87,6 +82,12 @@ class Config */ public $uglyUrls = false; + /** + * Stream downloaded files trough server? + * @var boolean + */ + public $stream = false; + /** * YAML config file path. * diff --git a/config.example.yml b/config.example.yml index 7fb80ef..6bb93e4 100644 --- a/config.example.yml +++ b/config.example.yml @@ -3,7 +3,6 @@ python: /usr/bin/python params: - --no-playlist - --no-warnings - - -f best[protocol^=http] - --playlist-end - 1 curl_params: @@ -12,3 +11,4 @@ avconv: vendor/bin/ffmpeg rtmpdump: vendor/bin/rtmpdump curl: /usr/bin/curl uglyUrls: false +stream: false diff --git a/controllers/FrontController.php b/controllers/FrontController.php index cc71335..0c101b8 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -68,6 +68,11 @@ class FrontController $session_factory = new \Aura\Session\SessionFactory(); $session = $session_factory->newInstance($_COOKIE); $this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController'); + if ($this->config->stream) { + $this->defaultFormat = 'best'; + } else { + $this->defaultFormat = 'best[protocol^=http]'; + } } /** @@ -156,7 +161,13 @@ class FrontController } if (isset($params['audio'])) { try { - return $this->getStream($params['url'], 'mp3', $response, $request, $password); + if ($this->config->stream) { + return $this->getStream($params['url'], 'mp3', $response, $request, $password); + } else { + $url = $this->download->getURL($params['url'], 'mp3[protocol^=http]', $password); + + return $response->withRedirect($url); + } } catch (PasswordException $e) { return $this->password($request, $response); } catch (\Exception $e) { @@ -176,10 +187,15 @@ class FrontController } } else { try { - $video = $this->download->getJSON($params['url'], null, $password); + $video = $this->download->getJSON($params['url'], $this->defaultFormat, $password); } catch (PasswordException $e) { return $this->password($request, $response); } + if ($this->config->stream) { + $protocol = ''; + } else { + $protocol = '[protocol^=http]'; + } $this->view->render( $response, 'video.tpl', @@ -188,6 +204,8 @@ class FrontController 'class' => 'video', 'title' => $video->title, 'description' => 'Download "'.$video->title.'" from '.$video->extractor_key, + 'protocol' => $protocol, + 'config' => $this->config, ] ); } @@ -270,13 +288,23 @@ class FrontController $params = $request->getQueryParams(); if (isset($params['url'])) { try { - return $this->getStream( - $params['url'], - $request->getParam('format'), - $response, - $request, - $this->sessionSegment->getFlash($params['url']) - ); + if ($this->config->stream) { + return $this->getStream( + $params['url'], + $request->getParam('format'), + $response, + $request, + $this->sessionSegment->getFlash($params['url']) + ); + } else { + $url = $this->download->getURL( + $params['url'], + $request->getParam('format'), + $this->sessionSegment->getFlash($params['url']) + ); + + return $response->withRedirect($url); + } } catch (PasswordException $e) { return $response->withRedirect( $this->container->get('router')->pathFor('video').'?url='.urlencode($params['url']) diff --git a/templates/video.tpl b/templates/video.tpl index 85a4a25..5b82363 100644 --- a/templates/video.tpl +++ b/templates/video.tpl @@ -29,17 +29,18 @@ {/if}


{else} - + Download
{/if} diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index 3c5d760..202e858 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -146,7 +146,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase { return [ [ - 'https://www.youtube.com/watch?v=M7IpKCZ47pU', null, + 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'best[protocol^=http]', "It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU", 'mp4', 'googlevideo.com', @@ -159,7 +159,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase 'googlevideo.com', ], [ - 'https://vimeo.com/24195442', null, + 'https://vimeo.com/24195442', 'best[protocol^=http]', 'Carving the Mountains-24195442', 'mp4', 'vimeocdn.com', @@ -188,7 +188,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase { return [ [ - 'https://twitter.com/verge/status/813055465324056576/video/1', null, + 'https://twitter.com/verge/status/813055465324056576/video/1', 'best', 'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576', 'mp4', 'video.twimg.com', @@ -225,6 +225,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase $this->assertObjectHasAttribute('url', $info); $this->assertObjectHasAttribute('ext', $info); $this->assertObjectHasAttribute('title', $info); + $this->assertObjectHasAttribute('extractor_key', $info); $this->assertObjectHasAttribute('formats', $info); $this->assertObjectHasAttribute('_filename', $info); } @@ -322,7 +323,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetAudioStreamAvconvError($url, $format) { - $config = \Alltube\Config::getInstance(); + $config = Config::getInstance(); $config->avconv = 'foobar'; $this->download->getAudioStream($url, $format); } @@ -339,7 +340,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetAudioStreamCurlError($url, $format) { - $config = \Alltube\Config::getInstance(); + $config = Config::getInstance(); $config->curl = 'foobar'; $config->rtmpdump = 'foobar'; $this->download->getAudioStream($url, $format);