Move Video class to a separate library

+ improve error handling
+ youtube-dl update
This commit is contained in:
Pierre Rudloff 2020-06-21 01:44:20 +02:00
parent 7d94271a49
commit 5c2823e3f1
30 changed files with 649 additions and 1152 deletions

View file

@ -6,7 +6,8 @@
namespace Alltube;
use Exception;
use Alltube\Exception\ConfigException;
use Alltube\Library\Downloader;
use Jawira\CaseConverter\CaseConverterException;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\Yaml\Yaml;
@ -141,7 +142,7 @@ class Config
* Config constructor.
*
* @param mixed[] $options Options
* @throws CaseConverterException
* @throws ConfigException
*/
private function __construct(array $options = [])
{
@ -197,20 +198,15 @@ class Config
* Throw an exception if some of the options are invalid.
*
* @return void
* @throws Exception If Python is missing
*
* @throws Exception If youtube-dl is missing
* @throws ConfigException If Python is missing
* @throws ConfigException If youtube-dl is missing
*/
private function validateOptions()
{
/*
We don't translate these exceptions because they usually occur before Slim can catch them
so they will go to the logs.
*/
if (!is_file($this->youtubedl)) {
throw new Exception("Can't find youtube-dl at " . $this->youtubedl);
} elseif (!Video::checkCommand([$this->python, '--version'])) {
throw new Exception("Can't find Python at " . $this->python);
throw new ConfigException("Can't find youtube-dl at " . $this->youtubedl);
} elseif (!Downloader::checkCommand([$this->python, '--version'])) {
throw new ConfigException("Can't find Python at " . $this->python);
}
if (!class_exists(Debug::class)) {
@ -241,13 +237,18 @@ class Config
* If the value is an array, you should use the YAML format: "CONVERT_ADVANCED_FORMATS='[foo, bar]'"
*
* @return void
* @throws CaseConverterException
* @throws ConfigException
*/
private function getEnv()
{
foreach (get_object_vars($this) as $prop => $value) {
$convert = new Convert($prop);
$env = getenv($convert->toMacro());
try {
$convert = new Convert($prop);
$env = getenv($convert->toMacro());
} catch (CaseConverterException $e) {
// This should not happen.
throw new ConfigException('Could not parse option name: ' . $prop, $e->getCode(), $e);
}
if ($env) {
$this->$prop = Yaml::parse($env);
}
@ -273,7 +274,7 @@ class Config
*
* @param string $file Path to the YAML file
* @return void
* @throws Exception
* @throws ConfigException
*/
public static function setFile($file)
{
@ -282,7 +283,7 @@ class Config
self::$instance = new self($options);
self::$instance->validateOptions();
} else {
throw new Exception("Can't find config file at " . $file);
throw new ConfigException("Can't find config file at " . $file);
}
}
@ -292,7 +293,7 @@ class Config
* @param mixed[] $options Options (see `config/config.example.yml` for available options)
* @param bool $update True to update an existing instance
* @return void
* @throws Exception
* @throws ConfigException
*/
public static function setOptions(array $options, $update = true)
{
@ -314,4 +315,21 @@ class Config
{
self::$instance = null;
}
/**
* Return a downloader object with the current config.
*
* @return Downloader
*/
public function getDownloader()
{
return new Downloader(
$this->youtubedl,
$this->params,
$this->python,
$this->avconv,
$this->phantomjsDir,
$this->avconvVerbosity
);
}
}

View file

@ -48,7 +48,6 @@ class UglyRouter extends Router
*
* @return string
* @throws InvalidArgumentException If required data not provided
*
* @throws RuntimeException If named route does not exist
*/
public function pathFor($name, array $data = [], array $queryParams = [])

View file

@ -1,647 +0,0 @@
<?php
/**
* VideoDownload class.
*/
namespace Alltube;
use Alltube\Exception\EmptyUrlException;
use Alltube\Exception\PasswordException;
use Exception;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use Symfony\Component\Process\Process;
/**
* Extract info about videos.
*
* Due to the way youtube-dl behaves, this class can also contain information about a playlist.
*
* @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
* @property-read string|null $_type Object type (usually "playlist" or null)
* @property-read stdClass $downloader_options
* @property-read stdClass $http_headers
*/
class Video
{
/**
* Config instance.
*
* @var Config
*/
private $config;
/**
* URL of the page containing the video.
*
* @var string
*/
private $webpageUrl;
/**
* Requested video format.
*
* @var string
*/
private $requestedFormat;
/**
* Password.
*
* @var string|null
*/
private $password;
/**
* JSON object returned by youtube-dl.
*
* @var stdClass
*/
private $json;
/**
* URLs of the video files.
*
* @var string[]
*/
private $urls;
/**
* LocaleManager instance.
*
* @var LocaleManager
*/
protected $localeManager;
/**
* VideoDownload constructor.
*
* @param string $webpageUrl URL of the page containing the video
* @param string $requestedFormat Requested video format
* (can be any format string accepted by youtube-dl,
* including selectors like "[height<=720]")
* @param string $password Password
*/
public function __construct($webpageUrl, $requestedFormat = 'best/bestvideo', $password = null)
{
$this->webpageUrl = $webpageUrl;
$this->requestedFormat = $requestedFormat;
$this->password = $password;
$this->config = Config::getInstance();
$this->localeManager = LocaleManager::getInstance();
}
/**
* Return a youtube-dl process with the specified arguments.
*
* @param string[] $arguments Arguments
*
* @return Process<string>
*/
private static function getProcess(array $arguments)
{
$config = Config::getInstance();
return new Process(
array_merge(
[$config->python, $config->youtubedl],
$config->params,
$arguments
)
);
}
/**
* List all extractors.
*
* @return string[] Extractors
*
* @throws PasswordException
*/
public static function getExtractors()
{
$video = new self('');
return explode("\n", trim($video->callYoutubedl(['--list-extractors'])));
}
/**
* Call youtube-dl.
*
* @param string[] $arguments Arguments
*
* @return string Result
* @throws Exception If the password is wrong
* @throws Exception If youtube-dl returns an error
*
* @throws PasswordException If the video is protected by a password and no password was specified
*/
private function callYoutubedl(array $arguments)
{
$config = Config::getInstance();
$process = self::getProcess($arguments);
//This is needed by the openload extractor because it runs PhantomJS
$process->setEnv(['PATH' => $config->phantomjsDir]);
$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 Exception($this->localeManager->t('Wrong password'), $exitCode);
} else {
throw new Exception($errorOutput, $exitCode);
}
} else {
return trim($process->getOutput());
}
}
/**
* Get a property from youtube-dl.
*
* @param string $prop Property
*
* @return string
* @throws PasswordException
*/
private function getProp($prop = 'dump-json')
{
$arguments = ['--' . $prop];
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;
}
return $this->callYoutubedl($arguments);
}
/**
* Get all information about a video.
*
* @return stdClass Decoded JSON
*
* @throws PasswordException
*/
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
* @throws PasswordException
*/
public function __get($name)
{
if (isset($this->$name)) {
return $this->getJson()->$name;
}
return null;
}
/**
* Magic method to check if the JSON object returned by youtube-dl has a property.
*
* @param string $name Property
*
* @return bool
* @throws PasswordException
*/
public function __isset($name)
{
return isset($this->getJson()->$name);
}
/**
* Get URL of video from URL of page.
*
* It generally returns only one URL.
* But it can return two URLs when multiple formats are specified
* (eg. bestvideo+bestaudio).
*
* @return string[] URLs of video
* @throws EmptyUrlException
* @throws PasswordException
*/
public function getUrl()
{
// Cache the URLs.
if (!isset($this->urls)) {
$this->urls = explode("\n", $this->getProp('get-url'));
if (empty($this->urls[0])) {
throw new EmptyUrlException($this->localeManager->t('youtube-dl returned an empty URL.'));
}
}
return $this->urls;
}
/**
* Get filename of video file from URL of page.
*
* @return string Filename of extracted video
*
* @throws PasswordException
*/
public function getFilename()
{
return trim($this->getProp('get-filename'));
}
/**
* Get filename of video with the specified extension.
*
* @param string $extension New file extension
*
* @return string Filename of extracted video with specified extension
* @throws PasswordException
*/
public function getFileNameWithExtension($extension)
{
return str_replace('.' . $this->ext, '.' . $extension, $this->getFilename());
}
/**
* Return arguments used to run rtmp for a specific video.
*
* @return string[] Arguments
*/
private function getRtmpArguments()
{
$arguments = [];
if ($this->protocol == 'rtmp') {
foreach (
[
'url' => '-rtmp_tcurl',
'webpage_url' => '-rtmp_pageurl',
'player_url' => '-rtmp_swfverify',
'flash_version' => '-rtmp_flashver',
'play_path' => '-rtmp_playpath',
'app' => '-rtmp_app',
] as $property => $option
) {
if (isset($this->{$property})) {
$arguments[] = $option;
$arguments[] = $this->{$property};
}
}
if (isset($this->rtmp_conn)) {
foreach ($this->rtmp_conn as $conn) {
$arguments[] = '-rtmp_conn';
$arguments[] = $conn;
}
}
}
return $arguments;
}
/**
* Check if a command runs successfully.
*
* @param string[] $command Command and arguments
*
* @return bool False if the command returns an error, true otherwise
*/
public static function checkCommand(array $command)
{
$process = new Process($command);
$process->run();
return $process->isSuccessful();
}
/**
* Get a process that runs avconv in order to convert a video.
*
* @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
*
* @return Process<string> Process
* @throws Exception If avconv/ffmpeg is missing
*
*/
private function getAvconvProcess(
$audioBitrate,
$filetype = 'mp3',
$audioOnly = true,
$from = null,
$to = null
) {
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw new Exception(
$this->localeManager->t(
"Can't find avconv or ffmpeg at @path.",
['@path' => $this->config->avconv]
)
);
}
$durationRegex = '/(\d+:)?(\d+:)?(\d+)/';
$afterArguments = [];
if ($audioOnly) {
$afterArguments[] = '-vn';
}
if (!empty($from)) {
if (!preg_match($durationRegex, $from)) {
throw new Exception($this->localeManager->t('Invalid start time: @from.', ['@from' => $from]));
}
$afterArguments[] = '-ss';
$afterArguments[] = $from;
}
if (!empty($to)) {
if (!preg_match($durationRegex, $to)) {
throw new Exception($this->localeManager->t('Invalid end time: @to.', ['@to' => $to]));
}
$afterArguments[] = '-to';
$afterArguments[] = $to;
}
$urls = $this->getUrl();
$arguments = array_merge(
[
$this->config->avconv,
'-v', $this->config->avconvVerbosity,
],
$this->getRtmpArguments(),
[
'-i', $urls[0],
'-f', $filetype,
'-b:a', $audioBitrate . 'k',
],
$afterArguments,
[
'pipe:1',
]
);
//Vimeo needs a correct user-agent
$arguments[] = '-user_agent';
$arguments[] = $this->getProp('dump-user-agent');
return new Process($arguments);
}
/**
* Get audio stream of converted video.
*
* @param string $from Start the conversion at this time
* @param string $to End the conversion at this time
*
* @return resource popen stream
* @throws Exception If the popen stream was not created correctly
*
* @throws Exception If your try to convert an M3U8 video
*/
public function getAudioStream($from = null, $to = null)
{
if (isset($this->_type) && $this->_type == 'playlist') {
throw new Exception($this->localeManager->t('Conversion of playlists is not supported.'));
}
if (isset($this->protocol)) {
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
} elseif ($this->protocol == 'http_dash_segments') {
throw new Exception($this->localeManager->t('Conversion of DASH segments is not supported.'));
}
}
$avconvProc = $this->getAvconvProcess($this->config->audioBitrate, 'mp3', true, $from, $to);
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
}
/**
* Get video stream from an M3U playlist.
*
* @return resource popen stream
* @throws Exception If the popen stream was not created correctly
*
* @throws Exception If avconv/ffmpeg is missing
*/
public function getM3uStream()
{
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw new Exception(
$this->localeManager->t(
"Can't find avconv or ffmpeg at @path.",
['@path' => $this->config->avconv]
)
);
}
$urls = $this->getUrl();
$process = new Process(
[
$this->config->avconv,
'-v', $this->config->avconvVerbosity,
'-i', $urls[0],
'-f', $this->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 Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
}
/**
* Get an avconv stream to remux audio and video.
*
* @return resource popen stream
* @throws Exception If the popen stream was not created correctly
*
*/
public function getRemuxStream()
{
$urls = $this->getUrl();
if (!isset($urls[0]) || !isset($urls[1])) {
throw new Exception($this->localeManager->t('This video does not have two URLs.'));
}
$process = new Process(
[
$this->config->avconv,
'-v', $this->config->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 Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
}
/**
* Get video stream from an RTMP video.
*
* @return resource popen stream
* @throws Exception If the popen stream was not created correctly
*
*/
public function getRtmpStream()
{
$urls = $this->getUrl();
$process = new Process(
array_merge(
[
$this->config->avconv,
'-v', $this->config->avconvVerbosity,
],
$this->getRtmpArguments(),
[
'-i', $urls[0],
'-f', $this->ext,
'pipe:1',
]
)
);
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
}
/**
* Get the stream of a converted video.
*
* @param int $audioBitrate Audio bitrate of the converted file
* @param string $filetype Filetype of the converted file
*
* @return resource popen stream
* @throws Exception If the popen stream was not created correctly
*
* @throws Exception If your try to convert and M3U8 video
*/
public function getConvertedStream($audioBitrate, $filetype)
{
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvProcess($audioBitrate, $filetype, false);
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
}
/**
* Get the same video but with another format.
*
* @param string $format New format
*
* @return Video
*/
public function withFormat($format)
{
return new self($this->webpageUrl, $format, $this->password);
}
/**
* Get a HTTP response containing the video.
*
* @param mixed[] $headers HTTP headers of the request
*
* @return ResponseInterface
* @throws EmptyUrlException
* @throws PasswordException
* @link https://github.com/guzzle/guzzle/issues/2640
*/
public function getHttpResponse(array $headers = [])
{
// IDN conversion breaks with Google hosts like https://r3---sn-25glene6.googlevideo.com/.
$client = new Client(['idn_conversion' => false]);
$urls = $this->getUrl();
$stream_context_options = [];
if (array_key_exists('Referer', (array)$this->http_headers)) {
$stream_context_options = [
'http' => [
'header' => 'Referer: ' . $this->http_headers->Referer
]
];
}
return $client->request(
'GET',
$urls[0],
[
'stream' => true,
'stream_context' => $stream_context_options,
'headers' => array_merge((array)$this->http_headers, $headers)
]
);
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace Alltube\Exception;
use Exception;
class ConfigException extends Exception
{
}

View file

@ -1,16 +0,0 @@
<?php
/**
* EmptyUrlException class.
*/
namespace Alltube\Exception;
use Exception;
/**
* Exception thrown when youtube-dl returns an empty URL.
*/
class EmptyUrlException extends Exception
{
}

View file

@ -1,16 +0,0 @@
<?php
/**
* PasswordException class.
*/
namespace Alltube\Exception;
use Exception;
/**
* Exception thrown when a video requires a password.
*/
class PasswordException extends Exception
{
}

View file

@ -6,8 +6,8 @@
namespace Alltube\Stream;
use Alltube\Video;
use Exception;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Video;
use Slim\Http\Stream;
/**
@ -21,11 +21,11 @@ class ConvertedPlaylistArchiveStream extends PlaylistArchiveStream
* @param Video $video Video to stream
*
* @return void
* @throws Exception
* @throws AlltubeLibraryException
*/
protected function startVideoStream(Video $video)
{
$this->curVideoStream = new Stream($video->getAudioStream());
$this->curVideoStream = new Stream($this->downloader->getAudioStream($video));
$this->init_file_stream_transfer(
$video->getFileNameWithExtension('mp3'),

View file

@ -6,9 +6,9 @@
namespace Alltube\Stream;
use Alltube\Exception\EmptyUrlException;
use Alltube\Exception\PasswordException;
use Alltube\Video;
use Alltube\Library\Downloader;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Video;
use Barracuda\ArchiveStream\ZipArchive;
use Psr\Http\Message\StreamInterface;
@ -47,22 +47,32 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface
*/
private $isComplete = false;
/**
* Downloader object.
*
* @var Downloader
*/
protected $downloader;
/**
* PlaylistArchiveStream constructor.
*
* We don't call the parent constructor because it messes up the output buffering.
*
* @param Downloader $downloader Downloader object
* @param Video $video Video/playlist to download
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(Video $video)
public function __construct(Downloader $downloader, Video $video)
{
$this->downloader = $downloader;
$buffer = fopen('php://temp', 'r+');
if ($buffer !== false) {
$this->buffer = $buffer;
}
foreach ($video->entries as $entry) {
$this->videos[] = new Video($entry->url);
$this->videos[] = $downloader->getVideo($entry->url);
}
}
@ -244,12 +254,11 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface
* @param Video $video Video to stream
*
* @return void
* @throws PasswordException
* @throws EmptyUrlException
* @throws AlltubeLibraryException
*/
protected function startVideoStream(Video $video)
{
$response = $video->getHttpResponse();
$response = $this->downloader->getHttpResponse($video);
$this->curVideoStream = $response->getBody();
$contentLengthHeaders = $response->getHeader('Content-Length');
@ -266,8 +275,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface
* @param int $count Number of bytes to read
*
* @return string|false
* @throws EmptyUrlException
* @throws PasswordException
* @throws AlltubeLibraryException
*/
public function read($count)
{

View file

@ -6,9 +6,9 @@
namespace Alltube\Stream;
use Alltube\Exception\EmptyUrlException;
use Alltube\Exception\PasswordException;
use Alltube\Video;
use Alltube\Library\Downloader;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Video;
use GuzzleHttp\Psr7\AppendStream;
/**
@ -20,15 +20,15 @@ class YoutubeStream extends AppendStream
/**
* YoutubeStream constructor.
*
* @param Downloader $downloader Downloader object
* @param Video $video Video to stream
* @throws EmptyUrlException
* @throws PasswordException
* @throws AlltubeLibraryException
*/
public function __construct(Video $video)
public function __construct(Downloader $downloader, Video $video)
{
parent::__construct();
$stream = $video->getHttpResponse();
$stream = $downloader->getHttpResponse($video);
$contentLenghtHeader = $stream->getHeader('Content-Length');
$rangeStart = 0;
@ -37,7 +37,7 @@ class YoutubeStream extends AppendStream
if ($rangeEnd >= $contentLenghtHeader[0]) {
$rangeEnd = intval($contentLenghtHeader[0]) - 1;
}
$response = $video->getHttpResponse(['Range' => 'bytes=' . $rangeStart . '-' . $rangeEnd]);
$response = $downloader->getHttpResponse($video, ['Range' => 'bytes=' . $rangeStart . '-' . $rangeEnd]);
$this->addStream(new YoutubeChunkStream($response));
$rangeStart = $rangeEnd + 1;
}