修正直播场景多流同步问题

This commit is contained in:
nilaoda 2023-04-12 17:46:21 +08:00
parent e5c175dd09
commit e26ca8bf42
2 changed files with 50 additions and 10 deletions

View File

@ -761,15 +761,8 @@ namespace N_m3u8DL_RE.DownloadManager
var takeLastCount = 15;
ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic = new(); //速度计算
ConcurrentDictionary<StreamSpec, bool?> Results = new();
//取最后15个分片
var maxIndex = SelectedSteams.Min(s => s.Playlist!.MediaParts[0].MediaSegments.Max(s => s.Index));
foreach (var item in SelectedSteams)
{
foreach (var part in item.Playlist!.MediaParts)
{
part.MediaSegments = part.MediaSegments.Where(s => s.Index <= maxIndex).TakeLast(takeLastCount).ToList();
}
}
//同步流
FilterUtil.SyncStreams(SelectedSteams, takeLastCount);
//设置等待时间
if (WAIT_SEC == 0)
{
@ -812,7 +805,7 @@ namespace N_m3u8DL_RE.DownloadManager
LastFileNameDic[task.Id] = "";
DateTimeDic[task.Id] = 0L;
RecordingDurDic[task.Id] = 0;
MaxIndexDic[task.Id] = item.Playlist?.MediaParts[0].MediaSegments.Max(s => s.Index) ?? 0L; //最大Index
MaxIndexDic[task.Id] = item.Playlist?.MediaParts[0].MediaSegments.LastOrDefault()?.Index ?? 0L; //最大Index
BlockDic[task.Id] = new BufferBlock<List<MediaSegment>>();
return (item, task);
}).ToDictionary(item => item.item, item => item.task);

View File

@ -131,5 +131,52 @@ namespace N_m3u8DL_RE.Util
return selectedStreams;
}
/// <summary>
/// 直播使用。对齐各个轨道的起始。
/// </summary>
/// <param name="streams"></param>
/// <param name="takeLastCount"></param>
public static void SyncStreams(List<StreamSpec> selectedSteams, int takeLastCount = 15)
{
//通过Date同步
if (selectedSteams.All(x => x.Playlist!.MediaParts[0].MediaSegments.All(x => x.DateTime != null)))
{
var minDate = selectedSteams.Max(s => s.Playlist!.MediaParts[0].MediaSegments.Min(s => s.DateTime))!;
foreach (var item in selectedSteams)
{
foreach (var part in item.Playlist!.MediaParts)
{
//秒级同步 忽略毫秒
part.MediaSegments = part.MediaSegments.Where(s => s.DateTime!.Value.Ticks / TimeSpan.TicksPerSecond >= minDate.Value.Ticks / TimeSpan.TicksPerSecond).ToList();
}
}
}
else //通过index同步
{
var minIndex = selectedSteams.Max(s => s.Playlist!.MediaParts[0].MediaSegments.Min(s => s.Index));
foreach (var item in selectedSteams)
{
foreach (var part in item.Playlist!.MediaParts)
{
part.MediaSegments = part.MediaSegments.Where(s => s.Index >= minIndex).ToList();
}
}
}
//取最新的N个分片
if (selectedSteams.Any(x => x.Playlist!.MediaParts[0].MediaSegments.Count > takeLastCount))
{
var skipCount = selectedSteams.Min(x => x.Playlist!.MediaParts[0].MediaSegments.Count) - takeLastCount;
if (skipCount < 0) skipCount = 0;
foreach (var item in selectedSteams)
{
foreach (var part in item.Playlist!.MediaParts)
{
part.MediaSegments = part.MediaSegments.Skip(skipCount).ToList();
}
}
}
}
}
}