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