2022-04-12 02:01:54 +02:00
|
|
|
import hashlib
|
2021-09-01 22:55:16 +02:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
from .ffmpeg import FFmpegPostProcessor
|
2022-03-25 04:01:45 +01:00
|
|
|
from ..compat import compat_urllib_parse_urlencode
|
2021-09-01 22:55:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SponsorBlockPP(FFmpegPostProcessor):
|
2021-12-09 13:40:31 +01:00
|
|
|
# https://wiki.sponsor.ajay.app/w/Types
|
2021-09-01 22:55:16 +02:00
|
|
|
EXTRACTORS = {
|
|
|
|
'Youtube': 'YouTube',
|
|
|
|
}
|
2021-12-09 13:40:31 +01:00
|
|
|
POI_CATEGORIES = {
|
|
|
|
'poi_highlight': 'Highlight',
|
|
|
|
}
|
2021-09-01 22:55:16 +02:00
|
|
|
CATEGORIES = {
|
|
|
|
'sponsor': 'Sponsor',
|
|
|
|
'intro': 'Intermission/Intro Animation',
|
|
|
|
'outro': 'Endcards/Credits',
|
|
|
|
'selfpromo': 'Unpaid/Self Promotion',
|
|
|
|
'preview': 'Preview/Recap',
|
2021-12-09 13:40:31 +01:00
|
|
|
'filler': 'Filler Tangent',
|
|
|
|
'interaction': 'Interaction Reminder',
|
|
|
|
'music_offtopic': 'Non-Music Section',
|
|
|
|
**POI_CATEGORIES,
|
2021-09-01 22:55:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, downloader, categories=None, api='https://sponsor.ajay.app'):
|
|
|
|
FFmpegPostProcessor.__init__(self, downloader)
|
|
|
|
self._categories = tuple(categories or self.CATEGORIES.keys())
|
|
|
|
self._API_URL = api if re.match('^https?://', api) else 'https://' + api
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
extractor = info['extractor_key']
|
|
|
|
if extractor not in self.EXTRACTORS:
|
|
|
|
self.to_screen(f'SponsorBlock is not supported for {extractor}')
|
|
|
|
return [], info
|
|
|
|
|
2021-10-18 05:49:37 +02:00
|
|
|
self.to_screen('Fetching SponsorBlock segments')
|
2022-04-23 18:45:00 +02:00
|
|
|
info['sponsorblock_chapters'] = self._get_sponsor_chapters(info, info.get('duration'))
|
2021-09-01 22:55:16 +02:00
|
|
|
return [], info
|
|
|
|
|
|
|
|
def _get_sponsor_chapters(self, info, duration):
|
|
|
|
segments = self._get_sponsor_segments(info['id'], self.EXTRACTORS[info['extractor_key']])
|
|
|
|
|
|
|
|
def duration_filter(s):
|
|
|
|
start_end = s['segment']
|
2022-02-22 15:18:44 +01:00
|
|
|
# Ignore entire video segments (https://wiki.sponsor.ajay.app/w/Types).
|
|
|
|
if start_end == (0, 0):
|
|
|
|
return False
|
2021-09-01 22:55:16 +02:00
|
|
|
# Ignore milliseconds difference at the start.
|
|
|
|
if start_end[0] <= 1:
|
|
|
|
start_end[0] = 0
|
2021-12-09 13:40:31 +01:00
|
|
|
# Make POI chapters 1 sec so that we can properly mark them
|
|
|
|
if s['category'] in self.POI_CATEGORIES.keys():
|
|
|
|
start_end[1] += 1
|
2021-09-01 22:55:16 +02:00
|
|
|
# Ignore milliseconds difference at the end.
|
|
|
|
# Never allow the segment to exceed the video.
|
|
|
|
if duration and duration - start_end[1] <= 1:
|
|
|
|
start_end[1] = duration
|
|
|
|
# SponsorBlock duration may be absent or it may deviate from the real one.
|
|
|
|
return s['videoDuration'] == 0 or not duration or abs(duration - s['videoDuration']) <= 1
|
|
|
|
|
|
|
|
duration_match = [s for s in segments if duration_filter(s)]
|
|
|
|
if len(duration_match) != len(segments):
|
|
|
|
self.report_warning('Some SponsorBlock segments are from a video of different duration, maybe from an old version of this video')
|
|
|
|
|
|
|
|
def to_chapter(s):
|
|
|
|
(start, end), cat = s['segment'], s['category']
|
|
|
|
return {
|
|
|
|
'start_time': start,
|
|
|
|
'end_time': end,
|
|
|
|
'category': cat,
|
|
|
|
'title': self.CATEGORIES[cat],
|
|
|
|
'_categories': [(cat, start, end)]
|
|
|
|
}
|
|
|
|
|
|
|
|
sponsor_chapters = [to_chapter(s) for s in duration_match]
|
|
|
|
if not sponsor_chapters:
|
|
|
|
self.to_screen('No segments were found in the SponsorBlock database')
|
|
|
|
else:
|
|
|
|
self.to_screen(f'Found {len(sponsor_chapters)} segments in the SponsorBlock database')
|
|
|
|
return sponsor_chapters
|
|
|
|
|
|
|
|
def _get_sponsor_segments(self, video_id, service):
|
2022-04-12 02:01:54 +02:00
|
|
|
hash = hashlib.sha256(video_id.encode('ascii')).hexdigest()
|
2021-09-01 22:55:16 +02:00
|
|
|
# SponsorBlock API recommends using first 4 hash characters.
|
|
|
|
url = f'{self._API_URL}/api/skipSegments/{hash[:4]}?' + compat_urllib_parse_urlencode({
|
|
|
|
'service': service,
|
|
|
|
'categories': json.dumps(self._categories),
|
2022-02-22 15:18:44 +01:00
|
|
|
'actionTypes': json.dumps(['skip', 'poi'])
|
2021-09-01 22:55:16 +02:00
|
|
|
})
|
2022-03-25 04:01:45 +01:00
|
|
|
for d in self._download_json(url) or []:
|
2021-09-01 22:55:16 +02:00
|
|
|
if d['videoID'] == video_id:
|
|
|
|
return d['segments']
|
|
|
|
return []
|