diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index 73f55fd..da646e8 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -761,15 +761,8 @@ namespace N_m3u8DL_RE.DownloadManager var takeLastCount = 15; ConcurrentDictionary SpeedContainerDic = new(); //速度计算 ConcurrentDictionary 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>(); return (item, task); }).ToDictionary(item => item.item, item => item.task); diff --git a/src/N_m3u8DL-RE/Util/FilterUtil.cs b/src/N_m3u8DL-RE/Util/FilterUtil.cs index ae82daf..cec0913 100644 --- a/src/N_m3u8DL-RE/Util/FilterUtil.cs +++ b/src/N_m3u8DL-RE/Util/FilterUtil.cs @@ -131,5 +131,52 @@ namespace N_m3u8DL_RE.Util return selectedStreams; } + + /// + /// 直播使用。对齐各个轨道的起始。 + /// + /// + /// + public static void SyncStreams(List 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(); + } + } + } + } } }