Esempio n. 1
0
 public ProgressUpdateEventArgs(long contentMax, long current, int mode, DownloadReservation downloadReservation)
 {
     ContentMax          = contentMax;
     Current             = current;
     Mode                = mode;
     DownloadReservation = downloadReservation;
 }
Esempio n. 2
0
 private void Run()
 {
     while (!stopped)
     {
         waitHandle.WaitOne();
         while (true)
         {
             DownloadReservation res = null;
             var a = BeginInvoke(new Action(() => {
                 lock (SyncRoot)
                 {
                     if (listDownloads.Items.Count > 0)
                     {
                         res = (DownloadReservation)listDownloads.Items[0];
                         listDownloads.Items.RemoveAt(0);
                     }
                 }
             }));
             a.AsyncWaitHandle.WaitOne();
             if (res != null)
             {
                 var downloader = new Downloader();
                 downloader.UpdateEvent += Downloader_UpdateEvent;
                 lastMode = -1;
                 downloader.StartDownload(res);
                 // MessageBox.Show(res.Name + "\n\n" + res.VideoURL);
             }
             else
             {
                 break;
             }
         }
     }
 }
Esempio n. 3
0
        public void StartDownload(DownloadReservation download)
        {
            var source  = download.SaveDir;
            var youtube = YouTube.Default;
            var vid     = youtube.GetVideo(download.VideoURL);

            var tmp  = Path.GetTempFileName();
            var name = Path.Combine(source, download.Name);
            var uri  = vid.Uri;

            var buffer = new byte[8192];
            var cur    = 0L;

            var request = (HttpWebRequest)WebRequest.Create(uri);

            using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                    using (var fs = File.Open(tmp, FileMode.Create))
                    {
                        var total = response.ContentLength;
                        while (true)
                        {
                            var r = stream.Read(buffer, 0, 8192);
                            if (r == 0)
                            {
                                break;
                            }
                            cur += r;
                            fs.Write(buffer, 0, r);
                            UpdateEvent?.Invoke(this, new ProgressUpdateEventArgs(total, cur, 0, download));
                        }
                    }

            var inputFile = new MediaFile {
                Filename = tmp
            };
            var outputFile = new MediaFile {
                Filename = $"{name}.mp3"
            };
            var option = new ConversionOptions()
            {
                AudioSampleRate = AudioSampleRate.Hz44100
            };

            using (var engine = new Engine())
            {
                engine.ConversionCompleteEvent += (s, e) =>
                {
                    File.Delete(tmp);
                };
                engine.ConvertProgressEvent += (s, e) =>
                {
                    var totalTT = e.TotalDuration.TotalMilliseconds;
                    var curTT   = e.TotalDuration.TotalMilliseconds / e.ProcessedDuration.TotalMilliseconds;
                    UpdateEvent?.Invoke(this, new ProgressUpdateEventArgs((long)totalTT, (long)curTT, 1, download));
                };
                engine.GetMetadata(inputFile);
                UpdateEvent?.Invoke(this, new ProgressUpdateEventArgs(1, 0, 1, download));
                engine.Convert(inputFile, outputFile, option);
                UpdateEvent?.Invoke(this, new ProgressUpdateEventArgs(1, 1, 1, download));
            }
            UpdateEvent?.Invoke(this, new ProgressUpdateEventArgs(1, 1, 2, download));
        }