2020-06-20 22:46:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Alltube\Library;
|
|
|
|
|
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
|
|
|
use Alltube\Library\Exception\AvconvException;
|
2020-10-17 22:47:16 +02:00
|
|
|
use Alltube\Library\Exception\EmptyUrlException;
|
2020-06-20 22:46:28 +02:00
|
|
|
use Alltube\Library\Exception\InvalidProtocolConversionException;
|
|
|
|
use Alltube\Library\Exception\InvalidTimeException;
|
|
|
|
use Alltube\Library\Exception\PasswordException;
|
|
|
|
use Alltube\Library\Exception\PlaylistConversionException;
|
|
|
|
use Alltube\Library\Exception\PopenStreamException;
|
|
|
|
use Alltube\Library\Exception\RemuxException;
|
|
|
|
use Alltube\Library\Exception\WrongPasswordException;
|
|
|
|
use Alltube\Library\Exception\YoutubedlException;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2021-02-08 22:03:09 +01:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
use Psr\Log\LoggerAwareTrait;
|
2020-07-15 22:22:12 +02:00
|
|
|
use Psr\Log\NullLogger;
|
2020-06-20 22:46:28 +02:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
2020-06-20 22:49:12 +02:00
|
|
|
/**
|
|
|
|
* Class used to call youtube-dl and download videos.
|
|
|
|
*/
|
2021-02-08 22:03:09 +01:00
|
|
|
class Downloader implements LoggerAwareInterface
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
2021-02-08 22:03:09 +01:00
|
|
|
use LoggerAwareTrait;
|
2020-06-20 22:46:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* youtube-dl binary path.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $youtubedl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* python binary path.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $python;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* avconv or ffmpeg binary path.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $avconv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* avconv/ffmpeg logging level.
|
|
|
|
* Must be one of these: quiet, panic, fatal, error, warning, info, verbose, debug.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $avconvVerbosity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to the directory that contains the phantomjs binary.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $phantomjsDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* youtube-dl parameters.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $params;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloader constructor.
|
|
|
|
* @param string $youtubedl youtube-dl binary path
|
|
|
|
* @param string[] $params youtube-dl parameters
|
|
|
|
* @param string $python python binary path
|
|
|
|
* @param string $avconv avconv or ffmpeg binary path
|
|
|
|
* @param string $phantomjsDir Path to the directory that contains the phantomjs binary
|
|
|
|
* @param string $avconvVerbosity avconv/ffmpeg logging level
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$youtubedl = '/usr/bin/youtube-dl',
|
|
|
|
array $params = ['--no-warnings'],
|
|
|
|
$python = '/usr/bin/python3',
|
|
|
|
$avconv = '/usr/bin/ffmpeg',
|
|
|
|
$phantomjsDir = '/usr/bin/',
|
|
|
|
$avconvVerbosity = 'error'
|
|
|
|
) {
|
|
|
|
$this->youtubedl = $youtubedl;
|
|
|
|
$this->params = $params;
|
|
|
|
$this->python = $python;
|
|
|
|
$this->avconv = $avconv;
|
|
|
|
$this->phantomjsDir = $phantomjsDir;
|
|
|
|
$this->avconvVerbosity = $avconvVerbosity;
|
2020-07-15 22:22:12 +02:00
|
|
|
|
|
|
|
$this->logger = new NullLogger();
|
|
|
|
}
|
|
|
|
|
2020-06-20 22:46:28 +02:00
|
|
|
/**
|
|
|
|
* @param string $webpageUrl URL of the page containing the video
|
|
|
|
* @param string $requestedFormat Requested video format
|
2020-10-17 22:47:16 +02:00
|
|
|
* @param string|null $password Password
|
2020-06-20 22:46:28 +02:00
|
|
|
* @return Video
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
public function getVideo(string $webpageUrl, $requestedFormat = 'best/bestvideo', string $password = null): Video
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
|
|
|
return new Video($this, $webpageUrl, $requestedFormat, $password);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a youtube-dl process with the specified arguments.
|
|
|
|
*
|
|
|
|
* @param string[] $arguments Arguments
|
|
|
|
*
|
|
|
|
* @return Process<string>
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
private function getProcess(array $arguments): Process
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
|
|
|
return new Process(
|
|
|
|
array_merge(
|
|
|
|
[$this->python, $this->youtubedl],
|
|
|
|
$this->params,
|
|
|
|
$arguments
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a command runs successfully.
|
|
|
|
*
|
|
|
|
* @param string[] $command Command and arguments
|
|
|
|
*
|
|
|
|
* @return bool False if the command returns an error, true otherwise
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
public static function checkCommand(array $command): bool
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
|
|
|
$process = new Process($command);
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
return $process->isSuccessful();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a process that runs avconv in order to convert a video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @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
|
2020-10-17 22:47:16 +02:00
|
|
|
* @param string|null $from Start the conversion at this time
|
|
|
|
* @param string|null $to End the conversion at this time
|
2020-06-20 22:46:28 +02:00
|
|
|
*
|
|
|
|
* @return Process<string> Process
|
|
|
|
* @throws AvconvException If avconv/ffmpeg is missing
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws EmptyUrlException
|
2020-06-20 22:46:28 +02:00
|
|
|
* @throws InvalidTimeException
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws PasswordException
|
|
|
|
* @throws WrongPasswordException
|
|
|
|
* @throws YoutubedlException
|
2020-06-20 22:46:28 +02:00
|
|
|
*/
|
|
|
|
private function getAvconvProcess(
|
|
|
|
Video $video,
|
2020-10-17 22:47:16 +02:00
|
|
|
int $audioBitrate,
|
2020-06-20 22:46:28 +02:00
|
|
|
$filetype = 'mp3',
|
|
|
|
$audioOnly = true,
|
2020-10-17 22:47:16 +02:00
|
|
|
string $from = null,
|
|
|
|
string $to = null
|
2020-12-17 22:45:50 +01:00
|
|
|
): Process {
|
2020-06-20 22:46:28 +02:00
|
|
|
if (!$this->checkCommand([$this->avconv, '-version'])) {
|
|
|
|
throw new AvconvException($this->avconv);
|
|
|
|
}
|
|
|
|
|
|
|
|
$durationRegex = '/(\d+:)?(\d+:)?(\d+)/';
|
|
|
|
|
|
|
|
$afterArguments = [];
|
|
|
|
|
|
|
|
if ($audioOnly) {
|
|
|
|
$afterArguments[] = '-vn';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($from)) {
|
|
|
|
if (!preg_match($durationRegex, $from)) {
|
|
|
|
throw new InvalidTimeException($from);
|
|
|
|
}
|
|
|
|
$afterArguments[] = '-ss';
|
|
|
|
$afterArguments[] = $from;
|
|
|
|
}
|
|
|
|
if (!empty($to)) {
|
|
|
|
if (!preg_match($durationRegex, $to)) {
|
|
|
|
throw new InvalidTimeException($to);
|
|
|
|
}
|
|
|
|
$afterArguments[] = '-to';
|
|
|
|
$afterArguments[] = $to;
|
|
|
|
}
|
|
|
|
|
|
|
|
$urls = $video->getUrl();
|
|
|
|
|
|
|
|
$arguments = array_merge(
|
|
|
|
[
|
|
|
|
$this->avconv,
|
|
|
|
'-v', $this->avconvVerbosity,
|
|
|
|
],
|
|
|
|
$video->getRtmpArguments(),
|
|
|
|
[
|
|
|
|
'-i', $urls[0],
|
|
|
|
'-f', $filetype,
|
|
|
|
'-b:a', $audioBitrate . 'k',
|
|
|
|
],
|
|
|
|
$afterArguments,
|
|
|
|
[
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
//Vimeo needs a correct user-agent
|
|
|
|
$arguments[] = '-user_agent';
|
|
|
|
$arguments[] = $video->getProp('dump-user-agent');
|
|
|
|
|
2020-07-15 22:22:12 +02:00
|
|
|
$process = new Process($arguments);
|
|
|
|
$this->logger->debug($process->getCommandLine());
|
|
|
|
|
|
|
|
return $process;
|
2020-06-20 22:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call youtube-dl.
|
|
|
|
*
|
|
|
|
* @param string[] $arguments Arguments
|
|
|
|
*
|
|
|
|
* @return string Result
|
|
|
|
* @throws WrongPasswordException If the password is wrong
|
|
|
|
* @throws YoutubedlException If youtube-dl returns an error
|
|
|
|
* @throws PasswordException If the video is protected by a password and no password was specified
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
public function callYoutubedl(array $arguments): string
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
|
|
|
$process = $this->getProcess($arguments);
|
|
|
|
//This is needed by the openload extractor because it runs PhantomJS
|
|
|
|
$process->setEnv(['PATH' => $this->phantomjsDir]);
|
2020-07-15 22:22:12 +02:00
|
|
|
$this->logger->debug($process->getCommandLine());
|
2020-06-20 22:46:28 +02:00
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
|
|
|
$errorOutput = trim($process->getErrorOutput());
|
|
|
|
$exitCode = intval($process->getExitCode());
|
|
|
|
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
|
|
|
|
throw new PasswordException($errorOutput, $exitCode);
|
|
|
|
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
|
|
|
|
throw new WrongPasswordException($errorOutput, $exitCode);
|
|
|
|
} else {
|
2020-06-21 13:05:33 +02:00
|
|
|
throw new YoutubedlException($process);
|
2020-06-20 22:46:28 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return trim($process->getOutput());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get video stream from an M3U playlist.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @return resource popen stream
|
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
* @throws AvconvException If avconv/ffmpeg is missing
|
|
|
|
* @throws PopenStreamException If the popen stream was not created correctly
|
|
|
|
*/
|
|
|
|
public function getM3uStream(Video $video)
|
|
|
|
{
|
|
|
|
if (!$this->checkCommand([$this->avconv, '-version'])) {
|
|
|
|
throw new AvconvException($this->avconv);
|
|
|
|
}
|
|
|
|
|
|
|
|
$urls = $video->getUrl();
|
|
|
|
|
|
|
|
$process = new Process(
|
|
|
|
[
|
|
|
|
$this->avconv,
|
|
|
|
'-v', $this->avconvVerbosity,
|
|
|
|
'-i', $urls[0],
|
|
|
|
'-f', $video->ext,
|
|
|
|
'-c', 'copy',
|
|
|
|
'-bsf:a', 'aac_adtstoasc',
|
|
|
|
'-movflags', 'frag_keyframe+empty_moov',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
|
|
|
if (!is_resource($stream)) {
|
|
|
|
throw new PopenStreamException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get audio stream of converted video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @param int $audioBitrate MP3 bitrate when converting (in kbit/s)
|
2020-10-17 22:47:16 +02:00
|
|
|
* @param string|null $from Start the conversion at this time
|
|
|
|
* @param string|null $to End the conversion at this time
|
2020-06-20 22:46:28 +02:00
|
|
|
*
|
|
|
|
* @return resource popen stream
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws AvconvException
|
|
|
|
* @throws EmptyUrlException
|
|
|
|
* @throws InvalidProtocolConversionException
|
|
|
|
* @throws InvalidTimeException
|
|
|
|
* @throws PasswordException
|
|
|
|
* @throws PlaylistConversionException
|
|
|
|
* @throws PopenStreamException
|
|
|
|
* @throws RemuxException
|
|
|
|
* @throws WrongPasswordException
|
|
|
|
* @throws YoutubedlException
|
2020-06-20 22:46:28 +02:00
|
|
|
*/
|
2020-10-17 22:47:16 +02:00
|
|
|
public function getAudioStream(Video $video, $audioBitrate = 128, string $from = null, string $to = null)
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
2020-06-21 12:03:22 +02:00
|
|
|
return $this->getConvertedStream($video, $audioBitrate, 'mp3', true, $from, $to);
|
2020-06-20 22:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an avconv stream to remux audio and video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @return resource popen stream
|
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
* @throws PopenStreamException If the popen stream was not created correctly
|
|
|
|
* @throws RemuxException If the video does not have two URLs
|
|
|
|
*/
|
|
|
|
public function getRemuxStream(Video $video)
|
|
|
|
{
|
|
|
|
$urls = $video->getUrl();
|
|
|
|
|
|
|
|
if (!isset($urls[0]) || !isset($urls[1])) {
|
|
|
|
throw new RemuxException('This video does not have two URLs.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$process = new Process(
|
|
|
|
[
|
|
|
|
$this->avconv,
|
|
|
|
'-v', $this->avconvVerbosity,
|
|
|
|
'-i', $urls[0],
|
|
|
|
'-i', $urls[1],
|
|
|
|
'-c', 'copy',
|
|
|
|
'-map', '0:v:0',
|
|
|
|
'-map', '1:a:0',
|
|
|
|
'-f', 'matroska',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
|
|
|
if (!is_resource($stream)) {
|
|
|
|
throw new PopenStreamException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get video stream from an RTMP video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @return resource popen stream
|
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
* @throws PopenStreamException If the popen stream was not created correctly
|
|
|
|
*/
|
|
|
|
public function getRtmpStream(Video $video)
|
|
|
|
{
|
|
|
|
$urls = $video->getUrl();
|
|
|
|
|
|
|
|
$process = new Process(
|
|
|
|
array_merge(
|
|
|
|
[
|
|
|
|
$this->avconv,
|
|
|
|
'-v', $this->avconvVerbosity,
|
|
|
|
],
|
|
|
|
$video->getRtmpArguments(),
|
|
|
|
[
|
|
|
|
'-i', $urls[0],
|
|
|
|
'-f', $video->ext,
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
|
|
|
if (!is_resource($stream)) {
|
|
|
|
throw new PopenStreamException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the stream of a converted video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @param int $audioBitrate Audio bitrate of the converted file
|
|
|
|
* @param string $filetype Filetype of the converted file
|
2020-06-21 12:03:22 +02:00
|
|
|
* @param bool $audioOnly True to return an audio-only file
|
2020-10-17 22:47:16 +02:00
|
|
|
* @param string|null $from Start the conversion at this time
|
|
|
|
* @param string|null $to End the conversion at this time
|
2020-06-20 22:46:28 +02:00
|
|
|
*
|
|
|
|
* @return resource popen stream
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws AvconvException
|
|
|
|
* @throws EmptyUrlException
|
2020-06-21 12:03:22 +02:00
|
|
|
* @throws InvalidProtocolConversionException If you try to convert an M3U or Dash media
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws InvalidTimeException
|
|
|
|
* @throws PasswordException
|
2020-06-21 12:03:22 +02:00
|
|
|
* @throws PlaylistConversionException If you try to convert a playlist
|
2020-10-17 22:47:16 +02:00
|
|
|
* @throws PopenStreamException If the popen stream was not created correctly
|
|
|
|
* @throws RemuxException
|
|
|
|
* @throws WrongPasswordException
|
|
|
|
* @throws YoutubedlException
|
2020-06-20 22:46:28 +02:00
|
|
|
*/
|
2020-06-21 12:03:22 +02:00
|
|
|
public function getConvertedStream(
|
|
|
|
Video $video,
|
2020-10-17 22:47:16 +02:00
|
|
|
int $audioBitrate,
|
|
|
|
string $filetype,
|
2020-06-21 12:03:22 +02:00
|
|
|
$audioOnly = false,
|
2020-10-17 22:47:16 +02:00
|
|
|
string $from = null,
|
|
|
|
string $to = null
|
2020-06-21 12:03:22 +02:00
|
|
|
) {
|
|
|
|
if (isset($video->_type) && $video->_type == 'playlist') {
|
|
|
|
throw new PlaylistConversionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($video->protocol) && in_array($video->protocol, ['m3u8', 'm3u8_native', 'http_dash_segments'])) {
|
2020-06-20 22:46:28 +02:00
|
|
|
throw new InvalidProtocolConversionException($video->protocol);
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:15:45 +02:00
|
|
|
if (count($video->getUrl()) > 1) {
|
|
|
|
throw new RemuxException('Can not convert and remux at the same time.');
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:03:22 +02:00
|
|
|
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, $audioOnly, $from, $to);
|
2020-06-20 22:46:28 +02:00
|
|
|
|
|
|
|
$stream = popen($avconvProc->getCommandLine(), 'r');
|
|
|
|
|
|
|
|
if (!is_resource($stream)) {
|
|
|
|
throw new PopenStreamException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all extractors.
|
|
|
|
*
|
|
|
|
* @return string[] Extractors
|
|
|
|
*
|
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
public function getExtractors(): array
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
|
|
|
return explode("\n", trim($this->callYoutubedl(['--list-extractors'])));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a HTTP response containing the video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video object
|
|
|
|
* @param mixed[] $headers HTTP headers of the request
|
|
|
|
*
|
|
|
|
* @return ResponseInterface
|
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
*/
|
2020-12-17 22:45:50 +01:00
|
|
|
public function getHttpResponse(Video $video, array $headers = []): ResponseInterface
|
2020-06-20 22:46:28 +02:00
|
|
|
{
|
2021-05-13 12:36:15 +02:00
|
|
|
$client = new Client();
|
2020-06-20 22:46:28 +02:00
|
|
|
|
|
|
|
return $client->request(
|
|
|
|
'GET',
|
2021-05-13 12:31:50 +02:00
|
|
|
$video->url,
|
2020-06-20 22:46:28 +02:00
|
|
|
[
|
|
|
|
'stream' => true,
|
|
|
|
'headers' => array_merge((array)$video->http_headers, $headers)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|