1 from hashlib
import sha256
5 from .ffmpeg
import FFmpegPostProcessor
6 from ..compat
import compat_urllib_parse_urlencode
9 class SponsorBlockPP(FFmpegPostProcessor
):
10 # https://wiki.sponsor.ajay.app/w/Types
15 'poi_highlight': 'Highlight',
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',
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
35 extractor
= info
['extractor_key']
36 if extractor
not in self
.EXTRACTORS
:
37 self
.to_screen(f
'SponsorBlock is not supported for {extractor}')
40 self
.to_screen('Fetching SponsorBlock segments')
41 info
['sponsorblock_chapters'] = self
._get
_sponsor
_chapters
(info
, info
['duration'])
44 def _get_sponsor_chapters(self
, info
, duration
):
45 segments
= self
._get
_sponsor
_segments
(info
['id'], self
.EXTRACTORS
[info
['extractor_key']])
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):
52 # Ignore milliseconds difference at the start.
55 # Make POI chapters 1 sec so that we can properly mark them
56 if s
['category'] in self
.POI_CATEGORIES
.keys():
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
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')
70 (start
, end
), cat
= s
['segment'], s
['category']
75 'title': self
.CATEGORIES
[cat
],
76 '_categories': [(cat
, start
, end
)]
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')
83 self
.to_screen(f
'Found {len(sponsor_chapters)} segments in the SponsorBlock database')
84 return sponsor_chapters
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({
91 'categories': json
.dumps(self
._categories
),
92 'actionTypes': json
.dumps(['skip', 'poi'])
94 for d
in self
._download
_json
(url
) or []:
95 if d
['videoID'] == video_id
: