Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            string input = "https://www.youtube.com/watch?v=xyvQaVUeu90";     // here 'input' is the URL of youtube video...
                                                                              // you can also assign URL of a video from playlist to 'input'...
                                                                              // 'input' can also be a search query (e.g. input = "windows 10")...
            string playlistID;
            string playlistTitle;

            YouTube youtube = new YouTube();    // create object of YouTube class...

            switch (youtube.Check(input))       // 'Check(input)' method automatically detects if input is a URL or a search query and returns 'InputType'...
            {                                   // (e.g. InputType.VideoURL, InputType.PlaylistURL, InputType.SearchQuery, InputType.Invalid)...
            case YouTube.InputType.VideoURL:
                foreach (Information information in youtube.Informations(youtube.GetVideoID(input)))
                {
                    /*
                     * all the information can be retrieved from 'information' object...
                     */

                    bool   adaptive = information.adaptive;                                                         // whether the video is in DASH format or not (if the video is in DASH format, value of 'adaptive' will be true)...
                    int    itag     = information.itag;                                                             // itag is a tag assigned by youtube for different resolutions and file formats...
                    string videoID  = information.videoID;                                                          // unique video id...
                    string title    = information.title;                                                            // title of the video...
                    string duration = information.duration;                                                         // duration of the video...
                    playlistID = information.playlistID;                                                            // unique playlist id...
                    string playlistIndex = information.playlistIndex;                                               // index of the video in it's playlist...
                    playlistTitle = information.playlistTitle;                                                      // title of the playlist...
                    string resolution    = information.resolution;                                                  // video quality (e.g. 4K, 2K, Full HD, HD etc)...
                    string fileType      = information.fileType;                                                    // if the information is about the DASH audio file or video file...
                    string fileSize      = information.fileSize;                                                    // size of the file...
                    string fileExtension = information.fileExtension;                                               // file extension...
                    string downloadLink  = information.downloadLink;                                                // download link of that video...

                    Uri thumbnailUri = youtube.GetThumbnailUri(information.videoID, YouTube.ThumbnailQuality.High); // there are 3 qualities (Low, Medium, High) available for thumbnail...

                    string thumbnailURL = thumbnailUri.ToString();                                                  // to get the URL of the thumbnail...

                    Console.WriteLine(information + "\n");                                                          // also you can just simply print the 'information' object...
                }

                break;

            case YouTube.InputType.PlaylistURL:
                playlistID = youtube.GetPlaylistID(input);                          // unique playlist id from PlaylistURL...
                string playlistPage = youtube.GetPlaylistPage(playlistID);          // downloads web page of the playlist as string...
                playlistTitle = youtube.GetPlaylistTitle(playlistPage);             // parses title of the playlist...
                int playlistVideoCount = youtube.CountPlaylistVideos(playlistPage); // parses the number of videos in a playlist...

                Console.WriteLine("Playlist Title = " + playlistTitle + "\nVideo(s) = " + playlistVideoCount + "\n");

                foreach (string url in youtube.PlaylistURLs(playlistID))            // you will get URLs of all the videos of the playlist...
                {
                    Console.WriteLine(url);
                }

                Console.WriteLine("\n");

                foreach (Information information in youtube.PlaylistInformations(playlistID))
                {
                    /*
                     *
                     * you can get all the information from 'information' object
                     * just like the previous example...
                     *
                     */

                    Console.WriteLine(information + "\n");         // also you can just simply print the 'information' object...
                }

                break;

            case YouTube.InputType.SearchQuery:
                int page = 3;                                           // page must be greater than zero...
                                                                        // here we set 'page = 3' because we want to get the search results from the third page...

                foreach (string videoID in youtube.Search(page, input)) // you will get URLs of all the videos from the third page of the search result...
                {
                    Console.WriteLine(youtube.ToVideoURL(videoID));
                }

                break;

            case YouTube.InputType.Invalid:
                Console.WriteLine("Invalid input detected...");

                break;
            }

            Console.ReadKey();
        }