优化变量替换和组装URL的顺序

This commit is contained in:
nilaoda 2022-12-21 18:37:53 +08:00
parent a8bc97841e
commit 418547d5a2
2 changed files with 16 additions and 12 deletions

View File

@ -312,13 +312,14 @@ namespace N_m3u8DL_RE.Parser.Extractor
var initialization = segmentTemplate.Attribute("initialization")?.Value ?? segmentTemplateOuter.Attribute("initialization")?.Value; var initialization = segmentTemplate.Attribute("initialization")?.Value ?? segmentTemplateOuter.Attribute("initialization")?.Value;
if (initialization != null) if (initialization != null)
{ {
var initUrl = ParserUtil.ReplaceVars(ParserUtil.CombineURL(segBaseUrl, initialization), varDic); var _init = ParserUtil.ReplaceVars(initialization, varDic);
var initUrl = ParserUtil.CombineURL(segBaseUrl, _init);
streamSpec.Playlist.MediaInit = new MediaSegment(); streamSpec.Playlist.MediaInit = new MediaSegment();
streamSpec.Playlist.MediaInit.Index = -1; //便于排序 streamSpec.Playlist.MediaInit.Index = -1; //便于排序
streamSpec.Playlist.MediaInit.Url = initUrl; streamSpec.Playlist.MediaInit.Url = initUrl;
} }
//处理分片 //处理分片
var media = segmentTemplate.Attribute("media")?.Value ?? segmentTemplateOuter.Attribute("media")?.Value; var mediaTemplate = segmentTemplate.Attribute("media")?.Value ?? segmentTemplateOuter.Attribute("media")?.Value;
var segmentTimeline = segmentTemplate.Elements().Where(e => e.Name.LocalName == "SegmentTimeline").FirstOrDefault(); var segmentTimeline = segmentTemplate.Elements().Where(e => e.Name.LocalName == "SegmentTimeline").FirstOrDefault();
if (segmentTimeline != null) if (segmentTimeline != null)
{ {
@ -340,11 +341,12 @@ namespace N_m3u8DL_RE.Parser.Extractor
var _repeatCount = Convert.ToInt64(_repeatCountStr); var _repeatCount = Convert.ToInt64(_repeatCountStr);
varDic[DASHTags.TemplateTime] = currentTime; varDic[DASHTags.TemplateTime] = currentTime;
varDic[DASHTags.TemplateNumber] = segNumber++; varDic[DASHTags.TemplateNumber] = segNumber++;
var oriUrl = ParserUtil.CombineURL(segBaseUrl, media!); var hasTime = mediaTemplate!.Contains(DASHTags.TemplateTime);
var mediaUrl = ParserUtil.ReplaceVars(oriUrl, varDic); var media = ParserUtil.ReplaceVars(mediaTemplate!, varDic);
var mediaUrl = ParserUtil.CombineURL(segBaseUrl, media!);
MediaSegment mediaSegment = new(); MediaSegment mediaSegment = new();
mediaSegment.Url = mediaUrl; mediaSegment.Url = mediaUrl;
if (oriUrl.Contains(DASHTags.TemplateTime)) if (hasTime)
mediaSegment.NameFromVar = currentTime.ToString(); mediaSegment.NameFromVar = currentTime.ToString();
mediaSegment.Duration = _duration / (double)timescale; mediaSegment.Duration = _duration / (double)timescale;
mediaSegment.Index = segIndex++; mediaSegment.Index = segIndex++;
@ -360,12 +362,13 @@ namespace N_m3u8DL_RE.Parser.Extractor
MediaSegment _mediaSegment = new(); MediaSegment _mediaSegment = new();
varDic[DASHTags.TemplateTime] = currentTime; varDic[DASHTags.TemplateTime] = currentTime;
varDic[DASHTags.TemplateNumber] = segNumber++; varDic[DASHTags.TemplateNumber] = segNumber++;
var _oriUrl = ParserUtil.CombineURL(segBaseUrl, media!); var _hashTime = mediaTemplate!.Contains(DASHTags.TemplateTime);
var _mediaUrl = ParserUtil.ReplaceVars(_oriUrl, varDic); var _media = ParserUtil.ReplaceVars(mediaTemplate!, varDic);
var _mediaUrl = ParserUtil.CombineURL(segBaseUrl, _media);
_mediaSegment.Url = _mediaUrl; _mediaSegment.Url = _mediaUrl;
_mediaSegment.Index = segIndex++; _mediaSegment.Index = segIndex++;
_mediaSegment.Duration = _duration / (double)timescale; _mediaSegment.Duration = _duration / (double)timescale;
if (_oriUrl.Contains(DASHTags.TemplateTime)) if (_hashTime)
_mediaSegment.NameFromVar = currentTime.ToString(); _mediaSegment.NameFromVar = currentTime.ToString();
streamSpec.Playlist.MediaParts[0].MediaSegments.Add(_mediaSegment); streamSpec.Playlist.MediaParts[0].MediaSegments.Add(_mediaSegment);
} }
@ -396,11 +399,12 @@ namespace N_m3u8DL_RE.Parser.Extractor
for (long index = startNumber, segIndex = 0; index < startNumber + totalNumber; index++, segIndex++) for (long index = startNumber, segIndex = 0; index < startNumber + totalNumber; index++, segIndex++)
{ {
varDic[DASHTags.TemplateNumber] = index; varDic[DASHTags.TemplateNumber] = index;
var oriUrl = ParserUtil.CombineURL(segBaseUrl, media!); var hasNumber = mediaTemplate!.Contains(DASHTags.TemplateNumber);
var mediaUrl = ParserUtil.ReplaceVars(oriUrl, varDic); var media = ParserUtil.ReplaceVars(mediaTemplate!, varDic);
var mediaUrl = ParserUtil.CombineURL(segBaseUrl, media!);
MediaSegment mediaSegment = new(); MediaSegment mediaSegment = new();
mediaSegment.Url = mediaUrl; mediaSegment.Url = mediaUrl;
if (oriUrl.Contains(DASHTags.TemplateNumber)) if (hasNumber)
mediaSegment.NameFromVar = index.ToString(); mediaSegment.NameFromVar = index.ToString();
mediaSegment.Index = isLive ? index : segIndex; //直播直接用startNumber mediaSegment.Index = isLive ? index : segIndex; //直播直接用startNumber
mediaSegment.Duration = duration / (double)timescale; mediaSegment.Duration = duration / (double)timescale;

View File

@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine
{ {
internal partial class CommandInvoker internal partial class CommandInvoker
{ {
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20221220"; public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20221221";
[GeneratedRegex("((best|worst)\\d*|all)")] [GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex(); private static partial Regex ForStrRegex();