]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/sponskrub.py
Native SponsorBlock implementation and related improvements (#360)
[yt-dlp.git] / yt_dlp / postprocessor / sponskrub.py
1 from __future__ import unicode_literals
2 import os
3 import subprocess
4
5 from .common import PostProcessor
6 from ..compat import compat_shlex_split
7 from ..utils import (
8 check_executable,
9 cli_option,
10 encodeArgument,
11 encodeFilename,
12 shell_quote,
13 str_or_none,
14 PostProcessingError,
15 prepend_extension,
16 process_communicate_or_kill,
17 )
18
19
20 # Deprecated in favor of the native implementation
21 class SponSkrubPP(PostProcessor):
22 _temp_ext = 'spons'
23 _exe_name = 'sponskrub'
24
25 def __init__(self, downloader, path='', args=None, ignoreerror=False, cut=False, force=False):
26 PostProcessor.__init__(self, downloader)
27 self.force = force
28 self.cutout = cut
29 self.args = str_or_none(args) or '' # For backward compatibility
30 self.path = self.get_exe(path)
31
32 if not ignoreerror and self.path is None:
33 if path:
34 raise PostProcessingError('sponskrub not found in "%s"' % path)
35 else:
36 raise PostProcessingError('sponskrub not found. Please install or provide the path using --sponskrub-path')
37
38 def get_exe(self, path=''):
39 if not path or not check_executable(path, ['-h']):
40 path = os.path.join(path, self._exe_name)
41 if not check_executable(path, ['-h']):
42 return None
43 return path
44
45 @PostProcessor._restrict_to(images=False)
46 def run(self, information):
47 if self.path is None:
48 return [], information
49
50 filename = information['filepath']
51 if not os.path.exists(encodeFilename(filename)): # no download
52 return [], information
53
54 if information['extractor_key'].lower() != 'youtube':
55 self.to_screen('Skipping sponskrub since it is not a YouTube video')
56 return [], information
57 if self.cutout and not self.force and not information.get('__real_download', False):
58 self.report_warning(
59 'Skipping sponskrub since the video was already downloaded. '
60 'Use --sponskrub-force to run sponskrub anyway')
61 return [], information
62
63 self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
64 if self.cutout:
65 self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
66 if not information.get('__real_download', False):
67 self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
68
69 temp_filename = prepend_extension(filename, self._temp_ext)
70 if os.path.exists(encodeFilename(temp_filename)):
71 os.remove(encodeFilename(temp_filename))
72
73 cmd = [self.path]
74 if not self.cutout:
75 cmd += ['-chapter']
76 cmd += cli_option(self._downloader.params, '-proxy', 'proxy')
77 cmd += compat_shlex_split(self.args) # For backward compatibility
78 cmd += self._configuration_args(self._exe_name, use_compat=False)
79 cmd += ['--', information['id'], filename, temp_filename]
80 cmd = [encodeArgument(i) for i in cmd]
81
82 self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
83 pipe = None if self.get_param('verbose') else subprocess.PIPE
84 p = subprocess.Popen(cmd, stdout=pipe)
85 stdout = process_communicate_or_kill(p)[0]
86
87 if p.returncode == 0:
88 os.replace(temp_filename, filename)
89 self.to_screen('Sponsor sections have been %s' % ('removed' if self.cutout else 'marked'))
90 elif p.returncode == 3:
91 self.to_screen('No segments in the SponsorBlock database')
92 else:
93 msg = stdout.decode('utf-8', 'replace').strip() if stdout else ''
94 msg = msg.split('\n')[0 if msg.lower().startswith('unrecognised') else -1]
95 raise PostProcessingError(msg if msg else 'sponskrub failed with error code %s' % p.returncode)
96 return [], information