diff --git a/src/N_m3u8DL-RE/Column/DownloadStatusColumn.cs b/src/N_m3u8DL-RE/Column/DownloadStatusColumn.cs index 9e256c9..9383efc 100644 --- a/src/N_m3u8DL-RE/Column/DownloadStatusColumn.cs +++ b/src/N_m3u8DL-RE/Column/DownloadStatusColumn.cs @@ -13,14 +13,12 @@ namespace N_m3u8DL_RE.Column { internal class DownloadStatusColumn : ProgressColumn { - private ConcurrentDictionary DownloadedSizeDic = new(); private ConcurrentDictionary SpeedContainerDic { get; set; } public Style MyStyle { get; set; } = new Style(foreground: Color.DarkCyan); public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green); - public DownloadStatusColumn(ConcurrentDictionary downloadedSizeDic, ConcurrentDictionary speedContainerDic) + public DownloadStatusColumn(ConcurrentDictionary speedContainerDic) { - this.DownloadedSizeDic = downloadedSizeDic; this.SpeedContainerDic = speedContainerDic; } @@ -28,24 +26,13 @@ namespace N_m3u8DL_RE.Column { 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]; - 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)}"; - if (done) sizeStr = GlobalUtil.FormatFileSize(totalSize); + var size = speedContainer.RDownloaded; + 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(); } } diff --git a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs index e80c0bd..86e6d10 100644 --- a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs +++ b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs @@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine { 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)")] private static partial Regex ForStrRegex(); diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index c189c8c..addc7ff 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -98,7 +98,7 @@ namespace N_m3u8DL_RE.DownloadManager } - private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer, ConcurrentDictionary sizeDic) + private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer) { speedContainer.ResetVars(); bool useAACFilter = false; //ffmpeg合并flag @@ -166,7 +166,6 @@ namespace N_m3u8DL_RE.DownloadManager } mp4InitFile = result.ActualFilePath; task.Increment(1); - sizeDic[task.Id] += result.ActualContentLength ?? 0; //读取mp4信息 if (result != null && result.Success) @@ -215,7 +214,6 @@ namespace N_m3u8DL_RE.DownloadManager throw new Exception("Download first segment failed!"); } task.Increment(1); - sizeDic[task.Id] += result.ActualContentLength ?? 0; if (result != null && result.Success) { //修复MSS init @@ -279,7 +277,6 @@ namespace N_m3u8DL_RE.DownloadManager var result = await Downloader.DownloadSegmentAsync(seg, path, speedContainer, headers); FileDic[seg] = result; task.Increment(1); - sizeDic[task.Id] += result.ActualContentLength ?? 0; //实时解密 if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID)) { @@ -633,7 +630,6 @@ namespace N_m3u8DL_RE.DownloadManager public async Task StartDownloadAsync() { - ConcurrentDictionary DownloadedSizeDic = new(); //大小计算 ConcurrentDictionary SpeedContainerDic = new(); //速度计算 ConcurrentDictionary Results = new(); @@ -645,7 +641,7 @@ namespace N_m3u8DL_RE.DownloadManager new TaskDescriptionColumn() { Alignment = Justify.Left }, new ProgressBarColumn(), new PercentageColumn(), - new DownloadStatusColumn(DownloadedSizeDic, SpeedContainerDic), + new DownloadStatusColumn(SpeedContainerDic), new DownloadSpeedColumn(SpeedContainerDic), //速度计算 new RemainingTimeColumn(), new SpinnerColumn(), @@ -663,7 +659,6 @@ namespace N_m3u8DL_RE.DownloadManager var description = item.ToShortShortString(); var task = ctx.AddTask(description, autoStart: false); SpeedContainerDic[task.Id] = new SpeedContainer(); //速度计算 - DownloadedSizeDic[task.Id] = 0; //大小计算 return (item, task); }).ToDictionary(item => item.item, item => item.task); @@ -673,7 +668,7 @@ namespace N_m3u8DL_RE.DownloadManager foreach (var kp in dic) { 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; } } @@ -683,7 +678,7 @@ namespace N_m3u8DL_RE.DownloadManager await Parallel.ForEachAsync(dic, async (kp, _) => { 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; }); } diff --git a/src/N_m3u8DL-RE/Entity/SpeedContainer.cs b/src/N_m3u8DL-RE/Entity/SpeedContainer.cs index bd5a949..4775756 100644 --- a/src/N_m3u8DL-RE/Entity/SpeedContainer.cs +++ b/src/N_m3u8DL-RE/Entity/SpeedContainer.cs @@ -13,7 +13,7 @@ namespace N_m3u8DL_RE.Entity { public bool SingleSegment { get; set; } = false; public long? ResponseLength { get; set; } - public long RDownloaded { get; set; } = 0L; + public long RDownloaded { get => _Rdownloaded; } private int _zeroSpeedCount = 0; public int LowSpeedCount { get => _zeroSpeedCount; } public bool ShouldStop { get => LowSpeedCount >= 20; } @@ -21,6 +21,7 @@ namespace N_m3u8DL_RE.Entity /////////////////////////////////////////////////// private long _downloaded = 0; + private long _Rdownloaded = 0; public long Downloaded { get => _downloaded; } public int AddLowSpeedCount() @@ -35,7 +36,7 @@ namespace N_m3u8DL_RE.Entity public long Add(long size) { - if (SingleSegment) RDownloaded += size; + Interlocked.Add(ref _Rdownloaded, size); return Interlocked.Add(ref _downloaded, size); } @@ -50,7 +51,7 @@ namespace N_m3u8DL_RE.Entity ResetLowSpeedCount(); SingleSegment = false; ResponseLength = null; - RDownloaded = 0L; + _Rdownloaded = 0L; } } }