优化单分段的速度计算及进度显示

This commit is contained in:
nilaoda 2022-08-23 23:39:39 +08:00
parent e9a6e6edc8
commit df63b42356
4 changed files with 26 additions and 1 deletions

View File

@ -27,6 +27,13 @@ namespace N_m3u8DL_RE.Column
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime) public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{ {
var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 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) if (DateTimeString != now)
{ {
@ -34,7 +41,6 @@ namespace N_m3u8DL_RE.Column
SpeedContainer.Reset(); SpeedContainer.Reset();
DateTimeString = now; DateTimeString = now;
} }
var flag = task.IsFinished || !task.IsStarted;
var style = flag ? Style.Plain : MyStyle; var style = flag ? Style.Plain : MyStyle;
return flag ? new Text("-", style).Centered() : new Text(Speed, style).Centered(); return flag ? new Text("-", style).Centered() : new Text(Speed, style).Centered();
} }

View File

@ -112,11 +112,13 @@ namespace N_m3u8DL_RE.DownloadManager
private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer) private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer)
{ {
speedContainer.ResetVars();
bool useAACFilter = false; //ffmpeg合并flag bool useAACFilter = false; //ffmpeg合并flag
ConcurrentDictionary<MediaSegment, DownloadResult?> FileDic = new(); ConcurrentDictionary<MediaSegment, DownloadResult?> FileDic = new();
var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments); var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments);
if (segments == null) return false; if (segments == null) return false;
if (segments.Count() == 1) speedContainer.SingleSegment = true;
var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO; 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}"; var dirName = $"{DownloaderConfig.SaveName ?? NowDateTime.ToString("yyyy-MM-dd_HH-mm-ss")}_{streamSpec.GroupId}_{streamSpec.Codecs}_{streamSpec.Bandwidth}_{streamSpec.Language}";

View File

@ -11,11 +11,18 @@ namespace N_m3u8DL_RE.Entity
{ {
internal class SpeedContainer internal class SpeedContainer
{ {
public bool SingleSegment { get; set; } = false;
public long? ResponseLength { get; set; }
public long RDownloaded { get; set; } = 0L;
///////////////////////////////////////////////////
private long _downloaded = 0; private long _downloaded = 0;
public long Downloaded { get => _downloaded; } public long Downloaded { get => _downloaded; }
public long Add(long size) public long Add(long size)
{ {
if (SingleSegment) RDownloaded += size;
return Interlocked.Add(ref _downloaded, size); return Interlocked.Add(ref _downloaded, size);
} }
@ -23,5 +30,13 @@ namespace N_m3u8DL_RE.Entity
{ {
Interlocked.Exchange(ref _downloaded, 0); Interlocked.Exchange(ref _downloaded, 0);
} }
public void ResetVars()
{
Reset();
SingleSegment = false;
ResponseLength = null;
RDownloaded = 0L;
}
} }
} }

View File

@ -51,6 +51,8 @@ namespace N_m3u8DL_RE.Util
} }
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength; 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 stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
using var responseStream = await response.Content.ReadAsStreamAsync(); using var responseStream = await response.Content.ReadAsStreamAsync();
var buffer = new byte[16 * 1024]; var buffer = new byte[16 * 1024];