Skip to content

Commit

Permalink
修复直播过滤已下载片段的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Oct 17, 2022
1 parent 3296328 commit 9ad3b9e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/N_m3u8DL-RE.Common/Resource/ResString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class ResString
public static string cmd_muxAfterDone_more { get => GetText("cmd_muxAfterDone_more"); }
public static string cmd_writeMetaJson { get => GetText("cmd_writeMetaJson"); }
public static string liveLimit { get => GetText("liveLimit"); }
public static string realTimeDecMessage { get => GetText("realTimeDecMessage"); }
public static string liveLimitReached { get => GetText("liveLimitReached"); }
public static string saveName { get => GetText("saveName"); }
public static string partMerge { get => GetText("partMerge"); }
Expand Down
6 changes: 6 additions & 0 deletions src/N_m3u8DL-RE.Common/Resource/StaticText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ internal class StaticText
zhTW: "本次直播錄製時長上限: ",
enUS: "Live recording duration limit: "
),
["realTimeDecMessage"] = new TextContainer
(
zhCN: "启用实时解密时,建议用shaka-packager而非mp4decrypt",
zhTW: "啟用即時解密時,建議用shaka-packager而非mp4decrypt",
enUS: "When enabling real-time decryption, it is recommended to use shaka-packager instead of mp4decrypt"
),
["liveLimitReached"] = new TextContainer
(
zhCN: "到达直播录制上限,即将停止录制",
Expand Down
2 changes: 1 addition & 1 deletion src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public async Task FetchPlayListAsync(List<StreamSpec> lists)
}
}
}
else if (lists.Count == 1 && ParserConfig.OriginalUrl != lists[0].Url)
else if (!MasterM3u8Flag && lists.Count == 1 && ParserConfig.OriginalUrl != lists[0].Url)
{
//单m3u8, 但是发生了重定向, 则应从原始URL开始解析
lists[0].Url = ParserConfig.OriginalUrl;
Expand Down
4 changes: 4 additions & 0 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ public async Task<bool> StartDownloadAsync(IEnumerable<StreamSpec> streamSpecs)
new SpinnerColumn(),
});

if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !DownloaderConfig.MyOptions.UseShakaPackager
&& DownloaderConfig.MyOptions.Keys != null && DownloaderConfig.MyOptions.Keys.Length > 0)
Logger.WarnMarkUp($"[darkorange3_1]{ResString.realTimeDecMessage}[/]");

