Use bestaudio format if available

This commit is contained in:
Pierre Rudloff 2016-04-08 23:01:07 +02:00
parent fca0ab6b49
commit 9354174b77

View file

@ -115,21 +115,22 @@ class FrontController
$video = $this->download->getJSON($params["url"]); $video = $this->download->getJSON($params["url"]);
//Vimeo needs a correct user-agent //Vimeo needs a correct user-agent
$UA = $this->download->getUA();
ini_set( ini_set(
'user_agent', 'user_agent',
$UA $video->http_headers->{'User-Agent'}
); );
$url_info = parse_url($video->url); $url_info = parse_url($video->url);
try {
return $this->getStream($params["url"], 'bestaudio', $response, $request);
} catch (\Exception $e) {
if ($url_info['scheme'] == 'rtmp') { if ($url_info['scheme'] == 'rtmp') {
ob_end_flush(); ob_end_flush();
header( header(
'Content-Disposition: attachment; filename="'. 'Content-Disposition: attachment; filename="'.
html_entity_decode( html_entity_decode(
pathinfo( pathinfo(
$this->download->getFilename( $video->_filename,
$video->webpage_url
),
PATHINFO_FILENAME PATHINFO_FILENAME
).'.mp3', ).'.mp3',
ENT_COMPAT, ENT_COMPAT,
@ -149,9 +150,7 @@ class FrontController
'Content-Disposition: attachment; filename="'. 'Content-Disposition: attachment; filename="'.
html_entity_decode( html_entity_decode(
pathinfo( pathinfo(
$this->download->getFilename( $video->_filename,
$video->webpage_url
),
PATHINFO_FILENAME PATHINFO_FILENAME
).'.mp3', ).'.mp3',
ENT_COMPAT, ENT_COMPAT,
@ -161,13 +160,14 @@ class FrontController
header("Content-Type: audio/mpeg"); header("Content-Type: audio/mpeg");
passthru( passthru(
'curl '.$this->config->curl_params. 'curl '.$this->config->curl_params.
' --user-agent '.escapeshellarg($UA). ' --user-agent '.escapeshellarg($video->http_headers->{'User-Agent'}).
' '.escapeshellarg($video->url). ' '.escapeshellarg($video->url).
' | '.$this->config->avconv. ' | '.$this->config->avconv.
' -v quiet -i - -f mp3 -vn pipe:1' ' -v quiet -i - -f mp3 -vn pipe:1'
); );
exit; exit;
} }
}
} catch (\Exception $e) { } catch (\Exception $e) {
$error = $e->getMessage(); $error = $e->getMessage();
} }
@ -213,6 +213,23 @@ class FrontController
} }
} }
private function getStream($url, $format, $response, $request)
{
if (!isset($format)) {
$format = 'best';
}
$video = $this->download->getJSON($url, $format);
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, array('stream'=>true));
$response = $response->withHeader('Content-Disposition', 'inline; filename="'.$video->_filename.'"');
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
}
return $response;
}
/** /**
* Redirect to video file * Redirect to video file
* *
@ -227,17 +244,7 @@ class FrontController
$params = $request->getQueryParams(); $params = $request->getQueryParams();
if (isset($params["url"])) { if (isset($params["url"])) {
try { try {
$format = isset($params["format"]) ? $params["format"] : 'best'; return $this->getStream($params["url"], $params["format"], $response, $request);
$video = $this->download->getJSON($params["url"], $format);
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, array('stream'=>true));
$response = $response->withHeader('Content-Disposition', 'inline; filename="'.$video->_filename.'"');
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
}
return $response;
} catch (\Exception $e) { } catch (\Exception $e) {
$response->getBody()->write($e->getMessage().PHP_EOL); $response->getBody()->write($e->getMessage().PHP_EOL);
return $response->withHeader('Content-Type', 'text/plain'); return $response->withHeader('Content-Type', 'text/plain');