public async Task CanExtractLinks()
        {
            IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync("https://www.youtube.com/watch?v=rKTUAESacQM");

            Assert.IsTrue(videoInfos != null);
            Assert.IsTrue(videoInfos.Any());
        }
 public static void GetYouTubeVideoUri(string uri, int preferredQuality, Action <bool, Uri> resultCallback)
 {
     if (preferredQuality <= 0)
     {
         preferredQuality = 480;
     }
     DownloadUrlResolver.GetDownloadUrlsAsync(uri, false).ContinueWith((Action <Task <IEnumerable <VideoInfo> > >)(task =>
     {
         if (task.Status != TaskStatus.RanToCompletion || task.Result == null || !Enumerable.Any <VideoInfo>(task.Result))
         {
             resultCallback(false, null);
         }
         else
         {
             VideoInfo videoInfo = (VideoInfo)Enumerable.FirstOrDefault <VideoInfo>(Enumerable.Concat <VideoInfo>(Enumerable.OrderBy <VideoInfo, int>(Enumerable.Where <VideoInfo>(task.Result, (Func <VideoInfo, bool>)(video =>
             {
                 if (video.Resolution < preferredQuality)
                 {
                     return(false);
                 }
                 if (video.VideoType != VideoType.Mp4)
                 {
                     return(video.VideoType == VideoType.Mobile);
                 }
                 return(true);
             })), (Func <VideoInfo, int>)(video => video.Resolution)), Enumerable.OrderBy <VideoInfo, int>(Enumerable.Where <VideoInfo>(task.Result, (Func <VideoInfo, bool>)(video =>
             {
                 if (video.Resolution >= preferredQuality)
                 {
                     return(false);
                 }
                 if (video.VideoType != VideoType.Mp4)
                 {
                     return(video.VideoType == VideoType.Mobile);
                 }
                 return(true);
             })), (Func <VideoInfo, int>)(video => - video.Resolution))));
             if (videoInfo == null)
             {
                 resultCallback(false, null);
             }
             else if (videoInfo.RequiresDecryption)
             {
                 try
                 {
                     DownloadUrlResolver.DecryptDownloadUrl(videoInfo);
                     resultCallback(true, new Uri(videoInfo.DownloadUrl));
                 }
                 catch (Exception)
                 {
                     resultCallback(false, null);
                 }
             }
             else
             {
                 resultCallback(true, new Uri(videoInfo.DownloadUrl));
             }
         }
     }));
 }
예제 #3
0
        private async void GetVideoInfos()
        {
            var videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync("http://www.youtube.com/watch?v=fRh_vgS2dFE");

            videoEnumerator = videoInfos.GetEnumerator();
            videoEnumerator.MoveNext();
            PlayVideo();
        }
예제 #4
0
        public async Task ChooseBestVideo()
        {
            IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync("https://www.youtube.com/watch?v=vxMxYgkUcdU");

            var downloader = new VideoDownloader();

            Assert.IsNotNull(downloader.ChooseBest(videoInfos));
        }
예제 #5
0
        private static async void Run()
        {
            IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync("http://www.youtube.com/watch?v=6bMmhKz6KXg");

            foreach (VideoInfo videoInfo in videoInfos)
            {
                Console.WriteLine(videoInfo.DownloadUrl);
                Console.WriteLine();
            }
        }
예제 #6
0
        /// <summary>
        /// 비동기로 다운로드를 시작한다
        /// </summary>
        public async void BeginDownload()
        {
            try
            {
                this.Status = "다운로드 시도";
                IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(this.DownloadUrl);

                if (videoInfos != null)
                {
                    switch (this.Type)
                    {
                    case ContentsType.Audio:
                    {
                        break;
                    }

                    case ContentsType.MP4:
                    {
                        IEnumerable <VideoInfo> mp4Videos = videoInfos.Where(infor => infor.VideoType == VideoType.Mp4);

                        foreach (var v in mp4Videos)
                        {
                            if (v.RequiresDecryption == true)
                            {
                                DownloadUrlResolver.DecryptDownloadUrl(v);
                            }

                            var           bgDownloader = new BackgroundDownloader();
                            StorageFolder saveDir      = KnownFolders.VideosLibrary;
                            var           part         = await saveDir.CreateFileAsync(
                                v.Title + v.VideoExtension, CreationCollisionOption.ReplaceExisting);

                            var operation = bgDownloader.CreateDownload(new Uri(v.DownloadUrl), part);

                            await this.DoDownloadAsync(operation);

                            break;
                        }
                        break;
                    }
                    }
                }
            }
            catch (Exception e)
            {
                this.Status = "다운로드 실패";
                var dialog = new MessageDialog(e.Message);
                await dialog.ShowAsync();
            }
        }
예제 #7
0
        async Task <IEnumerable <VideoInfo> > CheckVideoAvailability(string url)
        {
            try
            {
                IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(url);

                return(videoInfos);
            }catch (Exception ex)
            {
                Tracker.TrackVideoNotAvailable();
                Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
                return(null);
            }
        }
