Support audio conversion of password protected videos

This commit is contained in:
Pierre Rudloff 2016-10-20 23:13:37 +02:00
parent 75ddf27a95
commit efe0b97d7f
2 changed files with 16 additions and 12 deletions

View file

@ -154,16 +154,17 @@ class VideoDownload
/** /**
* Get filename of audio from URL of page. * Get filename of audio from URL of page.
* *
* @param string $url URL of page * @param string $url URL of page
* @param string $format Format to use for the video * @param string $format Format to use for the video
* @param string $password Video password
* *
* @return string Filename of converted audio file * @return string Filename of converted audio file
* */ * */
public function getAudioFilename($url, $format = null) public function getAudioFilename($url, $format = null, $password = null)
{ {
return html_entity_decode( return html_entity_decode(
pathinfo( pathinfo(
$this->getFilename($url, $format), $this->getFilename($url, $format, $password),
PATHINFO_FILENAME PATHINFO_FILENAME
).'.mp3', ).'.mp3',
ENT_COMPAT, ENT_COMPAT,
@ -258,18 +259,19 @@ class VideoDownload
/** /**
* Get audio stream of converted video. * Get audio stream of converted video.
* *
* @param string $url URL of page * @param string $url URL of page
* @param string $format Format to use for the video * @param string $format Format to use for the video
* @param string $password Video password
* *
* @return resource popen stream * @return resource popen stream
*/ */
public function getAudioStream($url, $format) public function getAudioStream($url, $format, $password = null)
{ {
if (!shell_exec('which '.$this->config->avconv)) { if (!shell_exec('which '.$this->config->avconv)) {
throw(new \Exception('Can\'t find avconv or ffmpeg')); throw(new \Exception('Can\'t find avconv or ffmpeg'));
} }
$video = $this->getJSON($url, $format); $video = $this->getJSON($url, $format, $password);
//Vimeo needs a correct user-agent //Vimeo needs a correct user-agent
ini_set( ini_set(

View file

@ -143,19 +143,21 @@ class FrontController
} }
if (isset($params['audio'])) { if (isset($params['audio'])) {
try { try {
$url = $this->download->getURL($params['url'], 'mp3[protocol^=http]'); $url = $this->download->getURL($params['url'], 'mp3[protocol^=http]', $password);
return $response->withRedirect($url); return $response->withRedirect($url);
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (\Exception $e) { } catch (\Exception $e) {
$response = $response->withHeader( $response = $response->withHeader(
'Content-Disposition', 'Content-Disposition',
'attachment; filename="'. 'attachment; filename="'.
$this->download->getAudioFilename($params['url'], 'bestaudio/best').'"' $this->download->getAudioFilename($params['url'], 'bestaudio/best', $password).'"'
); );
$response = $response->withHeader('Content-Type', 'audio/mpeg'); $response = $response->withHeader('Content-Type', 'audio/mpeg');
if ($request->isGet()) { if ($request->isGet() || $request->isPost()) {
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best'); $process = $this->download->getAudioStream($params['url'], 'bestaudio/best', $password);
$response = $response->withBody(new Stream($process)); $response = $response->withBody(new Stream($process));
} }