]>
Commit | Line | Data |
---|---|---|
1 | from hashlib import sha256 | |
2 | import json | |
3 | import re | |
4 | ||
5 | from .ffmpeg import FFmpegPostProcessor | |
6 | from ..compat import compat_urllib_parse_urlencode | |
7 | ||
8 | ||
9 | class SponsorBlockPP(FFmpegPostProcessor): | |
10 | # https://wiki.sponsor.ajay.app/w/Types | |
11 | EXTRACTORS = { | |
12 | 'Youtube': 'YouTube', | |
13 | } | |
14 | POI_CATEGORIES = { | |
15 | 'poi_highlight': 'Highlight', | |
16 | } | |
17 | CATEGORIES = { | |
18 | 'sponsor': 'Sponsor', | |
19 | 'intro': 'Intermission/Intro Animation', | |
20 | 'outro': 'Endcards/Credits', | |
21 | 'selfpromo': 'Unpaid/Self Promotion', | |
22 | 'preview': 'Preview/Recap', | |
23 | 'filler': 'Filler Tangent', | |
24 | 'interaction': 'Interaction Reminder', | |
25 | 'music_offtopic': 'Non-Music Section', | |
26 | **POI_CATEGORIES, | |
27 | } | |
28 | ||
29 | def __init__(self, downloader, categories=None, api='https://sponsor.ajay.app'): | |
30 | FFmpegPostProcessor.__init__(self, downloader) | |
31 | self._categories = tuple(categories or self.CATEGORIES.keys()) | |
32 | self._API_URL = api if re.match('^https?://', api) else 'https://' + api | |
33 | ||
34 | def run(self, info): | |
35 | extractor = info['extractor_key'] | |
36 | if extractor not in self.EXTRACTORS: | |
37 | self.to_screen(f'SponsorBlock is not supported for {extractor}') | |
38 | return [], info | |
39 | ||
40 | self.to_screen('Fetching SponsorBlock segments') | |
41 | info['sponsorblock_chapters'] = self._get_sponsor_chapters(info, info['duration']) | |
42 | return [], info | |
43 | ||
44 | def _get_sponsor_chapters(self, info, duration): | |
45 | segments = self._get_sponsor_segments(info['id'], self.EXTRACTORS[info['extractor_key']]) | |
46 | ||
47 | def duration_filter(s): | |
48 | start_end = s['segment'] | |
49 | # Ignore entire video segments (https://wiki.sponsor.ajay.app/w/Types). | |
50 | if start_end == (0, 0): | |
51 | return False | |
52 | # Ignore milliseconds difference at the start. | |
53 | if start_end[0] <= 1: | |
54 | start_end[0] = 0 | |
55 | # Make POI chapters 1 sec so that we can properly mark them | |
56 | if s['category'] in self.POI_CATEGORIES.keys(): | |
57 | start_end[1] += 1 | |
58 | # Ignore milliseconds difference at the end. | |
59 | # Never allow the segment to exceed the video. | |
60 | if duration and duration - start_end[1] <= 1: | |
61 | start_end[1] = duration | |
62 | # SponsorBlock duration may be absent or it may deviate from the real one. | |
63 | return s['videoDuration'] == 0 or not duration or abs(duration - s['videoDuration']) <= 1 | |
64 | ||
65 | duration_match = [s for s in segments if duration_filter(s)] | |
66 | if len(duration_match) != len(segments): | |
67 | self.report_warning('Some SponsorBlock segments are from a video of different duration, maybe from an old version of this video') | |
68 | ||
69 | def to_chapter(s): | |
70 | (start, end), cat = s['segment'], s['category'] | |
71 | return { | |
72 | 'start_time': start, | |
73 | 'end_time': end, | |
74 | 'category': cat, | |
75 | 'title': self.CATEGORIES[cat], | |
76 | '_categories': [(cat, start, end)] | |
77 | } | |
78 | ||
79 | sponsor_chapters = [to_chapter(s) for s in duration_match] | |
80 | if not sponsor_chapters: | |
81 | self.to_screen('No segments were found in the SponsorBlock database') | |
82 | else: | |
83 | self.to_screen(f'Found {len(sponsor_chapters)} segments in the SponsorBlock database') | |
84 | return sponsor_chapters | |
85 | ||
86 | def _get_sponsor_segments(self, video_id, service): | |
87 | hash = sha256(video_id.encode('ascii')).hexdigest() | |
88 | # SponsorBlock API recommends using first 4 hash characters. | |
89 | url = f'{self._API_URL}/api/skipSegments/{hash[:4]}?' + compat_urllib_parse_urlencode({ | |
90 | 'service': service, | |
91 | 'categories': json.dumps(self._categories), | |
92 | 'actionTypes': json.dumps(['skip', 'poi']) | |
93 | }) | |
94 | for d in self._download_json(url) or []: | |
95 | if d['videoID'] == video_id: | |
96 | return d['segments'] | |
97 | return [] |