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,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;
}