5 from .common
import PostProcessor
18 # Deprecated in favor of the native implementation
19 class SponSkrubPP(PostProcessor
):
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
)
27 self
.args
= str_or_none(args
) or '' # For backward compatibility
28 self
.path
= self
.get_exe(path
)
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:
37 raise PostProcessingError(f
'sponskrub not found in "{path}"')
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']):
48 @PostProcessor._restrict
_to
(images
=False)
49 def run(self
, information
):
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):
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'))
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
)
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
)
89 os
.replace(temp_filename
, filename
)
90 self
.to_screen('Sponsor sections have been %s' % ('removed' if self
.cutout
else 'marked'))
92 self
.to_screen('No segments in the SponsorBlock database')
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