コード例 #1
0
ファイル: YoutubeSource.cs プロジェクト: tony-jang/Delight
        public override bool Download(int SelectedIndex)
        {
            if (SelectedIndex == -1)
            {
                SelectedIndex = 0;
            }

            if (FileCacheDictionary.ContainsId(Id))
            {
                Console.WriteLine("Already Exists!");
                return(false);
            }
            else
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += Client_DownloadProgressChanged;
                client.DownloadFileCompleted   += Client_DownloadFileCompleted;

                YoutubeClient      youtubeClient = new YoutubeClient();
                MediaStreamInfoSet streamInfoSet = youtubeClient.GetVideoMediaStreamInfosAsync(Id).Result;
                MuxedStreamInfo    streamInfo    = streamInfoSet.Muxed.WithHighestVideoQuality();

                if (streamInfo == null)
                {
                    throw new Exception("다운로드 할 수 없는 영상입니다.");
                }

                Console.WriteLine($"Audio:{streamInfo.AudioEncoding.ToString()} Quality : {streamInfo.VideoQualityLabel} Download URL : {streamInfo.Url}");

                string basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Delight\\External Sources Cache\\";

                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }


                string path = basePath + Path.GetRandomFileName() + ".mp4";

                client.DownloadFileAsync(new Uri(streamInfo.Url), path);
                FileCacheDictionary.AddPair(Title, path, Id);

                Console.WriteLine(path);
                return(true);
            }
        }
コード例 #2
0
        private void Source_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            var item = FileCacheDictionary.GetPathFromId(DownloadingSource.Id);

            string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Delight", "External Sources Cache", item.Value.Item1);

            GlobalViewModel.MainWindowViewModel.MediaItems.Add(new VideoMedia()
            {
                Identifier  = item.Key,
                Time        = MediaTools.GetMediaDuration(path),
                Path        = path,
                Thumbnail   = new Uri(DownloadingSource.ThumbnailUri),
                FromYoutube = true,
                DownloadID  = DownloadingSource.Id,
                Id          = DownloadingSource.Id,
            });
        }