From df63b423569cb825bc0cb35327a29abc71800456 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Tue, 23 Aug 2022 23:39:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8D=95=E5=88=86=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E9=80=9F=E5=BA=A6=E8=AE=A1=E7=AE=97=E5=8F=8A=E8=BF=9B?= =?UTF-8?q?=E5=BA=A6=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE/Column/DownloadSpeedColumn.cs | 8 +++++++- .../DownloadManager/SimpleDownloadManager.cs | 2 ++ src/N_m3u8DL-RE/Entity/SpeedContainer.cs | 15 +++++++++++++++ src/N_m3u8DL-RE/Util/DownloadUtil.cs | 2 ++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/N_m3u8DL-RE/Column/DownloadSpeedColumn.cs b/src/N_m3u8DL-RE/Column/DownloadSpeedColumn.cs index 848ff9c..0fa0903 100644 --- a/src/N_m3u8DL-RE/Column/DownloadSpeedColumn.cs +++ b/src/N_m3u8DL-RE/Column/DownloadSpeedColumn.cs @@ -27,6 +27,13 @@ namespace N_m3u8DL_RE.Column public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime) { var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + var flag = task.IsFinished || !task.IsStarted; + //单文件下载汇报进度 + if (!flag && SpeedContainer.SingleSegment && SpeedContainer.ResponseLength != null) + { + task.MaxValue = (double)SpeedContainer.ResponseLength; + task.Value = SpeedContainer.RDownloaded; + } //一秒汇报一次即可 if (DateTimeString != now) { @@ -34,7 +41,6 @@ namespace N_m3u8DL_RE.Column SpeedContainer.Reset(); DateTimeString = now; } - var flag = task.IsFinished || !task.IsStarted; var style = flag ? Style.Plain : MyStyle; return flag ? new Text("-", style).Centered() : new Text(Speed, style).Centered(); } diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index b8e6697..442f847 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -112,11 +112,13 @@ namespace N_m3u8DL_RE.DownloadManager private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer) { + speedContainer.ResetVars(); bool useAACFilter = false; //ffmpeg合并flag ConcurrentDictionary FileDic = new(); var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments); if (segments == null) return false; + if (segments.Count() == 1) speedContainer.SingleSegment = true; var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO; var dirName = $"{DownloaderConfig.SaveName ?? NowDateTime.ToString("yyyy-MM-dd_HH-mm-ss")}_{streamSpec.GroupId}_{streamSpec.Codecs}_{streamSpec.Bandwidth}_{streamSpec.Language}"; diff --git a/src/N_m3u8DL-RE/Entity/SpeedContainer.cs b/src/N_m3u8DL-RE/Entity/SpeedContainer.cs index 16815d8..66aaa41 100644 --- a/src/N_m3u8DL-RE/Entity/SpeedContainer.cs +++ b/src/N_m3u8DL-RE/Entity/SpeedContainer.cs @@ -11,11 +11,18 @@ namespace N_m3u8DL_RE.Entity { internal class SpeedContainer { + public bool SingleSegment { get; set; } = false; + public long? ResponseLength { get; set; } + public long RDownloaded { get; set; } = 0L; + + /////////////////////////////////////////////////// + private long _downloaded = 0; public long Downloaded { get => _downloaded; } public long Add(long size) { + if (SingleSegment) RDownloaded += size; return Interlocked.Add(ref _downloaded, size); } @@ -23,5 +30,13 @@ namespace N_m3u8DL_RE.Entity { Interlocked.Exchange(ref _downloaded, 0); } + + public void ResetVars() + { + Reset(); + SingleSegment = false; + ResponseLength = null; + RDownloaded = 0L; + } } } diff --git a/src/N_m3u8DL-RE/Util/DownloadUtil.cs b/src/N_m3u8DL-RE/Util/DownloadUtil.cs index be518b5..8d1a4ea 100644 --- a/src/N_m3u8DL-RE/Util/DownloadUtil.cs +++ b/src/N_m3u8DL-RE/Util/DownloadUtil.cs @@ -51,6 +51,8 @@ namespace N_m3u8DL_RE.Util } response.EnsureSuccessStatusCode(); var contentLength = response.Content.Headers.ContentLength; + if (speedContainer.SingleSegment) speedContainer.ResponseLength = contentLength; + using var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); using var responseStream = await response.Content.ReadAsStreamAsync(); var buffer = new byte[16 * 1024];