2014-03-13 19:07:56 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload class.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-12-05 12:12:27 +00:00
|
|
|
|
2015-10-29 19:43:43 +00:00
|
|
|
namespace Alltube;
|
2016-03-29 23:49:08 +00:00
|
|
|
|
2019-04-22 19:06:05 +00:00
|
|
|
use Alltube\Exception\EmptyUrlException;
|
|
|
|
use Alltube\Exception\PasswordException;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Exception;
|
2019-04-21 16:30:02 +00:00
|
|
|
use GuzzleHttp\Client;
|
2020-05-13 19:39:38 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-02-05 15:48:58 +00:00
|
|
|
use stdClass;
|
2017-12-23 13:17:29 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
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.
|
2019-04-21 16:30:02 +00:00
|
|
|
*
|
2019-04-21 17:10:37 +00:00
|
|
|
* Due to the way youtube-dl behaves, this class can also contain information about a playlist.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @property-read string $title Title
|
|
|
|
* @property-read string $protocol Network protocol (HTTP, RTMP, etc.)
|
|
|
|
* @property-read string $url File URL
|
|
|
|
* @property-read string $ext File extension
|
|
|
|
* @property-read string $extractor_key youtube-dl extractor class used
|
|
|
|
* @property-read array $entries List of videos (if the object contains information about a playlist)
|
|
|
|
* @property-read array $rtmp_conn
|
2019-04-21 17:47:30 +00:00
|
|
|
* @property-read string|null $_type Object type (usually "playlist" or null)
|
2020-05-13 20:28:05 +00:00
|
|
|
* @property-read stdClass $downloader_options
|
|
|
|
* @property-read stdClass $http_headers
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
class Video
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-10-14 00:40:33 +00:00
|
|
|
/**
|
2016-10-14 00:40:57 +00:00
|
|
|
* Config instance.
|
|
|
|
*
|
2016-10-14 00:40:33 +00:00
|
|
|
* @var Config
|
|
|
|
*/
|
2016-10-10 19:32:07 +00:00
|
|
|
private $config;
|
2016-10-14 00:40:33 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
/**
|
2019-04-21 16:35:24 +00:00
|
|
|
* URL of the page containing the video.
|
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $webpageUrl;
|
|
|
|
|
|
|
|
/**
|
2019-04-21 16:35:24 +00:00
|
|
|
* Requested video format.
|
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $requestedFormat;
|
|
|
|
|
|
|
|
/**
|
2019-04-21 16:35:24 +00:00
|
|
|
* Password.
|
|
|
|
*
|
2019-04-21 16:49:05 +00:00
|
|
|
* @var string|null
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
|
|
|
private $password;
|
|
|
|
|
2019-04-21 17:10:37 +00:00
|
|
|
/**
|
|
|
|
* JSON object returned by youtube-dl.
|
|
|
|
*
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
|
|
|
private $json;
|
|
|
|
|
2019-04-22 18:20:04 +00:00
|
|
|
/**
|
|
|
|
* URLs of the video files.
|
2019-04-22 19:06:05 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @var string[]
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
|
|
|
private $urls;
|
|
|
|
|
2019-11-27 22:15:49 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager instance.
|
|
|
|
*
|
|
|
|
* @var LocaleManager
|
|
|
|
*/
|
|
|
|
protected $localeManager;
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload constructor.
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string $webpageUrl URL of the page containing the video
|
2019-04-21 16:30:02 +00:00
|
|
|
* @param string $requestedFormat Requested video format
|
2020-03-19 09:01:55 +00:00
|
|
|
* (can be any format string accepted by youtube-dl,
|
|
|
|
* including selectors like "[height<=720]")
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string $password Password
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function __construct($webpageUrl, $requestedFormat = 'best', $password = null)
|
2016-04-08 17:06:41 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->webpageUrl = $webpageUrl;
|
|
|
|
$this->requestedFormat = $requestedFormat;
|
|
|
|
$this->password = $password;
|
|
|
|
$this->config = Config::getInstance();
|
2019-11-27 22:15:49 +00:00
|
|
|
|
|
|
|
$this->localeManager = LocaleManager::getInstance();
|
2017-12-23 13:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-23 13:37:29 +00:00
|
|
|
* Return a youtube-dl process with the specified arguments.
|
2017-12-23 13:17:29 +00:00
|
|
|
*
|
|
|
|
* @param string[] $arguments Arguments
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return Process<string>
|
2017-12-23 13:17:29 +00:00
|
|
|
*/
|
2019-04-21 16:49:05 +00:00
|
|
|
private static function getProcess(array $arguments)
|
2017-12-23 13:17:29 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$config = Config::getInstance();
|
2019-04-21 16:35:24 +00:00
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
return new Process(
|
2016-04-08 17:37:59 +00:00
|
|
|
array_merge(
|
2019-04-21 16:30:02 +00:00
|
|
|
[$config->python, $config->youtubedl],
|
|
|
|
$config->params,
|
2017-12-23 13:17:29 +00:00
|
|
|
$arguments
|
2016-04-08 17:37:59 +00:00
|
|
|
)
|
|
|
|
);
|
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
|
2020-05-13 19:18:32 +00:00
|
|
|
*
|
|
|
|
* @throws PasswordException
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public static function getExtractors()
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
$video = new self('');
|
|
|
|
|
|
|
|
return explode("\n", trim($video->callYoutubedl(['--list-extractors'])));
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2015-09-04 20:45:55 +00:00
|
|
|
|
2016-10-14 00:40:33 +00:00
|
|
|
/**
|
2019-04-21 16:49:05 +00:00
|
|
|
* Call youtube-dl.
|
2016-10-14 00:40:57 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string[] $arguments Arguments
|
2016-10-14 00:40:57 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return string Result
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the password is wrong
|
|
|
|
* @throws Exception If youtube-dl returns an error
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @throws PasswordException If the video is protected by a password and no password was specified
|
2016-10-14 00:40:33 +00:00
|
|
|
*/
|
2019-11-27 22:15:49 +00:00
|
|
|
private function callYoutubedl(array $arguments)
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
$process = self::getProcess($arguments);
|
2017-11-01 22:02:19 +00:00
|
|
|
//This is needed by the openload extractor because it runs PhantomJS
|
2019-10-03 19:24:12 +00:00
|
|
|
$process->setEnv(['PATH' => $config->phantomjsDir]);
|
2016-03-31 22:42:28 +00:00
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
2016-10-20 21:01:31 +00:00
|
|
|
$errorOutput = trim($process->getErrorOutput());
|
2020-05-13 20:28:05 +00:00
|
|
|
$exitCode = intval($process->getExitCode());
|
2016-10-20 21:01:31 +00:00
|
|
|
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
|
2018-02-06 18:20:56 +00:00
|
|
|
throw new PasswordException($errorOutput, $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Wrong password'), $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
} else {
|
2018-02-06 18:20:56 +00:00
|
|
|
throw new Exception($errorOutput, $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
}
|
2014-03-18 14:08:16 +00:00
|
|
|
} else {
|
2017-04-24 17:16:38 +00:00
|
|
|
return trim($process->getOutput());
|
2014-03-18 14:08:16 +00:00
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:49:05 +00:00
|
|
|
/**
|
|
|
|
* Get a property from youtube-dl.
|
|
|
|
*
|
|
|
|
* @param string $prop Property
|
|
|
|
*
|
|
|
|
* @return string
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2019-04-21 16:49:05 +00:00
|
|
|
*/
|
|
|
|
private function getProp($prop = 'dump-json')
|
|
|
|
{
|
2019-10-03 19:24:12 +00:00
|
|
|
$arguments = ['--' . $prop];
|
2019-04-21 16:49:05 +00:00
|
|
|
|
|
|
|
if (isset($this->webpageUrl)) {
|
|
|
|
$arguments[] = $this->webpageUrl;
|
|
|
|
}
|
|
|
|
if (isset($this->requestedFormat)) {
|
|
|
|
$arguments[] = '-f';
|
|
|
|
$arguments[] = $this->requestedFormat;
|
|
|
|
}
|
|
|
|
if (isset($this->password)) {
|
|
|
|
$arguments[] = '--video-password';
|
|
|
|
$arguments[] = $this->password;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:15:49 +00:00
|
|
|
return $this->callYoutubedl($arguments);
|
2019-04-21 16:49:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:40:19 +00:00
|
|
|
/**
|
|
|
|
* Get all information about a video.
|
|
|
|
*
|
2019-03-30 17:21:45 +00:00
|
|
|
* @return stdClass Decoded JSON
|
2020-05-13 19:18:32 +00:00
|
|
|
*
|
|
|
|
* @throws PasswordException
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getJson()
|
|
|
|
{
|
|
|
|
if (!isset($this->json)) {
|
|
|
|
$this->json = json_decode($this->getProp('dump-single-json'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->json;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to get a property from the JSON object returned by youtube-dl.
|
|
|
|
*
|
|
|
|
* @param string $name Property
|
|
|
|
*
|
|
|
|
* @return mixed
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
|
|
|
if (isset($this->$name)) {
|
|
|
|
return $this->getJson()->$name;
|
|
|
|
}
|
2020-05-13 19:18:32 +00:00
|
|
|
|
|
|
|
return null;
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to check if the JSON object returned by youtube-dl has a property.
|
|
|
|
*
|
|
|
|
* @param string $name Property
|
|
|
|
*
|
2019-04-21 16:35:24 +00:00
|
|
|
* @return bool
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
|
|
|
public function __isset($name)
|
2016-10-13 14:40:19 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return isset($this->getJson()->$name);
|
2016-10-13 14:40:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* It generally returns only one URL.
|
|
|
|
* But it can return two URLs when multiple formats are specified
|
|
|
|
* (eg. bestvideo+bestaudio).
|
|
|
|
*
|
2017-04-25 12:55:21 +00:00
|
|
|
* @return string[] URLs of video
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws EmptyUrlException
|
|
|
|
* @throws PasswordException
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getUrl()
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2019-04-22 18:20:04 +00:00
|
|
|
// Cache the URLs.
|
|
|
|
if (!isset($this->urls)) {
|
|
|
|
$this->urls = explode("\n", $this->getProp('get-url'));
|
2018-05-23 19:50:06 +00:00
|
|
|
|
2019-04-22 19:07:36 +00:00
|
|
|
if (empty($this->urls[0])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new EmptyUrlException($this->localeManager->t('youtube-dl returned an empty URL.'));
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
2018-05-23 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:20:04 +00:00
|
|
|
return $this->urls;
|
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
|
|
|
*
|
|
|
|
* @return string Filename of extracted video
|
2020-05-13 19:18:32 +00:00
|
|
|
*
|
|
|
|
* @throws PasswordException
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getFilename()
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return trim($this->getProp('get-filename'));
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2017-04-24 22:41:49 +00:00
|
|
|
* Get filename of video with the specified extension.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* @param string $extension New file extension
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* @return string Filename of extracted video with specified extension
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2017-04-24 22:40:24 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getFileNameWithExtension($extension)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
2020-04-09 18:46:14 +00:00
|
|
|
return str_replace('.' . $this->ext, '.' . $extension, $this->getFilename());
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 14:18:04 +00:00
|
|
|
/**
|
2017-12-23 13:17:29 +00:00
|
|
|
* Return arguments used to run rtmp for a specific video.
|
2016-10-15 14:18:04 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return string[] Arguments
|
2016-10-15 14:18:04 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getRtmpArguments()
|
2016-10-15 14:18:04 +00:00
|
|
|
{
|
2017-12-23 15:04:55 +00:00
|
|
|
$arguments = [];
|
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
if ($this->protocol == 'rtmp') {
|
2019-10-03 19:24:12 +00:00
|
|
|
foreach (
|
|
|
|
[
|
2020-05-13 20:28:05 +00:00
|
|
|
'url' => '-rtmp_tcurl',
|
|
|
|
'webpage_url' => '-rtmp_pageurl',
|
|
|
|
'player_url' => '-rtmp_swfverify',
|
|
|
|
'flash_version' => '-rtmp_flashver',
|
|
|
|
'play_path' => '-rtmp_playpath',
|
|
|
|
'app' => '-rtmp_app',
|
2019-10-03 19:24:12 +00:00
|
|
|
] as $property => $option
|
|
|
|
) {
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->{$property})) {
|
2018-07-03 18:09:45 +00:00
|
|
|
$arguments[] = $option;
|
2019-04-21 16:30:02 +00:00
|
|
|
$arguments[] = $this->{$property};
|
2018-07-03 18:09:45 +00:00
|
|
|
}
|
2016-10-15 14:18:04 +00:00
|
|
|
}
|
2016-10-15 14:20:54 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->rtmp_conn)) {
|
|
|
|
foreach ($this->rtmp_conn as $conn) {
|
2018-07-03 18:09:45 +00:00
|
|
|
$arguments[] = '-rtmp_conn';
|
|
|
|
$arguments[] = $conn;
|
|
|
|
}
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 17:02:14 +00:00
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
return $arguments;
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:52:59 +00:00
|
|
|
/**
|
2017-05-13 22:54:47 +00:00
|
|
|
* Check if a command runs successfully.
|
2017-05-13 22:52:59 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string[] $command Command and arguments
|
2017-05-13 22:52:59 +00:00
|
|
|
*
|
|
|
|
* @return bool False if the command returns an error, true otherwise
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public static function checkCommand(array $command)
|
2017-05-13 22:52:59 +00:00
|
|
|
{
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process($command);
|
2017-05-13 22:52:59 +00:00
|
|
|
$process->run();
|
|
|
|
|
|
|
|
return $process->isSuccessful();
|
|
|
|
}
|
|
|
|
|
2016-10-14 17:01:51 +00:00
|
|
|
/**
|
2018-01-24 22:30:24 +00:00
|
|
|
* Get a process that runs avconv in order to convert a video.
|
2016-10-14 17:02:14 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param int $audioBitrate Audio bitrate of the converted file
|
|
|
|
* @param string $filetype Filetype of the converted file
|
|
|
|
* @param bool $audioOnly True to return an audio-only file
|
|
|
|
* @param string $from Start the conversion at this time
|
|
|
|
* @param string $to End the conversion at this time
|
2016-10-14 17:02:14 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return Process<string> Process
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If avconv/ffmpeg is missing
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2016-10-14 17:01:51 +00:00
|
|
|
*/
|
2018-07-03 17:47:35 +00:00
|
|
|
private function getAvconvProcess(
|
|
|
|
$audioBitrate,
|
|
|
|
$filetype = 'mp3',
|
|
|
|
$audioOnly = true,
|
|
|
|
$from = null,
|
|
|
|
$to = null
|
|
|
|
) {
|
2017-05-13 22:52:59 +00:00
|
|
|
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception(
|
|
|
|
$this->localeManager->t(
|
2019-11-29 20:33:49 +00:00
|
|
|
"Can't find avconv or ffmpeg at @path.",
|
|
|
|
['@path' => $this->config->avconv]
|
|
|
|
)
|
2019-11-27 22:15:49 +00:00
|
|
|
);
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
2016-10-14 17:02:14 +00:00
|
|
|
|
2018-07-03 17:47:35 +00:00
|
|
|
$durationRegex = '/(\d+:)?(\d+:)?(\d+)/';
|
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
$afterArguments = [];
|
2017-12-23 14:14:43 +00:00
|
|
|
|
2018-01-24 22:30:24 +00:00
|
|
|
if ($audioOnly) {
|
2018-07-03 18:09:45 +00:00
|
|
|
$afterArguments[] = '-vn';
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
if (!empty($from)) {
|
2018-07-03 17:47:35 +00:00
|
|
|
if (!preg_match($durationRegex, $from)) {
|
2019-11-29 20:33:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Invalid start time: @from.', ['@from' => $from]));
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
|
|
|
$afterArguments[] = '-ss';
|
|
|
|
$afterArguments[] = $from;
|
|
|
|
}
|
2018-07-03 18:09:45 +00:00
|
|
|
if (!empty($to)) {
|
2018-07-03 17:47:35 +00:00
|
|
|
if (!preg_match($durationRegex, $to)) {
|
2019-11-29 20:33:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Invalid end time: @to.', ['@to' => $to]));
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
|
|
|
$afterArguments[] = '-to';
|
|
|
|
$afterArguments[] = $to;
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 20:40:54 +00:00
|
|
|
$urls = $this->getUrl();
|
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
$arguments = array_merge(
|
|
|
|
[
|
|
|
|
$this->config->avconv,
|
|
|
|
'-v', $this->config->avconvVerbosity,
|
|
|
|
],
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->getRtmpArguments(),
|
2017-12-23 14:14:43 +00:00
|
|
|
[
|
2019-04-21 20:40:54 +00:00
|
|
|
'-i', $urls[0],
|
2018-01-24 22:30:24 +00:00
|
|
|
'-f', $filetype,
|
2019-10-03 19:24:12 +00:00
|
|
|
'-b:a', $audioBitrate . 'k',
|
2018-01-24 22:30:24 +00:00
|
|
|
],
|
2018-07-03 17:47:35 +00:00
|
|
|
$afterArguments,
|
2018-01-24 22:30:24 +00:00
|
|
|
[
|
2017-12-23 14:14:43 +00:00
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
2019-04-21 19:36:14 +00:00
|
|
|
|
|
|
|
//Vimeo needs a correct user-agent
|
|
|
|
$arguments[] = '-user_agent';
|
|
|
|
$arguments[] = $this->getProp('dump-user-agent');
|
2017-12-09 22:56:34 +00:00
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
return new Process($arguments);
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2019-04-21 16:35:24 +00:00
|
|
|
* @param string $from Start the conversion at this time
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string $to End the conversion at this time
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return resource popen stream
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @throws Exception If your try to convert an M3U8 video
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getAudioStream($from = null, $to = null)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->_type) && $this->_type == 'playlist') {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Conversion of playlists is not supported.'));
|
2018-05-23 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->protocol)) {
|
|
|
|
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
|
2019-04-21 16:30:02 +00:00
|
|
|
} elseif ($this->protocol == 'http_dash_segments') {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Conversion of DASH segments is not supported.'));
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
2016-12-26 23:01:42 +00:00
|
|
|
}
|
2016-07-29 22:15:17 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
$avconvProc = $this->getAvconvProcess($this->config->audioBitrate, 'mp3', true, $from, $to);
|
2017-04-24 17:16:38 +00:00
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
$stream = popen($avconvProc->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
|
|
|
|
if (!is_resource($stream)) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
2016-12-26 14:50:26 +00:00
|
|
|
|
2016-12-26 14:58:07 +00:00
|
|
|
/**
|
|
|
|
* Get video stream from an M3U playlist.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return resource popen stream
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @throws Exception If avconv/ffmpeg is missing
|
2016-12-26 14:58:07 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getM3uStream()
|
2016-12-26 14:50:26 +00:00
|
|
|
{
|
2017-05-13 22:52:59 +00:00
|
|
|
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception(
|
|
|
|
$this->localeManager->t(
|
2019-11-29 20:33:49 +00:00
|
|
|
"Can't find avconv or ffmpeg at @path.",
|
|
|
|
['@path' => $this->config->avconv]
|
|
|
|
)
|
2019-11-27 22:15:49 +00:00
|
|
|
);
|
2016-12-26 14:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 20:40:54 +00:00
|
|
|
$urls = $this->getUrl();
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process(
|
2016-12-26 14:50:26 +00:00
|
|
|
[
|
|
|
|
$this->config->avconv,
|
2017-12-23 14:14:43 +00:00
|
|
|
'-v', $this->config->avconvVerbosity,
|
2019-04-21 20:40:54 +00:00
|
|
|
'-i', $urls[0],
|
2019-04-21 16:30:02 +00:00
|
|
|
'-f', $this->ext,
|
2016-12-26 14:50:26 +00:00
|
|
|
'-c', 'copy',
|
|
|
|
'-bsf:a', 'aac_adtstoasc',
|
|
|
|
'-movflags', 'frag_keyframe+empty_moov',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2016-12-26 14:50:26 +00:00
|
|
|
}
|
2017-04-24 16:31:14 +00:00
|
|
|
|
2017-04-24 22:40:24 +00:00
|
|
|
/**
|
|
|
|
* Get an avconv stream to remux audio and video.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return resource popen stream
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getRemuxStream()
|
2017-04-24 22:40:24 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$urls = $this->getUrl();
|
|
|
|
|
|
|
|
if (!isset($urls[0]) || !isset($urls[1])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('This video does not have two URLs.'));
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process(
|
2017-04-24 22:40:24 +00:00
|
|
|
[
|
|
|
|
$this->config->avconv,
|
2017-12-23 14:14:43 +00:00
|
|
|
'-v', $this->config->avconvVerbosity,
|
2017-04-24 22:40:24 +00:00
|
|
|
'-i', $urls[0],
|
|
|
|
'-i', $urls[1],
|
|
|
|
'-c', 'copy',
|
2020-01-23 23:34:40 +00:00
|
|
|
'-map', '0:v:0',
|
2017-04-24 22:40:24 +00:00
|
|
|
'-map', '1:a:0',
|
|
|
|
'-f', 'matroska',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
2017-04-24 22:41:49 +00:00
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2017-04-24 22:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:31:14 +00:00
|
|
|
/**
|
|
|
|
* Get video stream from an RTMP video.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return resource popen stream
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-04-24 16:31:14 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getRtmpStream()
|
2017-04-24 16:31:14 +00:00
|
|
|
{
|
2019-04-21 20:40:54 +00:00
|
|
|
$urls = $this->getUrl();
|
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
$process = new Process(
|
|
|
|
array_merge(
|
|
|
|
[
|
|
|
|
$this->config->avconv,
|
|
|
|
'-v', $this->config->avconvVerbosity,
|
|
|
|
],
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->getRtmpArguments(),
|
2017-12-23 14:14:43 +00:00
|
|
|
[
|
2019-04-21 20:40:54 +00:00
|
|
|
'-i', $urls[0],
|
2019-04-21 16:30:02 +00:00
|
|
|
'-f', $this->ext,
|
2017-12-23 14:14:43 +00:00
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2017-04-24 16:31:14 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
|
2018-01-24 22:30:24 +00:00
|
|
|
/**
|
|
|
|
* Get the stream of a converted video.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param int $audioBitrate Audio bitrate of the converted file
|
|
|
|
* @param string $filetype Filetype of the converted file
|
2018-01-24 22:30:24 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return resource popen stream
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2018-01-24 22:30:24 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @throws Exception If your try to convert and M3U8 video
|
2018-01-24 22:30:24 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function getConvertedStream($audioBitrate, $filetype)
|
2018-01-24 22:30:24 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
$avconvProc = $this->getAvconvProcess($audioBitrate, $filetype, false);
|
2018-01-24 22:30:24 +00:00
|
|
|
|
|
|
|
$stream = popen($avconvProc->getCommandLine(), 'r');
|
|
|
|
|
|
|
|
if (!is_resource($stream)) {
|
2019-11-27 22:15:49 +00:00
|
|
|
throw new Exception($this->localeManager->t('Could not open popen stream.'));
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
2019-04-21 16:30:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the same video but with another format.
|
|
|
|
*
|
|
|
|
* @param string $format New format
|
|
|
|
*
|
|
|
|
* @return Video
|
|
|
|
*/
|
|
|
|
public function withFormat($format)
|
|
|
|
{
|
2019-04-21 16:35:24 +00:00
|
|
|
return new self($this->webpageUrl, $format, $this->password);
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a HTTP response containing the video.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $headers HTTP headers of the request
|
2019-04-21 22:05:53 +00:00
|
|
|
*
|
2020-05-13 19:39:38 +00:00
|
|
|
* @return ResponseInterface
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws EmptyUrlException
|
|
|
|
* @throws PasswordException
|
2020-05-14 09:55:30 +00:00
|
|
|
* @link https://github.com/guzzle/guzzle/issues/2640
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
2019-04-21 20:38:27 +00:00
|
|
|
public function getHttpResponse(array $headers = [])
|
2019-04-21 16:30:02 +00:00
|
|
|
{
|
2020-05-14 09:55:30 +00:00
|
|
|
// IDN conversion breaks with Google hosts like https://r3---sn-25glene6.googlevideo.com/.
|
|
|
|
$client = new Client(['idn_conversion' => false]);
|
2019-04-21 16:30:02 +00:00
|
|
|
$urls = $this->getUrl();
|
2020-05-14 08:59:59 +00:00
|
|
|
$stream_context_options = [];
|
|
|
|
|
|
|
|
if (array_key_exists('Referer', (array)$this->http_headers)) {
|
|
|
|
$stream_context_options = [
|
|
|
|
'http' => [
|
|
|
|
'header' => 'Referer: ' . $this->http_headers->Referer
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2019-04-21 16:30:02 +00:00
|
|
|
|
2019-10-16 21:00:05 +00:00
|
|
|
return $client->request(
|
|
|
|
'GET',
|
|
|
|
$urls[0],
|
|
|
|
[
|
|
|
|
'stream' => true,
|
2020-05-14 08:59:59 +00:00
|
|
|
'stream_context' => $stream_context_options,
|
2020-05-13 20:28:05 +00:00
|
|
|
'headers' => array_merge((array)$this->http_headers, $headers)
|
2019-10-16 21:00:05 +00:00
|
|
|
]
|
|
|
|
);
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|