1
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp synced 2025-01-18 23:03:05 +01:00

Handle cutting after last keyframe

This commit is contained in:
Quantum 2025-01-11 13:17:38 -05:00
parent 751f71a644
commit ecae8eb19f
2 changed files with 9 additions and 6 deletions

View file

@ -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},
])

View file

@ -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)