2019-04-22 18:20:04 +00:00
|
|
|
<?php
|
2019-10-03 19:41:54 +00:00
|
|
|
|
2019-04-22 18:20:04 +00:00
|
|
|
/**
|
|
|
|
* YoutubeStream class.
|
|
|
|
*/
|
|
|
|
|
2019-04-22 19:06:05 +00:00
|
|
|
namespace Alltube\Stream;
|
2019-04-22 18:20:04 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Downloader;
|
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
|
|
|
use Alltube\Library\Video;
|
2019-04-22 18:20:04 +00:00
|
|
|
use GuzzleHttp\Psr7\AppendStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream that downloads a video in chunks.
|
|
|
|
* This is required because Youtube throttles the download speed on chunks larger than 10M.
|
|
|
|
*/
|
|
|
|
class YoutubeStream extends AppendStream
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* YoutubeStream constructor.
|
|
|
|
*
|
2020-06-20 23:44:20 +00:00
|
|
|
* @param Downloader $downloader Downloader object
|
2019-04-22 18:20:04 +00:00
|
|
|
* @param Video $video Video to stream
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2020-06-20 23:44:20 +00:00
|
|
|
public function __construct(Downloader $downloader, Video $video)
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
$stream = $downloader->getHttpResponse($video);
|
2019-04-22 19:55:45 +00:00
|
|
|
$contentLenghtHeader = $stream->getHeader('Content-Length');
|
2019-04-26 18:38:14 +00:00
|
|
|
$rangeStart = 0;
|
2019-04-22 19:55:45 +00:00
|
|
|
|
|
|
|
while ($rangeStart < $contentLenghtHeader[0]) {
|
|
|
|
$rangeEnd = $rangeStart + $video->downloader_options->http_chunk_size;
|
2019-04-28 19:11:08 +00:00
|
|
|
if ($rangeEnd >= $contentLenghtHeader[0]) {
|
2020-05-13 20:28:05 +00:00
|
|
|
$rangeEnd = intval($contentLenghtHeader[0]) - 1;
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
2020-06-20 23:44:20 +00:00
|
|
|
$response = $downloader->getHttpResponse($video, ['Range' => 'bytes=' . $rangeStart . '-' . $rangeEnd]);
|
2019-04-22 18:20:04 +00:00
|
|
|
$this->addStream(new YoutubeChunkStream($response));
|
2019-04-22 19:55:45 +00:00
|
|
|
$rangeStart = $rangeEnd + 1;
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|