Пример #1
0
        public R <PlayResource> GetResourceById(AudioResource resource)
        {
            if (!WebWrapper.DownloadString(out string resulthtml, new Uri($"http://www.youtube.com/get_video_info?video_id={resource.ResourceId}&el=info")))
            {
                return(RResultCode.NoConnection.ToString());
            }

            var videoTypes = new List <VideoData>();
            NameValueCollection dataParse = HttpUtility.ParseQueryString(resulthtml);

            string videoDataUnsplit = dataParse["url_encoded_fmt_stream_map"];

            if (videoDataUnsplit != null)
            {
                string[] videoData = videoDataUnsplit.Split(',');

                foreach (string vdat in videoData)
                {
                    NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

                    string vLink = videoparse["url"];
                    if (vLink == null)
                    {
                        continue;
                    }

                    string vType = videoparse["type"];
                    if (vType == null)
                    {
                        continue;
                    }

                    string vQuality = videoparse["quality"];
                    if (vQuality == null)
                    {
                        continue;
                    }

                    var vt = new VideoData()
                    {
                        Link              = vLink,
                        Codec             = GetCodec(vType),
                        Qualitydesciption = vQuality
                    };
                    videoTypes.Add(vt);
                }
            }

            videoDataUnsplit = dataParse["adaptive_fmts"];
            if (videoDataUnsplit != null)
            {
                string[] videoData = videoDataUnsplit.Split(',');

                foreach (string vdat in videoData)
                {
                    NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

                    string vType = videoparse["type"];
                    if (vType == null)
                    {
                        continue;
                    }

                    bool audioOnly = false;
                    if (vType.StartsWith("video/", StringComparison.Ordinal))
                    {
                        continue;
                    }
                    else if (vType.StartsWith("audio/", StringComparison.Ordinal))
                    {
                        audioOnly = true;
                    }

                    string vLink = videoparse["url"];
                    if (vLink == null)
                    {
                        continue;
                    }

                    var vt = new VideoData()
                    {
                        Codec             = GetCodec(vType),
                        Qualitydesciption = vType,
                        Link = vLink
                    };
                    if (audioOnly)
                    {
                        vt.AudioOnly = true;
                    }
                    else
                    {
                        vt.VideoOnly = true;
                    }
                    videoTypes.Add(vt);
                }
            }

            // Validation Process

            if (videoTypes.Count <= 0)
            {
                return(RResultCode.YtNoVideosExtracted.ToString());
            }

            int codec = SelectStream(videoTypes);

            if (codec < 0)
            {
                return("No playable codec found");
            }

            var result = ValidateMedia(videoTypes[codec]);

            if (!result)
            {
                if (string.IsNullOrWhiteSpace(data.YoutubedlPath))
                {
                    return(result.Message);
                }

                return(YoutubeDlWrapped(resource));
            }

            return(new PlayResource(videoTypes[codec].Link, resource.ResourceTitle != null ? resource : resource.WithName(dataParse["title"] ?? $"<YT - no title : {resource.ResourceTitle}>")));
        }
Пример #2
0
		public RResultCode GetResourceById(string ytID, string name, out AudioResource result)
		{
			string resulthtml;
			if (!WebWrapper.DownloadString(out resulthtml, new Uri($"http://www.youtube.com/get_video_info?video_id={ytID}&el=info")))
			{
				result = null;
				return RResultCode.NoConnection;
			}

			var videoTypes = new List<VideoData>();
			NameValueCollection dataParse = HttpUtility.ParseQueryString(resulthtml);

			string videoDataUnsplit = dataParse["url_encoded_fmt_stream_map"];
			if (videoDataUnsplit != null)
			{
				string[] videoData = videoDataUnsplit.Split(',');

				foreach (string vdat in videoData)
				{
					NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

					string vLink = videoparse["url"];
					if (vLink == null)
						continue;

					string vType = videoparse["type"];
					if (vType == null)
						continue;

					string vQuality = videoparse["quality"];
					if (vQuality == null)
						continue;

					var vt = new VideoData();
					vt.link = vLink;
					vt.codec = GetCodec(vType);
					vt.qualitydesciption = vQuality;
					videoTypes.Add(vt);
				}
			}

			videoDataUnsplit = dataParse["adaptive_fmts"];
			if (videoDataUnsplit != null)
			{
				string[] videoData = videoDataUnsplit.Split(',');

				foreach (string vdat in videoData)
				{
					NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

					string vType = videoparse["type"];
					if (vType == null)
						continue;

					bool audioOnly = false;
					if (vType.StartsWith("video/"))
						continue;
					else if (vType.StartsWith("audio/"))
						audioOnly = true;

					string vLink = videoparse["url"];
					if (vLink == null)
						continue;

					var vt = new VideoData();
					vt.codec = GetCodec(vType);
					vt.qualitydesciption = vType;
					vt.link = vLink;
					if (audioOnly)
						vt.audioOnly = true;
					else
						vt.videoOnly = true;
					videoTypes.Add(vt);
				}
			}

			string finalName = name ?? dataParse["title"] ?? $"<YT - no title : {ytID}>";
			var ytResult = new YoutubeResource(ytID, finalName, videoTypes);
			result = ytResult;
			return ytResult.AvailableTypes.Count > 0 ? RResultCode.Success : RResultCode.YtNoVideosExtracted;
		}
Пример #3
0
 private static E <LocalStr> ValidateMedia(VideoData media) => WebWrapper.GetResponse(new Uri(media.Link), TimeSpan.FromSeconds(3));