Ocena użytkowników: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Plików:
VideoDownloader
(1 vote)
Data 15-04-2015 13:52:07 Wielkość 11.99 KB Download 3 Pobierz

 

YouTube VideoDownloader


Posłużę się następującym linkiem : http://www.youtube.com/watch?v=0uuYicF9eUg

Na początek potrzebny będzie VideoID z linku źródłowego :

NameValueCollection queryCollection = HttpUtility.ParseQueryString
(new Uri(this.YouTubeLink).Query);
if (queryCollection.AllKeys.Contains("v"))
    VideoID = queryCollection["v"];

 

Mając VideoID="0uuYicF9eUg" możemy pobrać informację o pobieranym filmiku, korzystając z poniższego linku.

private const string GetVideoInfoUrl = "http://www.youtube.com/get_video_info?&video_id={0}"; 
        private bool GetVideoInfo()
        {
            try
            {
                string videoInfoPageSource = DownloadPage(string.Format(GetVideoInfoUrl, VideoID));

                if (String.IsNullOrEmpty(videoInfoPageSource) || String.IsNullOrWhiteSpace(videoInfoPageSource))
                    return false;

                this.videoInfoCollection = HttpUtility.ParseQueryString(videoInfoPageSource);

                this.VideoTitle = videoInfoCollection["title"];
                this.VideoTime = videoInfoCollection["length_seconds"];
                this.Thumbinal = videoInfoCollection["thumbnail_url"];

                return true;
            }
            catch { return false; }
        }

W tym momencie mamy już "Tytuł", "Czas trwania (w sekundach)", oraz link do obrazu miniatury.

 

Lista dostępnych formatów video.

            string availableSizes = this.videoInfoCollection["fmt_list"];

            List<string> size_list = new List<string>(availableSizes.Split(",".ToCharArray()));
            foreach (string format in size_list)
            {
                if (string.IsNullOrEmpty(format.Trim()))
                    continue;
                string[] q = format.Split("/".ToCharArray());
                VideoSizes.Add(byte.Parse(q[0]), q[1]);
            };

 

Teraz czas wybrać linki do dostępnych formatów video.

            string availableUrls = this.videoInfoCollection["url_encoded_fmt_stream_map"];

            if (availableUrls == string.Empty) { return false; }

            List<string> urlsList = new List<string>(availableUrls.Split(",".ToCharArray()));

            foreach (string format in urlsList)
            {
                if (string.IsNullOrEmpty(format.Trim()))
                   continue;
                NameValueCollection formatInfoCollection = HttpUtility.ParseQueryString(format);
                string itag = formatInfoCollection["itag"];

                byte formatCode = byte.Parse(itag);
                string vSize = VideoSizes[formatCode];

                string urlEncoded = string.Format("{0}&fallback_host={1}&signature={2}",
                    new object[] 
                        {
                            formatInfoCollection["url"],
                            formatInfoCollection["fallback_host"],
                            formatInfoCollection["sig"]
                        });

                Uri url = new Uri(HttpUtility.UrlDecode(HttpUtility.UrlDecode(urlEncoded)));

                DownloadLinks.Add(
                    new VideoInfo
                    {
                        Title = videoInfoCollection["title"],
                        VideoSize = vSize,
                        VideoTime = videoInfoCollection["length_seconds"],
                        FormatCode = formatCode,
                        DownloadUrl = url.AbsoluteUri,
                    }
                );
            };

 

Rozmiar pliku filmiku dla każdego z dostępnych formatów trzeba już pobrać indywidualnie dla każdego linka. Poprzez odwołanie się do adresu i pobrania wielkości kontekstu.

 

Ocena użytkowników: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Korzystając z komponentu WebBrowser często mamy dźwięk (klik) po przeładowaniu strony. Nie zawsze jest to przyjemne i czasami wypada go wyłączyć.

