2014-03-13 19:07:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload class.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2015-10-29 19:43:43 +00:00
|
|
|
namespace Alltube;
|
2016-03-29 23:49:08 +00:00
|
|
|
|
2016-07-29 22:15:17 +00:00
|
|
|
use Chain\Chain;
|
2016-09-07 22:28:28 +00:00
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
2016-03-31 22:42:28 +00:00
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Extract info about videos.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-03-29 23:49:08 +00:00
|
|
|
class VideoDownload
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-10-10 19:32:07 +00:00
|
|
|
private $config;
|
|
|
|
private $procBuilder;
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload constructor.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-04-08 17:06:41 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->config = Config::getInstance();
|
2016-04-08 17:37:59 +00:00
|
|
|
$this->procBuilder = new ProcessBuilder();
|
|
|
|
$this->procBuilder->setPrefix(
|
|
|
|
array_merge(
|
2016-09-07 22:28:28 +00:00
|
|
|
[$this->config->python, $this->config->youtubedl],
|
2016-04-08 17:37:59 +00:00
|
|
|
$this->config->params
|
|
|
|
)
|
|
|
|
);
|
2016-04-08 17:06:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* List all extractors.
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return string[] Extractors
|
2014-03-13 19:07:56 +00:00
|
|
|
* */
|
2016-04-08 17:06:41 +00:00
|
|
|
public function listExtractors()
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-04-08 17:37:59 +00:00
|
|
|
$this->procBuilder->setArguments(
|
2016-09-07 22:28:28 +00:00
|
|
|
[
|
|
|
|
'--list-extractors',
|
|
|
|
]
|
2016-02-28 22:04:53 +00:00
|
|
|
);
|
2016-04-08 17:37:59 +00:00
|
|
|
$process = $this->procBuilder->getProcess();
|
2016-03-31 22:42:28 +00:00
|
|
|
$process->run();
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-08-05 23:46:29 +00:00
|
|
|
return explode(PHP_EOL, trim($process->getOutput()));
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2015-09-04 20:45:55 +00:00
|
|
|
|
2016-10-13 14:40:19 +00:00
|
|
|
private function getProp($url, $format = null, $prop = 'dump-json')
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-04-08 17:37:59 +00:00
|
|
|
$this->procBuilder->setArguments(
|
2016-09-07 22:28:28 +00:00
|
|
|
[
|
2016-10-13 14:40:19 +00:00
|
|
|
'--'.$prop,
|
2016-09-07 22:28:28 +00:00
|
|
|
$url,
|
|
|
|
]
|
2016-02-28 22:04:53 +00:00
|
|
|
);
|
2014-03-13 19:07:56 +00:00
|
|
|
if (isset($format)) {
|
2016-04-08 17:37:59 +00:00
|
|
|
$this->procBuilder->add('-f '.$format);
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2016-04-08 17:37:59 +00:00
|
|
|
$process = $this->procBuilder->getProcess();
|
2016-03-31 22:42:28 +00:00
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
2016-04-08 17:37:59 +00:00
|
|
|
throw new \Exception($process->getErrorOutput());
|
2014-03-18 14:08:16 +00:00
|
|
|
} else {
|
2016-10-13 14:40:19 +00:00
|
|
|
return $process->getOutput();
|
2014-03-18 14:08:16 +00:00
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:40:19 +00:00
|
|
|
/**
|
|
|
|
* Get all information about a video.
|
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
*
|
|
|
|
* @return object Decoded JSON
|
|
|
|
* */
|
|
|
|
public function getJSON($url, $format = null)
|
|
|
|
{
|
|
|
|
return json_decode($this->getProp($url, $format, 'dump-json'));
|
|
|
|
}
|
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get URL of video from URL of page.
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2014-03-13 19:07:56 +00:00
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2014-03-13 19:07:56 +00:00
|
|
|
* @return string URL of video
|
|
|
|
* */
|
2016-04-08 17:06:41 +00:00
|
|
|
public function getURL($url, $format = null)
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-10-13 14:40:19 +00:00
|
|
|
return $this->getProp($url, $format, 'get-url');
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2016-07-29 22:15:17 +00:00
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get filename of video file from URL of page.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
*
|
|
|
|
* @return string Filename of extracted video
|
|
|
|
* */
|
2016-07-29 22:15:17 +00:00
|
|
|
public function getFilename($url, $format = null)
|
|
|
|
{
|
2016-10-13 14:40:19 +00:00
|
|
|
return trim($this->getProp($url, $format, 'get-filename'));
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get filename of audio from URL of page.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
*
|
|
|
|
* @return string Filename of converted audio file
|
|
|
|
* */
|
2016-07-29 22:15:17 +00:00
|
|
|
public function getAudioFilename($url, $format = null)
|
|
|
|
{
|
|
|
|
return html_entity_decode(
|
|
|
|
pathinfo(
|
|
|
|
$this->getFilename($url, $format),
|
|
|
|
PATHINFO_FILENAME
|
|
|
|
).'.mp3',
|
|
|
|
ENT_COMPAT,
|
|
|
|
'ISO-8859-1'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get audio stream of converted video.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
*
|
|
|
|
* @return resource popen stream
|
|
|
|
*/
|
2016-07-29 22:47:46 +00:00
|
|
|
public function getAudioStream($url, $format)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
|
|
|
if (!shell_exec('which '.$this->config->avconv)) {
|
|
|
|
throw(new \Exception('Can\'t find avconv or ffmpeg'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$video = $this->getJSON($url, $format);
|
|
|
|
|
|
|
|
//Vimeo needs a correct user-agent
|
|
|
|
ini_set(
|
|
|
|
'user_agent',
|
|
|
|
$video->http_headers->{'User-Agent'}
|
|
|
|
);
|
|
|
|
$avconvProc = ProcessBuilder::create(
|
2016-09-07 22:28:28 +00:00
|
|
|
[
|
2016-07-29 22:15:17 +00:00
|
|
|
$this->config->avconv,
|
|
|
|
'-v', 'quiet',
|
|
|
|
'-i', '-',
|
|
|
|
'-f', 'mp3',
|
|
|
|
'-vn',
|
2016-09-07 22:28:28 +00:00
|
|
|
'pipe:1',
|
|
|
|
]
|
2016-07-29 22:15:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
2016-09-07 22:28:28 +00:00
|
|
|
[
|
2016-07-29 22:15:17 +00:00
|
|
|
$this->config->rtmpdump,
|
|
|
|
'-q',
|
|
|
|
'-r',
|
|
|
|
$video->url,
|
2016-09-07 22:28:28 +00:00
|
|
|
'--pageUrl', $video->webpage_url,
|
|
|
|
]
|
2016-07-29 22:15:17 +00:00
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
2016-07-30 12:01:00 +00:00
|
|
|
if (isset($video->rtmp_conn)) {
|
|
|
|
foreach ($video->rtmp_conn as $conn) {
|
|
|
|
$builder->add('--conn');
|
|
|
|
$builder->add($conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($video->app)) {
|
|
|
|
$builder->add('--app');
|
|
|
|
$builder->add($video->app);
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
|
|
|
$chain = new Chain($builder->getProcess());
|
|
|
|
$chain->add('|', $avconvProc);
|
|
|
|
} else {
|
2016-07-30 12:01:00 +00:00
|
|
|
if (!shell_exec('which '.$this->config->curl)) {
|
2016-07-29 22:15:17 +00:00
|
|
|
throw(new \Exception('Can\'t find curl'));
|
|
|
|
}
|
|
|
|
$chain = new Chain(
|
|
|
|
ProcessBuilder::create(
|
|
|
|
array_merge(
|
2016-09-07 22:28:28 +00:00
|
|
|
[
|
2016-07-30 12:01:00 +00:00
|
|
|
$this->config->curl,
|
2016-07-29 22:15:17 +00:00
|
|
|
'--silent',
|
|
|
|
'--location',
|
|
|
|
'--user-agent', $video->http_headers->{'User-Agent'},
|
2016-09-07 22:28:28 +00:00
|
|
|
$video->url,
|
|
|
|
],
|
2016-07-29 22:15:17 +00:00
|
|
|
$this->config->curl_params
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$chain->add('|', $avconvProc);
|
|
|
|
}
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-07-29 22:15:17 +00:00
|
|
|
return popen($chain->getProcess()->getCommandLine(), 'r');
|
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|