优化Linq写法

This commit is contained in:
nilaoda 2022-07-13 12:04:21 +08:00
parent 802bf56ede
commit 71988ddbca
1 changed files with 20 additions and 23 deletions

View File

@ -33,10 +33,10 @@ namespace N_m3u8DL_RE.Parser.Extractor
private string ExtendBaseUrl(XElement element, string oriBaseUrl) private string ExtendBaseUrl(XElement element, string oriBaseUrl)
{ {
var target = element.Elements().Where(e => e.Name.LocalName == "BaseURL"); var target = element.Elements().Where(e => e.Name.LocalName == "BaseURL").FirstOrDefault();
if (target.Any()) if (target != null)
{ {
oriBaseUrl = ParserUtil.CombineURL(oriBaseUrl, target.First().Value); oriBaseUrl = ParserUtil.CombineURL(oriBaseUrl, target.Value);
} }
return oriBaseUrl; return oriBaseUrl;
@ -83,10 +83,10 @@ namespace N_m3u8DL_RE.Parser.Extractor
var mediaPresentationDuration = mpdElement.Attribute("mediaPresentationDuration")?.Value; var mediaPresentationDuration = mpdElement.Attribute("mediaPresentationDuration")?.Value;
//读取在MPD开头定义的<BaseURL>并替换本身的URL //读取在MPD开头定义的<BaseURL>并替换本身的URL
var baseUrlElements = mpdElement.Elements().Where(e => e.Name.LocalName == "BaseURL"); var baseUrlElement = mpdElement.Elements().Where(e => e.Name.LocalName == "BaseURL").FirstOrDefault();
if (baseUrlElements.Any()) if (baseUrlElement != null)
{ {
var baseUrl = baseUrlElements.First().Value; var baseUrl = baseUrlElement.Value;
if (baseUrl.Contains("kkbox.com.tw/")) baseUrl = baseUrl.Replace("//https:%2F%2F", "//"); if (baseUrl.Contains("kkbox.com.tw/")) baseUrl = baseUrl.Replace("//https:%2F%2F", "//");
this.BaseUrl = ParserUtil.CombineURL(this.MpdUrl, baseUrl); this.BaseUrl = ParserUtil.CombineURL(this.MpdUrl, baseUrl);
} }
@ -158,22 +158,21 @@ namespace N_m3u8DL_RE.Parser.Extractor
} }
//读取声道数量 //读取声道数量
var audioChannelConfiguration = adaptationSet.Elements().Concat(representation.Elements()).Where(e => e.Name.LocalName == "AudioChannelConfiguration"); var audioChannelConfiguration = adaptationSet.Elements().Concat(representation.Elements()).Where(e => e.Name.LocalName == "AudioChannelConfiguration").FirstOrDefault();
if (audioChannelConfiguration.Any()) if (audioChannelConfiguration != null)
{ {
streamSpec.Channels = audioChannelConfiguration.First().Attribute("value")?.Value; streamSpec.Channels = audioChannelConfiguration.Attribute("value")?.Value;
} }
//第一种形式 SegmentBase //第一种形式 SegmentBase
var segmentBaseElements = representation.Elements().Where(e => e.Name.LocalName == "SegmentBase"); var segmentBaseElement = representation.Elements().Where(e => e.Name.LocalName == "SegmentBase").FirstOrDefault();
if (segmentBaseElements.Any()) if (segmentBaseElement != null)
{ {
//处理init url //处理init url
var initializationElements = segmentBaseElements.First().Elements().Where(e => e.Name.LocalName == "Initialization"); var initialization = segmentBaseElement.Elements().Where(e => e.Name.LocalName == "Initialization").FirstOrDefault();
if (initializationElements.Any()) if (initialization != null)
{ {
var initialization = initializationElements.First();
var sourceURL = initialization.Attribute("sourceURL")?.Value; var sourceURL = initialization.Attribute("sourceURL")?.Value;
if (sourceURL == null) if (sourceURL == null)
{ {
@ -204,16 +203,14 @@ namespace N_m3u8DL_RE.Parser.Extractor
} }
//第二种形式 SegmentList.SegmentList //第二种形式 SegmentList.SegmentList
var segmentListElements = representation.Elements().Where(e => e.Name.LocalName == "SegmentList"); var segmentList = representation.Elements().Where(e => e.Name.LocalName == "SegmentList").FirstOrDefault();
if (segmentListElements.Any()) if (segmentList != null)
{ {
var segmentList = segmentListElements.First();
var duration = segmentList.Attribute("duration")?.Value; var duration = segmentList.Attribute("duration")?.Value;
//处理init url //处理init url
var initializationElements = segmentList.Elements().Where(e => e.Name.LocalName == "Initialization"); var initialization = segmentList.Elements().Where(e => e.Name.LocalName == "Initialization").FirstOrDefault();
if (initializationElements.Any()) if (initialization != null)
{ {
var initialization = initializationElements.First();
var initUrl = ParserUtil.CombineURL(segBaseUrl, initialization.Attribute("sourceURL")?.Value); var initUrl = ParserUtil.CombineURL(segBaseUrl, initialization.Attribute("sourceURL")?.Value);
var initRange = initialization.Attribute("range")?.Value; var initRange = initialization.Attribute("range")?.Value;
streamSpec.Playlist.MediaInit = new MediaSegment(); streamSpec.Playlist.MediaInit = new MediaSegment();
@ -255,6 +252,7 @@ namespace N_m3u8DL_RE.Parser.Extractor
var segmentTemplateElements = representation.Elements().Where(e => e.Name.LocalName == "SegmentTemplate"); var segmentTemplateElements = representation.Elements().Where(e => e.Name.LocalName == "SegmentTemplate");
if (segmentTemplateElements.Any() || segmentTemplateElementsOuter.Any()) if (segmentTemplateElements.Any() || segmentTemplateElementsOuter.Any())
{ {
//优先使用最近的元素
var segmentTemplate = segmentTemplateElements.FirstOrDefault() ?? segmentTemplateElementsOuter.FirstOrDefault(); var segmentTemplate = segmentTemplateElements.FirstOrDefault() ?? segmentTemplateElementsOuter.FirstOrDefault();
var segmentTemplateOuter = segmentTemplateElementsOuter.FirstOrDefault() ?? segmentTemplateElements.FirstOrDefault(); var segmentTemplateOuter = segmentTemplateElementsOuter.FirstOrDefault() ?? segmentTemplateElements.FirstOrDefault();
var varDic = new Dictionary<string, object?>(); var varDic = new Dictionary<string, object?>();
@ -271,11 +269,10 @@ namespace N_m3u8DL_RE.Parser.Extractor
streamSpec.Playlist.MediaInit.Url = PreProcessUrl(initUrl); streamSpec.Playlist.MediaInit.Url = PreProcessUrl(initUrl);
//处理分片 //处理分片
var media = segmentTemplate.Attribute("media")?.Value ?? segmentTemplateOuter.Attribute("media")?.Value; var media = segmentTemplate.Attribute("media")?.Value ?? segmentTemplateOuter.Attribute("media")?.Value;
var segmentTimelineElements = segmentTemplate.Elements().Where(e => e.Name.LocalName == "SegmentTimeline"); var segmentTimeline = segmentTemplate.Elements().Where(e => e.Name.LocalName == "SegmentTimeline").FirstOrDefault();
if (segmentTimelineElements.Any()) if (segmentTimeline != null)
{ {
//使用了SegmentTimeline 结果精确 //使用了SegmentTimeline 结果精确
var segmentTimeline = segmentTimelineElements.First();
var segNumber = Convert.ToInt32(startNumberStr); var segNumber = Convert.ToInt32(startNumberStr);
var Ss = segmentTimeline.Elements().Where(e => e.Name.LocalName == "S"); var Ss = segmentTimeline.Elements().Where(e => e.Name.LocalName == "S");
var currentTime = 0L; var currentTime = 0L;