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,58 +115,58 @@ 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);
if ($url_info['scheme'] == 'rtmp') {
ob_end_flush(); try {
header( return $this->getStream($params["url"], 'bestaudio', $response, $request);
'Content-Disposition: attachment; filename="'. } catch (\Exception $e) {
html_entity_decode( if ($url_info['scheme'] == 'rtmp') {
pathinfo( ob_end_flush();
$this->download->getFilename( header(
$video->webpage_url 'Content-Disposition: attachment; filename="'.
), html_entity_decode(
PATHINFO_FILENAME pathinfo(
).'.mp3', $video->_filename,
ENT_COMPAT, PATHINFO_FILENAME
'ISO-8859-1' ).'.mp3',
).'"' ENT_COMPAT,
); 'ISO-8859-1'
header("Content-Type: audio/mpeg"); ).'"'
passthru( );
'/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url). header("Content-Type: audio/mpeg");
' | '.$this->config->avconv. passthru(
' -v quiet -i - -f mp3 -vn pipe:1' '/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url).
); ' | '.$this->config->avconv.
exit; ' -v quiet -i - -f mp3 -vn pipe:1'
} else { );
ob_end_flush(); exit;
header( } else {
'Content-Disposition: attachment; filename="'. ob_end_flush();
html_entity_decode( header(
pathinfo( 'Content-Disposition: attachment; filename="'.
$this->download->getFilename( html_entity_decode(
$video->webpage_url pathinfo(
), $video->_filename,
PATHINFO_FILENAME PATHINFO_FILENAME
).'.mp3', ).'.mp3',
ENT_COMPAT, ENT_COMPAT,
'ISO-8859-1' 'ISO-8859-1'
).'"' ).'"'
); );
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');