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