Możemy to zrobić korzystając z poniższej metody (Microsoft)

    #region URL sound disable - microsoft

    /// <summary>
    /// Enables or disables a specified Internet Explorer feature control
    /// Minimum availability: Internet Explorer 6.0
    /// Minimum operating systems: Windows XP SP2
    /// </summary>
    public class URLSecurityZoneAPI
    {
        /// <summary>
        /// Specifies where to set the feature control value
        /// http://msdn.microsoft.com/en-us/library/ms537168%28VS.85%29.aspx
        /// </summary>
        public enum SetFeatureOn : int
        {
            THREAD = 0x00000001,
            PROCESS = 0x00000002,
            REGISTRY = 0x00000004,
            THREAD_LOCALMACHINE = 0x00000008,
            THREAD_INTRANET = 0x00000010,
            THREAD_TRUSTED = 0x00000020,
            THREAD_INTERNET = 0x00000040,
            THREAD_RESTRICTED = 0x00000080
        }

        /// <summary>
        /// InternetFeaturelist
        /// http://msdn.microsoft.com/en-us/library/ms537169%28v=VS.85%29.aspx
        /// </summary>
        public enum InternetFeaturelist : int
        {
            OBJECT_CACHING = 0,
            ZONE_ELEVATION = 1,
            MIME_HANDLING = 2,
            MIME_SNIFFING = 3,
            WINDOW_RESTRICTIONS = 4,
            WEBOC_POPUPMANAGEMENT = 5,
            BEHAVIORS = 6,
            DISABLE_MK_PROTOCOL = 7,
            LOCALMACHINE_LOCKDOWN = 8,
            SECURITYBAND = 9,
            RESTRICT_ACTIVEXINSTALL = 10,
            VALIDATE_NAVIGATE_URL = 11,
            RESTRICT_FILEDOWNLOAD = 12,
            ADDON_MANAGEMENT = 13,
            PROTOCOL_LOCKDOWN = 14,
            HTTP_USERNAME_PASSWORD_DISABLE = 15,
            SAFE_BINDTOOBJECT = 16,
            UNC_SAVEDFILECHECK = 17,
            GET_URL_DOM_FILEPATH_UNENCODED = 18,
            TABBED_BROWSING = 19,
            SSLUX = 20,
            DISABLE_NAVIGATION_SOUNDS = 21,
            DISABLE_LEGACY_COMPRESSION = 22,
            FORCE_ADDR_AND_STATUS = 23,
            XMLHTTP = 24,
            DISABLE_TELNET_PROTOCOL = 25,
            FEEDS = 26,
            BLOCK_INPUT_PROMPTS = 27,
            MAX = 28
        }

        /// <summary>
        /// Enables or disables a specified feature control.
        /// http://msdn.microsoft.com/en-us/library/ms537168%28VS.85%29.aspx
        /// </summary>
        [DllImport("urlmon.dll", ExactSpelling = true), PreserveSig, SecurityCritical, SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Error)]
        private static extern int CoInternetSetFeatureEnabled(int featureEntry, [MarshalAs(UnmanagedType.U4)] int dwFlags, bool fEnable);

        /// <summary>
        /// Determines whether the specified feature control is enabled.
        /// http://msdn.microsoft.com/en-us/library/ms537164%28v=VS.85%29.aspx
        /// </summary>
        [DllImport("urlmon.dll", ExactSpelling = true), PreserveSig, SecurityCritical, SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Error)]
        private static extern int CoInternetIsFeatureEnabled(int featureEntry, int dwFlags);

        /// <summary>
        /// Set the internet feature enabled/disabled
        /// </summary>
        /// <param name="feature">The feature from <c>InternetFeaturelist</c></param>
        /// <param name="target">The target from <c>SetFeatureOn</c></param>
        /// <param name="enabled">enabled the feature?</param>
        /// <returns><c>true</c> if [is internet set feature enabled] [the specified feature]; otherwise, <c>false</c>.</returns>
        public static bool InternetSetFeatureEnabled(InternetFeaturelist feature, SetFeatureOn target, bool enabled)
        {
            return (CoInternetSetFeatureEnabled((int)feature, (int)target, enabled) == 0);
        }

        /// <summary>
        /// Determines whether the internet feature is enabled.
        /// </summary>
        /// <param name="feature">The feature from <c>InternetFeaturelist</c></param>
        /// <param name="target">The target from <c>SetFeatureOn</c></param>
        /// <returns><c>true</c> if the internet feature is enabled; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsInternetSetFeatureEnabled(InternetFeaturelist feature, SetFeatureOn target)
        {
            return (CoInternetIsFeatureEnabled((int)feature, (int)target) == 0);
        }
    }

    #endregion URL sound disable - microsoft

A następnie wywołać w następujący sposób :

URLSecurityZoneAPI.InternetSetFeatureEnabled(
                URLSecurityZoneAPI.InternetFeaturelist.DISABLE_NAVIGATION_SOUNDS,
                URLSecurityZoneAPI.SetFeatureOn.PROCESS, true);