Guest User

seekable network stream ?

a guest
Jun 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using VideoLibrary;
  10. namespace YoutubeDownloader
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             YouTube youtube = YouTube.Default;
  17.             // 19 seconds of nature slides Totaly SFW NO AUDIO too
  18.             YouTubeVideo video = youtube.GetVideo("https://www.youtube.com/watch?v=668nUCeBHyY");
  19.             string outFile = $"dummy2{video.FileExtension}";
  20.             FileStream fs = new FileStream(outFile, FileMode.OpenOrCreate);
  21.             // problem if the user started the download ,then it break at somepoint
  22.             // I know the video is on-demand so what ever position you wanna play
  23.             // I can get
  24.             // but can't because the stream is a net stream and can't be seekable
  25.             // here is what I tried
  26.             Stream videoStream = WebRequest.Create(video.Uri).GetResponse().GetResponseStream();
  27.             byte[] buff = new byte[1024];
  28.             int i = 0;
  29.             int n = 0;
  30.             while ((n = videoStream.Read(buff, i, buff.Length)) > 0)
  31.             {
  32.  
  33.                 fs.Write(buff, i, buff.Length);
  34.                 i++;
  35.             }
  36.             // right now i download and get the video's like this
  37.             using (WebClient client = new WebClient())
  38.             {
  39.                 // I use as assync and handle events but for simplicity
  40.                 // i'm just gonna use this
  41.                 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
  42.                 client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(downloadFInished);
  43.                 client.DownloadFileAsync(new Uri(video.Uri), outFile);
  44.                 // but I have no way of resuming or pausing
  45.  
  46.             }
  47.         }
  48.         private static void downloadFInished(object sender, AsyncCompletedEventArgs e)
  49.         {
  50.             // do something on download finshed
  51.         }
  52.         private static void downloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  53.         {
  54.             // show progress or what ever
  55.         }
  56.     }
  57. }
Add Comment
Please, Sign In to add comment