예제 #8
0
        public async Task TestVideos()
        {
            var list = new List <string>()
            {
                "https://www.youtube.com/watch?v=vxMxYgkUcdU",
                "https://www.youtube.com/watch?v=ZS0WvzRVByg",
                "https://www.youtube.com/watch?v=iX-QaNzd-0Y",
                "https://www.youtube.com/watch?v=Pyly3JtXoy4"
            };

            foreach (var item in list)
            {
                IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(item);

                Assert.IsNotNull(videoInfos);
                Assert.IsTrue(videoInfos.Any());
            }
        }
예제 #9
0
        private async Task <List <IVideoLink> > GetYouTubeLinks(string videoUrl)
        {
            try
            {
                var links = await DownloadUrlResolver.GetDownloadUrlsAsync(videoUrl.Replace("?__ref=vk.wphone", ""));

                return(links
                       .Where(v => v.VideoType == YoutubeExtractor.VideoType.Mp4)
                       .Select(v => new CommonVideoLink {
                    Name = v.Resolution.ToString(), Source = v.DownloadUrl
                })
                       .Cast <IVideoLink>()
                       .ToList());
            }
            catch (Exception)
            {
                throw new LinksExtractionFailedException("YouTube", videoUrl);
            }
        }
예제 #10
0
        public async Task DownloadVideo()
        {
            IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync("https://www.youtube.com/watch?v=vxMxYgkUcdU");

            var downloader = new VideoDownloader();
            var stream     = await downloader.Execute(videoInfos.FirstOrDefault());

            Assert.IsTrue(stream.Length > 0);

            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                File.WriteAllBytes("video.flv", ms.ToArray());
            }
        }
예제 #11
0
        public async Task <VideoInfo> ResolveDownloadUrls(string link, Android.Content.Context context)
        {
            IEnumerable <VideoInfo> videosInfors = new List <VideoInfo>();

            try
            {
                videosInfors = await DownloadUrlResolver.GetDownloadUrlsAsync(link, true);
            }
            catch (Exception ex)
            {
                var displayHelper = new DisplayHelper();
                displayHelper.AlertUserOfError(ex.Message, context);
                Toast.MakeText(context, "Cannot play this video", ToastLength.Short);
            }

            VideoInfo video = videosInfors.FirstOrDefault(infor => (infor.AudioType == AudioType.Aac || infor.AudioType == AudioType.Mp3) && infor.Resolution == 144);

            if (video == null)
            {
                video = videosInfors.FirstOrDefault(infor => (infor.AudioType == AudioType.Aac || infor.AudioType == AudioType.Mp3) && infor.Resolution == 240);
            }

            if (video == null)
            {
                video = videosInfors.FirstOrDefault(infor => infor.AudioType == AudioType.Aac || infor.AudioType == AudioType.Mp3);
            }

            if (video != null)
            {
                if (video.RequiresDecryption)
                {
                    await this.DecryptDownloadUrlAsync(video);

                    var response = await this.GetHttpResponse(video.DownloadUrl, video);
                }
            }
            return(video);
        }
