]> jfr.im git - yt-dlp.git/blame - yt_dlp/postprocessor/modify_chapters.py
Improved progress reporting (See desc) (#1125)
[yt-dlp.git] / yt_dlp / postprocessor / modify_chapters.py
CommitLineData
7a340e0d
NA
1import copy
2import heapq
3import os
4
5from .common import PostProcessor
6from .ffmpeg import (
7 FFmpegPostProcessor,
8 FFmpegSubtitlesConvertorPP
9)
10from .sponsorblock import SponsorBlockPP
11from ..utils import (
7a340e0d
NA
12 orderedSet,
13 PostProcessingError,
14 prepend_extension,
7a340e0d
NA
15)
16
17
c6af2dd8 18_TINY_CHAPTER_DURATION = 1
7a340e0d
NA
19DEFAULT_SPONSORBLOCK_CHAPTER_TITLE = '[SponsorBlock]: %(category_names)l'
20
21
22class ModifyChaptersPP(FFmpegPostProcessor):
23 def __init__(self, downloader, remove_chapters_patterns=None, remove_sponsor_segments=None,
24 sponsorblock_chapter_title=DEFAULT_SPONSORBLOCK_CHAPTER_TITLE, force_keyframes=False):
25 FFmpegPostProcessor.__init__(self, downloader)
26 self._remove_chapters_patterns = set(remove_chapters_patterns or [])
27 self._remove_sponsor_segments = set(remove_sponsor_segments or [])
28 self._sponsorblock_chapter_title = sponsorblock_chapter_title
29 self._force_keyframes = force_keyframes
30
31 @PostProcessor._restrict_to(images=False)
32 def run(self, info):
33 chapters, sponsor_chapters = self._mark_chapters_to_remove(
34 info.get('chapters') or [], info.get('sponsorblock_chapters') or [])
35 if not chapters and not sponsor_chapters:
36 return [], info
37
165efb82 38 real_duration = self._get_real_video_duration(info)
7a340e0d
NA
39 if not chapters:
40 chapters = [{'start_time': 0, 'end_time': real_duration, 'title': info['title']}]
41
42 info['chapters'], cuts = self._remove_marked_arrange_sponsors(chapters + sponsor_chapters)
43 if not cuts:
44 return [], info
45
165efb82 46 if self._duration_mismatch(real_duration, info.get('duration')):
47 if not self._duration_mismatch(real_duration, info['chapters'][-1]['end_time']):
7a340e0d
NA
48 self.to_screen(f'Skipping {self.pp_key()} since the video appears to be already cut')
49 return [], info
50 if not info.get('__real_download'):
51 raise PostProcessingError('Cannot cut video since the real and expected durations mismatch. '
52 'Different chapters may have already been removed')
7a340e0d
NA
53 else:
54 self.write_debug('Expected and actual durations mismatch')
55
56 concat_opts = self._make_concat_opts(cuts, real_duration)
57
58 def remove_chapters(file, is_sub):
59 return file, self.remove_chapters(file, cuts, concat_opts, self._force_keyframes and not is_sub)
60
61 in_out_files = [remove_chapters(info['filepath'], False)]
62 in_out_files.extend(remove_chapters(in_file, True) for in_file in self._get_supported_subs(info))
63
64 # Renaming should only happen after all files are processed
65 files_to_remove = []
66 for in_file, out_file in in_out_files:
67 uncut_file = prepend_extension(in_file, 'uncut')
68 os.replace(in_file, uncut_file)
69 os.replace(out_file, in_file)
70 files_to_remove.append(uncut_file)
71
165efb82 72 info['_real_duration'] = info['chapters'][-1]['end_time']
7a340e0d
NA
73 return files_to_remove, info
74
75 def _mark_chapters_to_remove(self, chapters, sponsor_chapters):
76 if self._remove_chapters_patterns:
77 warn_no_chapter_to_remove = True
78 if not chapters:
79 self.to_screen('Chapter information is unavailable')
80 warn_no_chapter_to_remove = False
81 for c in chapters:
82 if any(regex.search(c['title']) for regex in self._remove_chapters_patterns):
83 c['remove'] = True
84 warn_no_chapter_to_remove = False
85 if warn_no_chapter_to_remove:
86 self.to_screen('There are no chapters matching the regex')
87
88 if self._remove_sponsor_segments:
89 warn_no_chapter_to_remove = True
90 if not sponsor_chapters:
91 self.to_screen('SponsorBlock information is unavailable')
92 warn_no_chapter_to_remove = False
93 for c in sponsor_chapters:
94 if c['category'] in self._remove_sponsor_segments:
95 c['remove'] = True
96 warn_no_chapter_to_remove = False
97 if warn_no_chapter_to_remove:
98 self.to_screen('There are no matching SponsorBlock chapters')
99
100 return chapters, sponsor_chapters
101
7a340e0d
NA
102 def _get_supported_subs(self, info):
103 for sub in (info.get('requested_subtitles') or {}).values():
104 sub_file = sub.get('filepath')
105 # The file might have been removed by --embed-subs
106 if not sub_file or not os.path.exists(sub_file):
107 continue
108 ext = sub['ext']
109 if ext not in FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS:
110 self.report_warning(f'Cannot remove chapters from external {ext} subtitles; "{sub_file}" is now out of sync')
111 continue
112 # TODO: create __real_download for subs?
113 yield sub_file
114
115 def _remove_marked_arrange_sponsors(self, chapters):
116 # Store cuts separately, since adjacent and overlapping cuts must be merged.
117 cuts = []
118
119 def append_cut(c):
120 assert 'remove' in c
121 last_to_cut = cuts[-1] if cuts else None
122 if last_to_cut and last_to_cut['end_time'] >= c['start_time']:
123 last_to_cut['end_time'] = max(last_to_cut['end_time'], c['end_time'])
124 else:
125 cuts.append(c)
126 return len(cuts) - 1
127
128 def excess_duration(c):
129 # Cuts that are completely within the chapter reduce chapters' duration.
130 # Since cuts can overlap, excess duration may be less that the sum of cuts' durations.
131 # To avoid that, chapter stores the index to the fist cut within the chapter,
132 # instead of storing excess duration. append_cut ensures that subsequent cuts (if any)
133 # will be merged with previous ones (if necessary).
134 cut_idx, excess = c.pop('cut_idx', len(cuts)), 0
135 while cut_idx < len(cuts):
136 cut = cuts[cut_idx]
137 if cut['start_time'] >= c['end_time']:
138 break
139 if cut['end_time'] > c['start_time']:
140 excess += min(cut['end_time'], c['end_time'])
141 excess -= max(cut['start_time'], c['start_time'])
142 cut_idx += 1
143 return excess
144
145 new_chapters = []
146
7a340e0d
NA
147 def append_chapter(c):
148 assert 'remove' not in c
c6af2dd8 149 length = c['end_time'] - c['start_time'] - excess_duration(c)
7a340e0d
NA
150 # Chapter is completely covered by cuts or sponsors.
151 if length <= 0:
152 return
153 start = new_chapters[-1]['end_time'] if new_chapters else 0
154 c.update(start_time=start, end_time=start + length)
c6af2dd8 155 new_chapters.append(c)
7a340e0d
NA
156
157 # Turn into a priority queue, index is a tie breaker.
158 # Plain stack sorted by start_time is not enough: after splitting the chapter,
159 # the part returned to the stack is not guaranteed to have start_time
160 # less than or equal to the that of the stack's head.
161 chapters = [(c['start_time'], i, c) for i, c in enumerate(chapters)]
162 heapq.heapify(chapters)
163
164 _, cur_i, cur_chapter = heapq.heappop(chapters)
165 while chapters:
166 _, i, c = heapq.heappop(chapters)
167 # Non-overlapping chapters or cuts can be appended directly. However,
168 # adjacent non-overlapping cuts must be merged, which is handled by append_cut.
169 if cur_chapter['end_time'] <= c['start_time']:
170 (append_chapter if 'remove' not in cur_chapter else append_cut)(cur_chapter)
171 cur_i, cur_chapter = i, c
172 continue
173
174 # Eight possibilities for overlapping chapters: (cut, cut), (cut, sponsor),
175 # (cut, normal), (sponsor, cut), (normal, cut), (sponsor, sponsor),
176 # (sponsor, normal), and (normal, sponsor). There is no (normal, normal):
177 # normal chapters are assumed not to overlap.
178 if 'remove' in cur_chapter:
179 # (cut, cut): adjust end_time.
180 if 'remove' in c:
181 cur_chapter['end_time'] = max(cur_chapter['end_time'], c['end_time'])
182 # (cut, sponsor/normal): chop the beginning of the later chapter
183 # (if it's not completely hidden by the cut). Push to the priority queue
184 # to restore sorting by start_time: with beginning chopped, c may actually
185 # start later than the remaining chapters from the queue.
186 elif cur_chapter['end_time'] < c['end_time']:
187 c['start_time'] = cur_chapter['end_time']
188 c['_was_cut'] = True
189 heapq.heappush(chapters, (c['start_time'], i, c))
190 # (sponsor/normal, cut).
191 elif 'remove' in c:
192 cur_chapter['_was_cut'] = True
193 # Chop the end of the current chapter if the cut is not contained within it.
194 # Chopping the end doesn't break start_time sorting, no PQ push is necessary.
195 if cur_chapter['end_time'] <= c['end_time']:
196 cur_chapter['end_time'] = c['start_time']
197 append_chapter(cur_chapter)
198 cur_i, cur_chapter = i, c
199 continue
200 # Current chapter contains the cut within it. If the current chapter is
201 # a sponsor chapter, check whether the categories before and after the cut differ.
202 if '_categories' in cur_chapter:
203 after_c = dict(cur_chapter, start_time=c['end_time'], _categories=[])
204 cur_cats = []
205 for cat_start_end in cur_chapter['_categories']:
206 if cat_start_end[1] < c['start_time']:
207 cur_cats.append(cat_start_end)
208 if cat_start_end[2] > c['end_time']:
209 after_c['_categories'].append(cat_start_end)
210 cur_chapter['_categories'] = cur_cats
211 if cur_chapter['_categories'] != after_c['_categories']:
212 # Categories before and after the cut differ: push the after part to PQ.
213 heapq.heappush(chapters, (after_c['start_time'], cur_i, after_c))
214 cur_chapter['end_time'] = c['start_time']
215 append_chapter(cur_chapter)
216 cur_i, cur_chapter = i, c
217 continue
218 # Either sponsor categories before and after the cut are the same or
219 # we're dealing with a normal chapter. Just register an outstanding cut:
220 # subsequent append_chapter will reduce the duration.
221 cur_chapter.setdefault('cut_idx', append_cut(c))
222 # (sponsor, normal): if a normal chapter is not completely overlapped,
223 # chop the beginning of it and push it to PQ.
224 elif '_categories' in cur_chapter and '_categories' not in c:
225 if cur_chapter['end_time'] < c['end_time']:
226 c['start_time'] = cur_chapter['end_time']
227 c['_was_cut'] = True
228 heapq.heappush(chapters, (c['start_time'], i, c))
229 # (normal, sponsor) and (sponsor, sponsor)
230 else:
231 assert '_categories' in c
232 cur_chapter['_was_cut'] = True
233 c['_was_cut'] = True
234 # Push the part after the sponsor to PQ.
235 if cur_chapter['end_time'] > c['end_time']:
236 # deepcopy to make categories in after_c and cur_chapter/c refer to different lists.
237 after_c = dict(copy.deepcopy(cur_chapter), start_time=c['end_time'])
238 heapq.heappush(chapters, (after_c['start_time'], cur_i, after_c))
239 # Push the part after the overlap to PQ.
240 elif c['end_time'] > cur_chapter['end_time']:
241 after_cur = dict(copy.deepcopy(c), start_time=cur_chapter['end_time'])
242 heapq.heappush(chapters, (after_cur['start_time'], cur_i, after_cur))
243 c['end_time'] = cur_chapter['end_time']
244 # (sponsor, sponsor): merge categories in the overlap.
245 if '_categories' in cur_chapter:
246 c['_categories'] = cur_chapter['_categories'] + c['_categories']
247 # Inherit the cuts that the current chapter has accumulated within it.
248 if 'cut_idx' in cur_chapter:
249 c['cut_idx'] = cur_chapter['cut_idx']
250 cur_chapter['end_time'] = c['start_time']
251 append_chapter(cur_chapter)
252 cur_i, cur_chapter = i, c
253 (append_chapter if 'remove' not in cur_chapter else append_cut)(cur_chapter)
c6af2dd8
NA
254 return self._remove_tiny_rename_sponsors(new_chapters), cuts
255
256 def _remove_tiny_rename_sponsors(self, chapters):
257 new_chapters = []
258 for i, c in enumerate(chapters):
259 # Merge with the previous/next if the chapter is tiny.
260 # Only tiny chapters resulting from a cut can be skipped.
261 # Chapters that were already tiny in the original list will be preserved.
262 if (('_was_cut' in c or '_categories' in c)
263 and c['end_time'] - c['start_time'] < _TINY_CHAPTER_DURATION):
264 if not new_chapters:
265 # Prepend tiny chapter to the next one if possible.
266 if i < len(chapters) - 1:
267 chapters[i + 1]['start_time'] = c['start_time']
268 continue
269 else:
270 old_c = new_chapters[-1]
271 if i < len(chapters) - 1:
272 next_c = chapters[i + 1]
273 # Not a typo: key names in old_c and next_c are really different.
274 prev_is_sponsor = 'categories' in old_c
275 next_is_sponsor = '_categories' in next_c
276 # Preferentially prepend tiny normals to normals and sponsors to sponsors.
277 if (('_categories' not in c and prev_is_sponsor and not next_is_sponsor)
278 or ('_categories' in c and not prev_is_sponsor and next_is_sponsor)):
279 next_c['start_time'] = c['start_time']
280 continue
281 old_c['end_time'] = c['end_time']
282 continue
7a340e0d 283
7a340e0d
NA
284 c.pop('_was_cut', None)
285 cats = c.pop('_categories', None)
286 if cats:
287 category = min(cats, key=lambda c: c[2] - c[1])[0]
288 cats = orderedSet(x[0] for x in cats)
289 c.update({
290 'category': category,
291 'categories': cats,
292 'name': SponsorBlockPP.CATEGORIES[category],
293 'category_names': [SponsorBlockPP.CATEGORIES[c] for c in cats]
294 })
819e0531 295 c['title'] = self._downloader.evaluate_outtmpl(self._sponsorblock_chapter_title, c)
c6af2dd8
NA
296 # Merge identically named sponsors.
297 if (new_chapters and 'categories' in new_chapters[-1]
298 and new_chapters[-1]['title'] == c['title']):
299 new_chapters[-1]['end_time'] = c['end_time']
300 continue
301 new_chapters.append(c)
302 return new_chapters
7a340e0d
NA
303
304 def remove_chapters(self, filename, ranges_to_cut, concat_opts, force_keyframes=False):
305 in_file = filename
306 out_file = prepend_extension(in_file, 'temp')
307 if force_keyframes:
165efb82 308 in_file = self.force_keyframes(in_file, (t for c in ranges_to_cut for t in (c['start_time'], c['end_time'])))
7a340e0d
NA
309 self.to_screen(f'Removing chapters from {filename}')
310 self.concat_files([in_file] * len(concat_opts), out_file, concat_opts)
311 if in_file != filename:
312 os.remove(in_file)
313 return out_file
314
315 @staticmethod
316 def _make_concat_opts(chapters_to_remove, duration):
317 opts = [{}]
318 for s in chapters_to_remove:
319 # Do not create 0 duration chunk at the beginning.
320 if s['start_time'] == 0:
321 opts[-1]['inpoint'] = f'{s["end_time"]:.6f}'
322 continue
323 opts[-1]['outpoint'] = f'{s["start_time"]:.6f}'
324 # Do not create 0 duration chunk at the end.
325 if s['end_time'] != duration:
326 opts.append({'inpoint': f'{s["end_time"]:.6f}'})
327 return opts