Example #1
0
        public static async Task <BiliVideo> GetSingleVideoAsync(string bv, long cid, int quality, string sESSDATA)
        {
            var master = await GetVideoMasterInfoAsync(bv, sESSDATA);

            List <(string, string)> cookies = null;

            if (!string.IsNullOrWhiteSpace(sESSDATA))
            {
                cookies = new List <(string, string)>();
                cookies.Add(("SESSDATA", sESSDATA));
            }
            if (quality == 0)
            {
                quality = 64;
            }

            var json = JsonConvert.DeserializeObject <VideoStreamJson>
                           (await NetHelper.HttpGet("http://api.bilibili.com/x/player/playurl",
                                                    cookies, $"bvid={bv}", $"cid={cid}", $"qn={quality}", "fnval=16"));

            if (json.data == null)
            {
                throw new ParsingVideoException("视频解析失败,请检查是否缺少大会员权限");
            }

            var video = new BiliVideo();

            video.Bv      = bv;
            video.Cid     = cid;
            video.Name    = master.VideoList.Where(v => v.Cid == cid).FirstOrDefault().Name;
            video.P       = master.VideoList.Where(v => v.Cid == cid).FirstOrDefault().P;
            video.Quality = (BiliVideoQuality)json.data.quality;
            video.Title   = master.Title;

            if ((bool)ApplicationData.Current.LocalSettings.Values["HEVC"])//优先使用hevc
            {
                if (json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("hev")).Count() > 0)
                {
                    video.VideoUrl = json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("hev")).FirstOrDefault().baseUrl;
                }
                else if (json.data.dash.video.Where(v => v.codecs.Contains("hev")).Count() > 0)
                {
                    var list = json.data.dash.video.Where(v => v.codecs.Contains("hev")).OrderByDescending(v => v.id).ToList();
                    video.VideoUrl = list.First().baseUrl;
                }
                else if (json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("avc")).Count() > 0)
                {
                    video.VideoUrl = json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("avc")).FirstOrDefault().baseUrl;
                }
                else
                {
                    var list = json.data.dash.video.Where(v => v.codecs.Contains("avc")).OrderByDescending(v => v.id).ToList();
                    video.VideoUrl = list.First().baseUrl;
                }
            }
            else
            {
                if (json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("avc")).Count() > 0)
                {
                    video.VideoUrl = json.data.dash.video.Where(v => v.id == quality && v.codecs.Contains("avc")).FirstOrDefault().baseUrl;
                }
                else
                {
                    var list = json.data.dash.video.Where(v => v.codecs.Contains("avc")).OrderByDescending(v => v.id).ToList();
                    video.VideoUrl = list.First().baseUrl;
                }
            }

            if (json.data.dash.audio.Where(v => v.id == 30280).Count() > 0)
            {
                video.AudioUrl = json.data.dash.audio.Where(v => v.id == 30280).FirstOrDefault().baseUrl;
            }
            else
            {
                var list = json.data.dash.audio.OrderByDescending(v => v.id).ToList();
                video.AudioUrl = list.First().baseUrl;
            }

            return(video);
        }