예제 #12
0
        private static async Task RunExample()
        {
            const string videoUrl = "https://www.youtube.com/watch?v=6tjHBbvKd3M";

            try
            {
                var videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(videoUrl, false);

                var videoWithAudio =
                    videoInfos.FirstOrDefault(video => video.Resolution > 0 && video.AudioBitrate > 0);

                if (videoWithAudio != null)
                {
                    Console.WriteLine($"Video title:{videoWithAudio.Title}");
                    Console.WriteLine($"Video url:{videoWithAudio.DownloadUrl}");
                }
                else
                {
                    Console.WriteLine("Video with audio not found");
                }
            }
            catch (YoutubeVideoNotAvailableException)
            {
                Console.WriteLine("Video is not available");
            }
            catch (YoutubeParseException)
            {
                Console.WriteLine("Error while trying to parse youtube data");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
예제 #13
0
        public static async Task <string> GetEmbededVideo(string url)
        {
            try
            {
                if (url.Contains("youtube") || url.Contains("youtu"))
                {
                    VideoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(url);

                    var videoInfos = VideoInfos.ToList();
                    if (videoInfos.Count() > 1)
                    {
                        YoutubeExtractor.VideoInfo video = videoInfos.FirstOrDefault(info => info.VideoType == VideoType.Mp4 && info.Resolution == 720);

                        if (video == null)
                        {
                            video = videoInfos.FirstOrDefault(
                                info => info.VideoType == VideoType.Mp4 && info.Resolution == 480);
                        }

                        if (video == null)
                        {
                            video = videoInfos.FirstOrDefault(
                                info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);
                        }

                        if (video == null)
                        {
                            video = videoInfos.FirstOrDefault(
                                info => info.VideoType == VideoType.Mp4 && info.Resolution == 240);
                        }

                        if (video == null)
                        {
                            video = videoInfos.FirstOrDefault(
                                info => info.VideoType == VideoType.Mp4 && info.Resolution == 144);
                        }

                        if (video != null)
                        {
                            if (video.RequiresDecryption)
                            {
                                DownloadUrlResolver.DecryptDownloadUrl(video);
                            }
                            VideoSelected       = video;
                            VideoDownloadstring = video.DownloadUrl;

                            return(VideoDownloadstring);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        var result = await Get_Youtube_Video(url);

                        if (result != null)
                        {
                            VideoDownloadstring = result[0].Videourl;
                            return(VideoDownloadstring);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }

                else
                {
                    return(null);
                }
            }
            catch (IOException exception)
            {
                Console.WriteLine(exception);
                return("Invalid");
            }
        }
예제 #14
0
        public async void catchlink()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(url_))
                {
                    return;
                }
                else
                {
                    cmbQuality.Enabled = false;
                    btnCancel.Enabled  = false;
                    btnOk.Enabled      = false;
                    button1.Enabled    = false;
                    button2.Enabled    = false;
                    textBox1.Enabled   = false;
                    //    pbdown.Value = 0;
                    string link = url_;
                    pictureBox1.Image = Properties.Resources.loading;

                    /*
                     * Get the available video formats.
                     * We'll work with them in the video and audio download examples.
                     */
                    IEnumerable <VideoInfo> videoInfos = await DownloadUrlResolver.GetDownloadUrlsAsync(link);

                    /*
                     * download selected quality video or extract audio
                     */
                    if (cmbQuality.SelectedItem.ToString().Trim().Contains("Mp3"))
                    {
                        TubeDlHelpers.video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 0 && info.AudioBitrate == 128);
                    }
                    else
                    {
                        int vres;
                        switch (cmbQuality.SelectedItem.ToString().Trim())
                        {
                        case "1080p: 1920x1080 (no audio)":
                            vres = 1080;
                            break;

                        case "720p: 1280x720":
                            vres = 720;
                            break;

                        case "480p: 854x480":
                            vres = 480;
                            break;

                        case "360p: 640x360":
                            vres = 360;
                            break;

                        case "240p: 426x240":
                            vres = 240;
                            break;

                        default:
                            vres = 720;
                            break;

                            /*1080p: 1920x1080
                             * 720p: 1280x720
                             * 480p: 854x480
                             * 360p: 640x360
                             * 240p: 426x240*/
                        }
                        TubeDlHelpers.video = videoInfos
                                              .First(info => info.VideoType == VideoType.Mp4 && info.Resolution == vres);
                    }
#if DEBUG
                    //  MessageBox.Show(TubeDlMethods.video.CanExtractAudio.ToString());
#endif

                    /*
                     * If the video has a decrypted signature, decipher it
                     */
                    if (TubeDlHelpers.video.RequiresDecryption)
                    {
                        DownloadUrlResolver.DecryptDownloadUrl(TubeDlHelpers.video);
                    }

                    /*
                     * show video info
                     * */
                    lbltitle.Text = TubeDlHelpers.video.Title;
                    if (TubeDlHelpers.video.Resolution.ToString() == ("0"))
                    {
                        lblformat.Text = "Mp3";
                    }
                    else
                    {
                        lblformat.Text = "Mp4";
                    }
                    lblquality.Text = TubeDlHelpers.video.Resolution.ToString() == ("0")
                        ? "Audio" : TubeDlHelpers.video.Resolution.ToString() + "p";
                    lblsize.Text  = TubeDlHelpers.GetFileSize(new Uri(TubeDlHelpers.video.DownloadUrl));
                    lblaudio.Text = TubeDlHelpers.video.AudioType.ToString() + " " + TubeDlHelpers.video.AudioBitrate.ToString() + "kHz";
                    var imgurl = "https://img.youtube.com/vi/" +
                                 url_.Replace("http://", "").Replace("https://", "").Replace("www", "").Replace("youtube.com/watch?v=", "").Trim()
                                 + "/0.jpg";
                    pictureBox1.ImageLocation = imgurl;
                    string vname = Unkdevt.StringHelpers.RemoveIllegalPathCharacters(TubeDlHelpers.video.Title)
                                   + " " + (TubeDlHelpers.video.Resolution == 0 ? "" : TubeDlHelpers.video.Resolution.ToString() + "p") + TubeDlHelpers.Extention();

                    TubeDlHelpers.downloadurl = TubeDlHelpers.video.DownloadUrl;
                    textBox1.Text             = Path.Combine(TubeDlHelpers.SavePath, vname);
                    cmbQuality.Enabled        = true;
                    btnCancel.Enabled         = true;
                    btnOk.Enabled             = true;
                    button1.Enabled           = true;
                    button2.Enabled           = true;
                    textBox1.Enabled          = true;
                }
            }
            catch (YoutubeParseException)
            {
                //btndownload.Enabled = false;
                if (MessageBox.Show("Error while parse URL \n" + exTextBox1.Text, Text, MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                {
                    catchlink();
                }
                else
                {
                    Close();
                }
            }

            catch (Exception ex)
            {
                if (ex.Message.Contains("youtube.com"))
                {
                    MessageBox.Show("Check internet connection");
                }
                else
                {
                    MessageBox.Show(ex.Message + "\ntry again with low quality");
                }
                cmbQuality.Enabled = true;
                pictureBox1.Image  = null;

                //Close();
            }
        }