I have a url (example) of audio format from a youtube video
https://rr6---sn-n8v7znsr.googlevideo.com/videoplayback?expire=1691861737&ei=iW7XZM3hHciwv_IP3oe_oAk&ip=kekw&id=o-AH6t-rpPJjbEN9qDVOJlCEU2FTLNrvRtMslTqQ0T4as4&itag=251&source=youtube&requiressl=yes&mh=QC&mm=31%2C26&mn=sn-n8v7znsr%2Csn-c0q7lnse&ms=au%2Conr&mv=m&mvi=6&pl=21&gcr=ru&initcwndbps=1281250&vprv=1&mime=audio%2Fwebm&gir=yes&clen=1641566&dur=108.561&lmt=1689370605701687&mt=1691839772&fvip=5&keepalive=yes&fexp=24007246&c=ANDROID_TESTSUITE&txp=5432434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIgV1LPFsKrQ558nwhqkatBGRWchRXYBWo1fSrc0fSujtkCIQDfpHxCk8hwwyTezP6e6yIGXR7yGNxLfv9qEkP99C_eiw%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgFvFgT1ALo6OrKY_QRvD6-XilXezUdxdObEtQ3Xo9DaQCIQCD-LWIv8v0y04x0ZExDjfZ_kFZbbxBYHaDejTILMmHug%3D%3D
that I can open in browser and click "download" button to download webm file, but how do I do this in C# code?
Let's say I have a method like this:
public async Task Download(string url) { }
In short. I'm trying to download audio from youtube video by clicking a button on UWP form. Tried using YoutubeExplode to download audio by video id, but as I'm writing on UWP I'm unable to use File class that used in this library bc access is always denied. VideoLibrary + MediaToolKit also not a good idea as it downloads video and then convert it to audio format (takes much time). All I can get now are video&audio formats and their urls (like above) and info about videos by Youtube API requests.
HttpClient + Windows.Storage works fine with that, but takes too long to download bc
For most streams, YouTube limits transfer speed to match the video playback rate. This helps them avoid unnecessary bandwidth, but for us it's a hindrance because we want to download the stream as fast as possible. To solve this, we divide the logical stream up into multiple segments and download them all separately.
that is another topic...
Working code:
public async Task Download(string url)
{
StorageFile destination = await (await StorageFolder.GetFolderFromPathAsync("dl-path"))
.CreateFileAsync("name", CreationCollisionOption.GenerateUniqueName);
byte[] buffer = await httpClient.GetByteArrayAsync(url);
using (Stream stream = await destination.OpenStreamForWriteAsync())
{
await stream.WriteAsync(buffer, 0, buffer.Length);
// renaming to control download status
await destination.RenameAsync("name" + ".mp3");
}
}