优化已下载大小统计

This commit is contained in:
nilaoda 2022-12-08 22:45:42 +08:00
parent b9dc0858ee
commit 5d4309c90a
4 changed files with 15 additions and 32 deletions

View File

@ -13,14 +13,12 @@ namespace N_m3u8DL_RE.Column
{ {
internal class DownloadStatusColumn : ProgressColumn internal class DownloadStatusColumn : ProgressColumn
{ {
private ConcurrentDictionary<int, long> DownloadedSizeDic = new();
private ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic { get; set; } private ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic { get; set; }
public Style MyStyle { get; set; } = new Style(foreground: Color.DarkCyan); public Style MyStyle { get; set; } = new Style(foreground: Color.DarkCyan);
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green); public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
public DownloadStatusColumn(ConcurrentDictionary<int, long> downloadedSizeDic, ConcurrentDictionary<int, SpeedContainer> speedContainerDic) public DownloadStatusColumn(ConcurrentDictionary<int, SpeedContainer> speedContainerDic)
{ {
this.DownloadedSizeDic = downloadedSizeDic;
this.SpeedContainerDic = speedContainerDic; this.SpeedContainerDic = speedContainerDic;
} }
@ -28,24 +26,13 @@ namespace N_m3u8DL_RE.Column
{ {
if (task.Value == 0) return new Text("-", MyStyle).RightAligned(); if (task.Value == 0) return new Text("-", MyStyle).RightAligned();
var done = task.IsFinished;
var flag = DownloadedSizeDic.TryGetValue(task.Id, out var size);
var totalSize = flag ? (size / (task.Value / task.MaxValue)) : 0;
//单文件下载汇报进度
var speedContainer = SpeedContainerDic[task.Id]; var speedContainer = SpeedContainerDic[task.Id];
if (!done && speedContainer.SingleSegment && speedContainer.ResponseLength != null)
{
task.MaxValue = (double)speedContainer.ResponseLength;
task.Value = speedContainer.RDownloaded;
size = speedContainer.RDownloaded;
totalSize = (double)speedContainer.ResponseLength;
}
var sizeStr = $"{GlobalUtil.FormatFileSize(flag ? size : 0)}/{GlobalUtil.FormatFileSize(totalSize)}"; var size = speedContainer.RDownloaded;
if (done) sizeStr = GlobalUtil.FormatFileSize(totalSize); var totalSize = speedContainer.SingleSegment ? (speedContainer.ResponseLength ?? 0) : (long)(size / (task.Value / task.MaxValue));
var sizeStr = $"{GlobalUtil.FormatFileSize(size)}/{GlobalUtil.FormatFileSize(totalSize)}";
if (task.IsFinished) sizeStr = GlobalUtil.FormatFileSize(size);
return new Text(sizeStr, MyStyle).RightAligned(); return new Text(sizeStr, MyStyle).RightAligned();
} }
} }

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) 20221206"; public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20221208";
[GeneratedRegex("((best|worst)\\d*|all)")] [GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex(); private static partial Regex ForStrRegex();

View File

@ -98,7 +98,7 @@ namespace N_m3u8DL_RE.DownloadManager
} }
private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer, ConcurrentDictionary<int, long> sizeDic) private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer)
{ {
speedContainer.ResetVars(); speedContainer.ResetVars();
bool useAACFilter = false; //ffmpeg合并flag bool useAACFilter = false; //ffmpeg合并flag
@ -166,7 +166,6 @@ namespace N_m3u8DL_RE.DownloadManager
} }
mp4InitFile = result.ActualFilePath; mp4InitFile = result.ActualFilePath;
task.Increment(1); task.Increment(1);
sizeDic[task.Id] += result.ActualContentLength ?? 0;
//读取mp4信息 //读取mp4信息
if (result != null && result.Success) if (result != null && result.Success)
@ -215,7 +214,6 @@ namespace N_m3u8DL_RE.DownloadManager
throw new Exception("Download first segment failed!"); throw new Exception("Download first segment failed!");
} }
task.Increment(1); task.Increment(1);
sizeDic[task.Id] += result.ActualContentLength ?? 0;
if (result != null && result.Success) if (result != null && result.Success)
{ {
//修复MSS init //修复MSS init
@ -279,7 +277,6 @@ namespace N_m3u8DL_RE.DownloadManager
var result = await Downloader.DownloadSegmentAsync(seg, path, speedContainer, headers); var result = await Downloader.DownloadSegmentAsync(seg, path, speedContainer, headers);
FileDic[seg] = result; FileDic[seg] = result;
task.Increment(1); task.Increment(1);
sizeDic[task.Id] += result.ActualContentLength ?? 0;
//实时解密 //实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID)) if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
{ {
@ -633,7 +630,6 @@ namespace N_m3u8DL_RE.DownloadManager
public async Task<bool> StartDownloadAsync() public async Task<bool> StartDownloadAsync()
{ {
ConcurrentDictionary<int, long> DownloadedSizeDic = new(); //大小计算
ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic = new(); //速度计算 ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic = new(); //速度计算
ConcurrentDictionary<StreamSpec, bool?> Results = new(); ConcurrentDictionary<StreamSpec, bool?> Results = new();
@ -645,7 +641,7 @@ namespace N_m3u8DL_RE.DownloadManager
new TaskDescriptionColumn() { Alignment = Justify.Left }, new TaskDescriptionColumn() { Alignment = Justify.Left },
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn(), new PercentageColumn(),
new DownloadStatusColumn(DownloadedSizeDic, SpeedContainerDic), new DownloadStatusColumn(SpeedContainerDic),
new DownloadSpeedColumn(SpeedContainerDic), //速度计算 new DownloadSpeedColumn(SpeedContainerDic), //速度计算
new RemainingTimeColumn(), new RemainingTimeColumn(),
new SpinnerColumn(), new SpinnerColumn(),
@ -663,7 +659,6 @@ namespace N_m3u8DL_RE.DownloadManager
var description = item.ToShortShortString(); var description = item.ToShortShortString();
var task = ctx.AddTask(description, autoStart: false); var task = ctx.AddTask(description, autoStart: false);
SpeedContainerDic[task.Id] = new SpeedContainer(); //速度计算 SpeedContainerDic[task.Id] = new SpeedContainer(); //速度计算
DownloadedSizeDic[task.Id] = 0; //大小计算
return (item, task); return (item, task);
}).ToDictionary(item => item.item, item => item.task); }).ToDictionary(item => item.item, item => item.task);
@ -673,7 +668,7 @@ namespace N_m3u8DL_RE.DownloadManager
foreach (var kp in dic) foreach (var kp in dic)
{ {
var task = kp.Value; var task = kp.Value;
var result = await DownloadStreamAsync(kp.Key, task, SpeedContainerDic[task.Id], DownloadedSizeDic); var result = await DownloadStreamAsync(kp.Key, task, SpeedContainerDic[task.Id]);
Results[kp.Key] = result; Results[kp.Key] = result;
} }
} }
@ -683,7 +678,7 @@ namespace N_m3u8DL_RE.DownloadManager
await Parallel.ForEachAsync(dic, async (kp, _) => await Parallel.ForEachAsync(dic, async (kp, _) =>
{ {
var task = kp.Value; var task = kp.Value;
var result = await DownloadStreamAsync(kp.Key, task, SpeedContainerDic[task.Id], DownloadedSizeDic); var result = await DownloadStreamAsync(kp.Key, task, SpeedContainerDic[task.Id]);
Results[kp.Key] = result; Results[kp.Key] = result;
}); });
} }

