From e6371f2b564112bec09d9052aff97f1154c4a016 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Mon, 18 Jul 2022 17:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96VTT=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=BD=B4=E5=81=8F=E7=A7=BB=E7=9A=84=E8=87=AA=E5=8A=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs | 32 ++++++++++++++----- .../DownloadManager/SimpleDownloadManager.cs | 5 +++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs b/src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs index 26a9d31..f2f3100 100644 --- a/src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs +++ b/src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs @@ -111,24 +111,40 @@ namespace N_m3u8DL_RE.Common.Entity FixTimestamp(webSub, this.MpegtsTimestamp); foreach (var item in webSub.Cues) { - if (!this.Cues.Contains(item)) this.Cues.Add(item); + if (!this.Cues.Contains(item)) + { + //如果相差只有1ms,且payload相同,则拼接 + var last = this.Cues.LastOrDefault(); + if (last != null && this.Cues.Count > 0 && (item.StartTime - last.EndTime).TotalMilliseconds <= 1 && item.Payload == last.Payload) + { + last.EndTime = item.EndTime; + } + else + { + this.Cues.Add(item); + } + } } return this; } - public static void FixTimestamp(WebVttSub sub, long baseTimestamp) + private void FixTimestamp(WebVttSub sub, long baseTimestamp) { - if (baseTimestamp == 0 || sub.MpegtsTimestamp == 0) + if (sub.MpegtsTimestamp == 0) { return; } - //The MPEG2 transport stream clocks (PCR, PTS, DTS) all have units of 1/90000 second - var seconds = (sub.MpegtsTimestamp - baseTimestamp) / 90000; - for (int i = 0; i < sub.Cues.Count; i++) + //确实存在时间轴错误的情况,才修复 + if ((this.Cues.Count > 0 && sub.Cues.Count > 0 && sub.Cues.First().StartTime < this.Cues.Last().EndTime) || this.Cues.Count == 0) { - sub.Cues[i].StartTime += TimeSpan.FromSeconds(seconds); - sub.Cues[i].EndTime += TimeSpan.FromSeconds(seconds); + //The MPEG2 transport stream clocks (PCR, PTS, DTS) all have units of 1/90000 second + var seconds = (sub.MpegtsTimestamp - baseTimestamp) / 90000; + for (int i = 0; i < sub.Cues.Count; i++) + { + sub.Cues[i].StartTime += TimeSpan.FromSeconds(seconds); + sub.Cues[i].EndTime += TimeSpan.FromSeconds(seconds); + } } } diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 85356c5..2d85386 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -132,6 +132,11 @@ namespace N_m3u8DL_RE.DownloadManager { var vttContent = File.ReadAllText(FileDic[seg]!.ActualFilePath); var vtt = WebVttSub.Parse(vttContent); + //手动计算MPEGTS + if (finalVtt.MpegtsTimestamp == 0 && vtt.MpegtsTimestamp == 0) + { + vtt.MpegtsTimestamp = 90 * (long)(seg.Duration * 1000) * seg.Index; + } if (first) { finalVtt = vtt;