[ie/youtube] Add age-gate workaround for some embeddable videos (#11821)
[yt-dlp.git] / yt_dlp / postprocessor / sponskrub.py
blobac6db1bc7bd2ec274453a635dc3e2eccd9c6982f
1 import os
2 import shlex
3 import subprocess
5 from .common import PostProcessor
6 from ..utils import (
7 Popen,
8 PostProcessingError,
9 check_executable,
10 cli_option,
11 encodeArgument,
12 prepend_extension,
13 shell_quote,
14 str_or_none,
18 # Deprecated in favor of the native implementation
19 class SponSkrubPP(PostProcessor):
20 _temp_ext = 'spons'
21 _exe_name = 'sponskrub'
23 def __init__(self, downloader, path='', args=None, ignoreerror=False, cut=False, force=False, _from_cli=False):
24 PostProcessor.__init__(self, downloader)
25 self.force = force
26 self.cutout = cut
27 self.args = str_or_none(args) or '' # For backward compatibility
28 self.path = self.get_exe(path)
30 if not _from_cli:
31 self.deprecation_warning(
32 'yt_dlp.postprocessor.SponSkrubPP support is deprecated and may be removed in a future version. '
33 'Use yt_dlp.postprocessor.SponsorBlock and yt_dlp.postprocessor.ModifyChaptersPP instead')
35 if not ignoreerror and self.path is None:
36 if path:
37 raise PostProcessingError(f'sponskrub not found in "{path}"')
38 else:
39 raise PostProcessingError('sponskrub not found. Please install or provide the path using --sponskrub-path')
41 def get_exe(self, path=''):
42 if not path or not check_executable(path, ['-h']):
43 path = os.path.join(path, self._exe_name)
44 if not check_executable(path, ['-h']):
45 return None
46 return path
48 @PostProcessor._restrict_to(images=False)
49 def run(self, information):
50 if self.path is None:
51 return [], information
53 filename = information['filepath']
54 if not os.path.exists(filename): # no download
55 return [], information
57 if information['extractor_key'].lower() != 'youtube':
58 self.to_screen('Skipping sponskrub since it is not a YouTube video')
59 return [], information
60 if self.cutout and not self.force and not information.get('__real_download', False):
61 self.report_warning(
62 'Skipping sponskrub since the video was already downloaded. '
63 'Use --sponskrub-force to run sponskrub anyway')
64 return [], information
66 self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
67 if self.cutout:
68 self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
69 if not information.get('__real_download', False):
70 self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
72 temp_filename = prepend_extension(filename, self._temp_ext)
73 if os.path.exists(temp_filename):
74 os.remove(temp_filename)
76 cmd = [self.path]
77 if not self.cutout:
78 cmd += ['-chapter']
79 cmd += cli_option(self._downloader.params, '-proxy', 'proxy')
80 cmd += shlex.split(self.args) # For backward compatibility
81 cmd += self._configuration_args(self._exe_name, use_compat=False)
82 cmd += ['--', information['id'], filename, temp_filename]
83 cmd = [encodeArgument(i) for i in cmd]
85 self.write_debug(f'sponskrub command line: {shell_quote(cmd)}')
86 stdout, _, returncode = Popen.run(cmd, text=True, stdout=None if self.get_param('verbose') else subprocess.PIPE)
88 if not returncode:
89 os.replace(temp_filename, filename)
90 self.to_screen('Sponsor sections have been %s' % ('removed' if self.cutout else 'marked'))
91 elif returncode == 3:
92 self.to_screen('No segments in the SponsorBlock database')
93 else:
94 raise PostProcessingError(
95 stdout.strip().splitlines()[0 if stdout.strip().lower().startswith('unrecognised') else -1]
96 or f'sponskrub failed with error code {returncode}')
97 return [], information