Youtubeexplode: `GetMediaStreamAsync` To handle Youtube stuff?

Created on 22 Mar 2018  路  9Comments  路  Source: Tyrrrz/YoutubeExplode

GetMediaStreamAsync that would handle rate limiting for me?

It took me 2 hours until it occured to me to check how DownloadMediaStreamAsync works.

I would like to get a stream that handles all the Youtube "shenanigans" for me, because right now to make my stream work I would have to basically copy DownloadMediaStreamAsyc code, instead of reusing it from this library?

enhancement

All 9 comments

I was already considering what would be the best way to do this. Will try to prioritize it.

Also can you clarify what's the use case for this? Are you playing the video or trying to load it into memory?

trying to load into MemoryStream.

public async Task<HttpResult> Get(YoutubeDownloadVideoRequest request)
{
    var mediaInfo = await _youtubeClient.GetVideoMediaStreamInfosAsync(request.VideoId);
    var videoStream = mediaInfo.Video.WithHighestVideoQuality();
    var stream = await _youtubeClient.GetMediaStreamAsync(videoStream);
    var ms = new MemoryStream();
    stream.CopyTo(ms);
    return new HttpResult(ms, $"video/{stream.Info.Container}");
}

Was testing out if I can do something like this.
But know I know that I would need to download the stream fully, so there would be no point in returning memory stream in this case.
But still loading into memory would be nice.

Why would you want to do that considering videos can be large?

You should be able to download it to file instead and steam it from there as a response. Could also be useful for caching.

True that, I might consider this :).
But then again, it writes only on physical storage, where as I need to write to virtual/temporary file storage :), I access files through interface.

Yeah you will have to clean up after yourself I guess, but you really shouldn't load large binary files into RAM. I will do something to make it possible though.

That's the problem :)
I need a stream that I could load into a file or through network, without loading into ram.

It would be really difficult to return a single stream that will run multiple HTTP requests behind the hood and I think in most use-cases it's not worth it. However I added an overload for DownloadMediaStreamAsync that takes stream as an output (instead of file path), this way you can copy the contents to a stream at full speed, without allocating the video in memory. In your example, however, you would need to figure out a different way to use it, because the way it is you'll still have a memory stream anyway, but at least now you won't have to copy-paste the rate-bypass logic from YoutubeExplode.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vsjtechnologies picture vsjtechnologies  路  7Comments

nk34player picture nk34player  路  4Comments

Luke1998x picture Luke1998x  路  5Comments

jungjanos picture jungjanos  路  6Comments

alexrainman picture alexrainman  路  5Comments