Merge branch 'develop' into feature/stream

Conflicts:
	composer.lock
This commit is contained in:
Pierre Rudloff 2016-08-18 23:23:36 +02:00
commit e7924d584e
23 changed files with 1879 additions and 334 deletions

View file

@ -1,39 +1,44 @@
<?php
/**
* FrontController class
*
* PHP Version 5.3.10
*
* @category Youtube-dl
* @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro>
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
*/
namespace Alltube\Controller;
use Alltube\VideoDownload;
use Alltube\Config;
use Symfony\Component\Process\ProcessBuilder;
use Chain\Chain;
use Slim\Http\Stream;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;
/**
* Main controller
*
* PHP Version 5.3.10
*
* @category Youtube-dl
* @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro>
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
*/
class FrontController
{
public function __construct($container)
/**
* Config instance
* @var Config
*/
private $config;
/**
* VideoDownload instance
* @var VideoDownload
*/
private $download;
/**
* Slim dependency container
* @var Container
*/
private $container;
/**
* FrontController constructor
* @param Container $container Slim dependency container
*/
public function __construct(Container $container)
{
$this->config = Config::getInstance();
$this->download = new VideoDownload();
@ -52,24 +57,13 @@ class FrontController
{
$this->container->view->render(
$response,
'head.tpl',
'index.tpl',
array(
'convert'=>$this->config->convert,
'class'=>'index',
'description'=>'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
)
);
$this->container->view->render(
$response,
'header.tpl'
);
$this->container->view->render(
$response,
'index.tpl',
array(
'convert'=>$this->config->convert
)
);
$this->container->view->render($response, 'footer.tpl');
}
/**
@ -84,24 +78,15 @@ class FrontController
{
$this->container->view->render(
$response,
'head.tpl',
'extractors.tpl',
array(
'extractors'=>$this->download->listExtractors(),
'class'=>'extractors',
'title'=>'Supported websites',
'description'
=>'List of all supported websites from which Alltube Download can extract video or audio files'
)
);
$this->container->view->render($response, 'header.tpl');
$this->container->view->render($response, 'logo.tpl');
$this->container->view->render(
$response,
'extractors.tpl',
array(
'extractors'=>$this->download->listExtractors()
)
);
$this->container->view->render($response, 'footer.tpl');
}
/**
@ -110,7 +95,7 @@ class FrontController
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @return void
* @return Response HTTP response
*/
public function video(Request $request, Response $response)
{
@ -121,96 +106,16 @@ class FrontController
try {
return $this->getStream($params["url"], 'mp3[protocol^=http]', $response, $request);
} catch (\Exception $e) {
$video = $this->download->getJSON($params["url"], 'bestaudio/best');
if (!shell_exec('which '.$this->config->avconv)) {
throw(new \Exception('Can\'t find avconv or ffmpeg'));
}
$avconvProc = ProcessBuilder::create(
array(
$this->config->avconv,
'-v', 'quiet',
'-i', '-',
'-f', 'mp3',
'-vn',
'pipe:1'
)
);
//Vimeo needs a correct user-agent
ini_set(
'user_agent',
$video->http_headers->{'User-Agent'}
);
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
html_entity_decode(
pathinfo(
$video->_filename,
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
).'"'
$this->download->getAudioFilename($params["url"], 'bestaudio/best').'"'
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') {
if (!shell_exec('which '.$this->config->rtmpdump)) {
throw(new \Exception('Can\'t find rtmpdump'));
}
$builder = new ProcessBuilder(
array(
$this->config->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);
} else {
if (!shell_exec('which curl')) {
throw(new \Exception('Can\'t find curl'));
}
$chain = new Chain(
ProcessBuilder::create(
array_merge(
array(
'curl',
'--silent',
'--location',
'--user-agent', $video->http_headers->{'User-Agent'},
$video->url
),
$this->config->curl_params
)
)
);
$chain->add('|', $avconvProc);
}
if ($request->isGet()) {
$response = $response->withBody(new Stream(popen($chain->getProcess()->getCommandLine(), 'r')));
$process = $this->download->getAudioStream($params["url"], 'bestaudio/best');
$response = $response->withBody(new Stream($process));
}
return $response;
}
@ -218,45 +123,38 @@ class FrontController
$video = $this->download->getJSON($params["url"]);
$this->container->view->render(
$response,
'head.tpl',
'video.tpl',
array(
'video'=>$video,
'class'=>'video',
'title'=>$video->title,
'description'=>'Download "'.$video->title.'" from '.$video->extractor_key
)
);
$this->container->view->render(
$response,
'video.tpl',
array(
'video'=>$video
)
);
$this->container->view->render($response, 'footer.tpl');
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
}
}
/**
* Display an error page
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param \Exception $exception Error to display
* @return Response HTTP response
*/
public function error(Request $request, Response $response, \Exception $exception)
{
$this->container->view->render(
$response,
'head.tpl',
'error.tpl',
array(
'errors'=>$exception->getMessage(),
'class'=>'video',
'title'=>'Error'
)
);
$this->container->view->render(
$response,
'error.tpl',
array(
'errors'=>$exception->getMessage()
)
);
$this->container->view->render($response, 'footer.tpl');
return $response->withStatus(500);
}
@ -283,7 +181,7 @@ class FrontController
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @return void
* @return Response HTTP response
*/
public function redirect(Request $request, Response $response)
{
@ -304,7 +202,7 @@ class FrontController
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @return void
* @return Response HTTP response
*/
public function json(Request $request, Response $response)
{