View File

@ -13,7 +13,7 @@ namespace N_m3u8DL_RE.Entity
{ {
public bool SingleSegment { get; set; } = false; public bool SingleSegment { get; set; } = false;
public long? ResponseLength { get; set; } public long? ResponseLength { get; set; }
public long RDownloaded { get; set; } = 0L; public long RDownloaded { get => _Rdownloaded; }
private int _zeroSpeedCount = 0; private int _zeroSpeedCount = 0;
public int LowSpeedCount { get => _zeroSpeedCount; } public int LowSpeedCount { get => _zeroSpeedCount; }
public bool ShouldStop { get => LowSpeedCount >= 20; } public bool ShouldStop { get => LowSpeedCount >= 20; }
@ -21,6 +21,7 @@ namespace N_m3u8DL_RE.Entity
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
private long _downloaded = 0; private long _downloaded = 0;
private long _Rdownloaded = 0;
public long Downloaded { get => _downloaded; } public long Downloaded { get => _downloaded; }
public int AddLowSpeedCount() public int AddLowSpeedCount()
@ -35,7 +36,7 @@ namespace N_m3u8DL_RE.Entity
public long Add(long size) public long Add(long size)
{ {
if (SingleSegment) RDownloaded += size; Interlocked.Add(ref _Rdownloaded, size);
return Interlocked.Add(ref _downloaded, size); return Interlocked.Add(ref _downloaded, size);
} }
@ -50,7 +51,7 @@ namespace N_m3u8DL_RE.Entity
ResetLowSpeedCount(); ResetLowSpeedCount();
SingleSegment = false; SingleSegment = false;
ResponseLength = null; ResponseLength = null;
RDownloaded = 0L; _Rdownloaded = 0L;
} }
} }
} }