await progress.StartAsync(async ctx =>
{
//创建任务
Expand Down
26 changes: 14 additions & 12 deletions src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class SimpleLiveRecordManager2
bool STOP_FLAG = false;
int WAIT_SEC = 0; //刷新间隔
ConcurrentDictionary<int, int> RecordingDurDic = new(); //已录制时长
ConcurrentDictionary<string, string> LastUrlDic = new(); //上次下载的url
ConcurrentDictionary<string, string> LastFileNameDic = new(); //上次下载的文件名
ConcurrentDictionary<string, long> DateTimeDic = new(); //上次下载的dateTime
CancellationTokenSource CancellationTokenSource = new(); //取消Wait

Expand Down Expand Up @@ -178,6 +178,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
//TryReceiveAll可以稍微缓解一下
source.TryReceiveAll(out IList<List<MediaSegment>>? segmentsList);
var segments = segmentsList!.SelectMany(s => s);
Logger.DebugMarkUp(string.Join(",", segments.Select(sss => GetSegmentName(sss, false))));

//下载init
if (!initDownloaded && streamSpec.Playlist?.MediaInit != null)
Expand Down Expand Up @@ -529,15 +530,16 @@ private async Task PlayListProduceAsync(StreamSpec streamSpec, ProgressTask task
{
if (WAIT_SEC != 0)
{
var allHasDatetime = streamSpec.Playlist!.MediaParts[0].MediaSegments.All(s => s.DateTime != null);
//过滤不需要下载的片段
FilterMediaSegments(streamSpec, LastUrlDic[streamSpec.ToShortString()], DateTimeDic[streamSpec.ToShortString()]);
FilterMediaSegments(streamSpec, allHasDatetime);
var newList = streamSpec.Playlist!.MediaParts[0].MediaSegments;
if (newList.Count > 0)
{
//推送给消费者
await target.SendAsync(newList);
//更新最新链接
LastUrlDic[streamSpec.ToShortString()] = GetPath(newList.Last().Url);
LastFileNameDic[streamSpec.ToShortString()] = GetSegmentName(newList.Last(), allHasDatetime);
//尝试更新时间戳
var dt = newList.Last().DateTime;
DateTimeDic[streamSpec.ToShortString()] = dt != null ? GetUnixTimestamp(dt.Value) : 0L;
Expand All @@ -560,11 +562,13 @@ private async Task PlayListProduceAsync(StreamSpec streamSpec, ProgressTask task
target.Complete();
}

private void FilterMediaSegments(StreamSpec streamSpec, string lastUrl, long dateTime)
private void FilterMediaSegments(StreamSpec streamSpec, bool allHasDatetime)
{
if (string.IsNullOrEmpty(lastUrl) && dateTime == 0) return;
if (string.IsNullOrEmpty(LastFileNameDic[streamSpec.ToShortString()]) && DateTimeDic[streamSpec.ToShortString()] == 0) return;

var index = -1;
var dateTime = DateTimeDic[streamSpec.ToShortString()];
var lastName = LastFileNameDic[streamSpec.ToShortString()];

//优先使用dateTime判断
if (dateTime != 0 && streamSpec.Playlist!.MediaParts[0].MediaSegments.All(s => s.DateTime != null))
Expand All @@ -573,7 +577,7 @@ private void FilterMediaSegments(StreamSpec streamSpec, string lastUrl, long dat
}
else
{
index = streamSpec.Playlist!.MediaParts[0].MediaSegments.FindIndex(s => GetPath(s.Url) == lastUrl);
index = streamSpec.Playlist!.MediaParts[0].MediaSegments.FindIndex(s => GetSegmentName(s, allHasDatetime) == lastName);
}

if (index > -1)
Expand All @@ -582,11 +586,6 @@ private void FilterMediaSegments(StreamSpec streamSpec, string lastUrl, long dat
}
}

private string GetPath(string url)
{
return url.Split('?').First();
}

public async Task<bool> StartRecordAsync()
{
var takeLastCount = 15;
Expand All @@ -604,7 +603,7 @@ public async Task<bool> StartRecordAsync()
//初始化dic
foreach (var item in SelectedSteams)
{
LastUrlDic[item.ToShortString()] = "";
LastFileNameDic[item.ToShortString()] = "";
DateTimeDic[item.ToShortString()] = 0L;
}
//设置等待时间
Expand Down Expand Up @@ -645,6 +644,9 @@ await progress.StartAsync(async ctx =>
DownloaderConfig.MyOptions.ConcurrentDownload = true;
DownloaderConfig.MyOptions.MP4RealTimeDecryption = true;
DownloaderConfig.MyOptions.LiveRecordLimit = DownloaderConfig.MyOptions.LiveRecordLimit ?? TimeSpan.MaxValue;
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !DownloaderConfig.MyOptions.UseShakaPackager
&& DownloaderConfig.MyOptions.Keys != null && DownloaderConfig.MyOptions.Keys.Length > 0)
Logger.WarnMarkUp($"[darkorange3_1]{ResString.realTimeDecMessage}[/]");
var limit = DownloaderConfig.MyOptions.LiveRecordLimit;
if (limit != TimeSpan.MaxValue)
Logger.WarnMarkUp($"[darkorange3_1]{ResString.liveLimit}{GlobalUtil.FormatTime((int)limit.Value.TotalSeconds)}[/]");
Expand Down
2 changes: 1 addition & 1 deletion src/N_m3u8DL-RE/N_m3u8DL-RE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<Version>0.0.4</Version>
<Version>0.0.5</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 9ad3b9e

Please sign in to comment.