alltube/classes/VideoDownload.php

233 lines
6.5 KiB
PHP
Raw Normal View History

2014-03-13 19:07:56 +00:00
<?php
/**
2015-10-31 14:42:25 +00:00
* VideoDownload class
*
2014-03-13 19:07:56 +00:00
* PHP Version 5.3.10
*
2014-03-13 19:07:56 +00:00
* @category Youtube-dl
* @package Youtubedl
2015-01-07 09:47:46 +00:00
* @author Pierre Rudloff <contact@rudloff.pro>
2014-03-13 19:07:56 +00:00
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
2015-10-29 19:43:43 +00:00
namespace Alltube;
2016-03-29 23:49:08 +00:00
2016-03-31 22:42:28 +00:00
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
use Chain\Chain;
2016-03-31 22:42:28 +00:00
2014-03-13 19:07:56 +00:00
/**
* Main class
*
2014-03-13 19:07:56 +00:00
* PHP Version 5.3.10
*
2014-03-13 19:07:56 +00:00
* @category Youtube-dl
* @package Youtubedl
2015-01-07 09:47:46 +00:00
* @author Pierre Rudloff <contact@rudloff.pro>
2014-03-13 19:07:56 +00:00
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
2016-03-29 23:49:08 +00:00
class VideoDownload
2014-03-13 19:07:56 +00:00
{
2016-04-08 17:06:41 +00:00
public function __construct()
{
$this->config = Config::getInstance();
$this->procBuilder = new ProcessBuilder();
$this->procBuilder->setPrefix(
array_merge(
array($this->config->python, $this->config->youtubedl),
$this->config->params
)
);
2016-04-08 17:06:41 +00:00
}
2014-03-13 19:07:56 +00:00
/**
* List all extractors
*
2014-03-13 19:07:56 +00:00
* @return array Extractors
* */
2016-04-08 17:06:41 +00:00
public function listExtractors()
2014-03-13 19:07:56 +00:00
{
$this->procBuilder->setArguments(
array(
'--list-extractors'
)
2016-02-28 22:04:53 +00:00
);
$process = $this->procBuilder->getProcess();
2016-03-31 22:42:28 +00:00
$process->run();
return explode(PHP_EOL, $process->getOutput());
2014-03-13 19:07:56 +00:00
}
2014-03-13 19:07:56 +00:00
/**
* Get all information about a video
*
2014-03-13 19:07:56 +00:00
* @param string $url URL of page
* @param string $format Format to use for the video
*
2016-04-08 18:08:04 +00:00
* @return object Decoded JSON
2014-03-13 19:07:56 +00:00
* */
2016-04-08 17:06:41 +00:00
public function getJSON($url, $format = null)
2014-03-13 19:07:56 +00:00
{
$this->procBuilder->setArguments(
array(
'--dump-json',
$url
)
2016-02-28 22:04:53 +00:00
);
2014-03-13 19:07:56 +00:00
if (isset($format)) {
$this->procBuilder->add('-f '.$format);
2014-03-13 19:07:56 +00:00
}
$process = $this->procBuilder->getProcess();
2016-03-31 22:42:28 +00:00
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
2014-03-18 14:08:16 +00:00
} else {
2016-03-31 22:42:28 +00:00
return json_decode($process->getOutput());
2014-03-18 14:08:16 +00:00
}
2014-03-13 19:07:56 +00:00
}
/**
* Get URL of video from URL of page
*
2014-03-13 19:07:56 +00:00
* @param string $url URL of page
* @param string $format Format to use for the video
*
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
{
$this->procBuilder->setArguments(
array(
'--get-url',
$url
)
2016-02-28 22:04:53 +00:00
);
2014-03-13 19:07:56 +00:00
if (isset($format)) {
$this->procBuilder->add('-f '.$format);
2014-03-13 19:07:56 +00:00
}
$process = $this->procBuilder->getProcess();
2016-03-31 22:42:28 +00:00
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
2014-03-13 19:07:56 +00:00
} else {
2016-04-08 18:08:04 +00:00
return $process->getOutput();
2014-03-13 19:07:56 +00:00
}
}
public function getFilename($url, $format = null)
{
$this->procBuilder->setArguments(
array(
'--get-filename',
$url
)
);
if (isset($format)) {
$this->procBuilder->add('-f '.$format);
}
$process = $this->procBuilder->getProcess();
$process->run();
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
} else {
2016-07-29 22:47:46 +00:00
return trim($process->getOutput());
}
}
public function getAudioFilename($url, $format = null)
{
return html_entity_decode(
pathinfo(
$this->getFilename($url, $format),
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
);
}
2016-07-29 22:47:46 +00:00
public function getAudioStream($url, $format)
{
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(
array(
$this->config->avconv,
'-v', 'quiet',
'-i', '-',
'-f', 'mp3',
'-vn',
'pipe:1'
)
);
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);
}
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);
}
$chain = new Chain($builder->getProcess());
$chain->add('|', $avconvProc);
} else {
2016-07-30 12:01:00 +00:00
if (!shell_exec('which '.$this->config->curl)) {
throw(new \Exception('Can\'t find curl'));
}
$chain = new Chain(
ProcessBuilder::create(
array_merge(
array(
2016-07-30 12:01:00 +00:00
$this->config->curl,
'--silent',
'--location',
'--user-agent', $video->http_headers->{'User-Agent'},
$video->url
),
$this->config->curl_params
)
)
);
$chain->add('|', $avconvProc);
}
return popen($chain->getProcess()->getCommandLine(), 'r');
}
2014-03-13 19:07:56 +00:00
}