diff --git a/test/test_postprocessors.py b/test/test_postprocessors.py index 8d3fac86c..510c13f81 100644 --- a/test/test_postprocessors.py +++ b/test/test_postprocessors.py @@ -564,7 +564,7 @@ class TestModifyChaptersPP(unittest.TestCase): {'start_time': 0, 'end_time': 2}, {'start_time': 2, 'end_time': 6, 'remove': True}, {'start_time': 6, 'end_time': 10, 'remove': False}, - ]) + ], duration=12) self.assertEqual(chapters, [ {'start_time': 0, 'end_time': 2}, {'start_time': 2, 'end_time': 5, 'remove': True}, @@ -577,7 +577,7 @@ class TestModifyChaptersPP(unittest.TestCase): {'start_time': 0, 'end_time': 2}, {'start_time': 3, 'end_time': 7, 'remove': True}, {'start_time': 6, 'end_time': 10, 'remove': False}, - ]) + ], duration=12) self.assertEqual(chapters, [ {'start_time': 0, 'end_time': 2}, {'start_time': 3, 'end_time': 7, 'remove': True}, @@ -589,7 +589,7 @@ class TestModifyChaptersPP(unittest.TestCase): chapters = self._pp._round_remove_chapters(keyframes, [ {'start_time': 0, 'end_time': 2}, {'start_time': 3, 'end_time': 8, 'remove': True}, - ]) + ], duration=8) self.assertEqual(chapters, [ {'start_time': 0, 'end_time': 2}, {'start_time': 3, 'end_time': 8, 'remove': True}, @@ -600,7 +600,7 @@ class TestModifyChaptersPP(unittest.TestCase): chapters = self._pp._round_remove_chapters(keyframes, [ {'start_time': 0, 'end_time': 2}, {'start_time': 8, 'end_time': 9, 'remove': True}, - ]) + ], duration=10) self.assertEqual(chapters, [ {'start_time': 0, 'end_time': 2}, ]) diff --git a/yt_dlp/postprocessor/modify_chapters.py b/yt_dlp/postprocessor/modify_chapters.py index a6477451f..f950823ca 100644 --- a/yt_dlp/postprocessor/modify_chapters.py +++ b/yt_dlp/postprocessor/modify_chapters.py @@ -42,7 +42,7 @@ class ModifyChaptersPP(FFmpegPostProcessor): chapters += sponsor_chapters if self._round_to_keyframes: keyframes = self.get_keyframe_timestamps(info['filepath']) - self._round_remove_chapters(keyframes, chapters) + self._round_remove_chapters(keyframes, chapters, info.get('duration') or real_duration) info['chapters'], cuts = self._remove_marked_arrange_sponsors(chapters) if not cuts: @@ -334,13 +334,16 @@ class ModifyChaptersPP(FFmpegPostProcessor): return out_file @staticmethod - def _round_remove_chapters(keyframes, chapters): + def _round_remove_chapters(keyframes, chapters, duration): result = [] for c in chapters: if not c.get('remove', False) or not keyframes: result.append(c) continue + if c['end_time'] > keyframes[-1] and c['end_time'] != duration: + continue + if c['end_time'] < keyframes[-1]: c['end_time'] = keyframes[bisect.bisect_right(keyframes, c['end_time']) - 1] result.append(c)