alltube/classes/streams/YoutubeStream.php

42 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2019-10-03 19:41:54 +00:00
/**
* YoutubeStream class.
*/
namespace Alltube\Stream;
use Alltube\Video;
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.
*
* @param Video $video Video to stream
*/
public function __construct(Video $video)
{
parent::__construct();
$stream = $video->getHttpResponse();
$contentLenghtHeader = $stream->getHeader('Content-Length');
2019-04-26 18:38:14 +00:00
$rangeStart = 0;
while ($rangeStart < $contentLenghtHeader[0]) {
$rangeEnd = $rangeStart + $video->downloader_options->http_chunk_size;
2019-04-28 19:11:08 +00:00
if ($rangeEnd >= $contentLenghtHeader[0]) {
$rangeEnd = $contentLenghtHeader[0] - 1;
}
2019-10-03 19:41:54 +00:00
$response = $video->getHttpResponse(['Range' => 'bytes=' . $rangeStart . '-' . $rangeEnd]);
$this->addStream(new YoutubeChunkStream($response));
$rangeStart = $rangeEnd + 1;
}
}
}