]> jfr.im git - yt-dlp.git/blame - yt_dlp/postprocessor/sponskrub.py
[cleanup] Misc cleanup
[yt-dlp.git] / yt_dlp / postprocessor / sponskrub.py
CommitLineData
a9e7f546 1from __future__ import unicode_literals
2import os
3import subprocess
4
5from .common import PostProcessor
6from ..compat import compat_shlex_split
7from ..utils import (
8 check_executable,
d034ab66 9 cli_option,
a9e7f546 10 encodeArgument,
477cf32f 11 encodeFilename,
a9e7f546 12 shell_quote,
43820c03 13 str_or_none,
d3c93ec2 14 Popen,
a9e7f546 15 PostProcessingError,
477cf32f 16 prepend_extension,
a9e7f546 17)
18
19
7a340e0d 20# Deprecated in favor of the native implementation
a9e7f546 21class SponSkrubPP(PostProcessor):
22 _temp_ext = 'spons'
a9e7f546 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
43820c03 29 self.args = str_or_none(args) or '' # For backward compatibility
a9e7f546 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:
beb4b92a 36 raise PostProcessingError('sponskrub not found. Please install or provide the path using --sponskrub-path')
a9e7f546 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
8326b00a 45 @PostProcessor._restrict_to(images=False)
a9e7f546 46 def run(self, information):
47 if self.path is None:
48 return [], information
49
1bf540d2 50 filename = information['filepath']
51 if not os.path.exists(encodeFilename(filename)): # no download
52 return [], information
53
a9e7f546 54 if information['extractor_key'].lower() != 'youtube':
1b77b347 55 self.to_screen('Skipping sponskrub since it is not a YouTube video')
a9e7f546 56 return [], information
57 if self.cutout and not self.force and not information.get('__real_download', False):
f446cc66 58 self.report_warning(
59 'Skipping sponskrub since the video was already downloaded. '
a9e7f546 60 'Use --sponskrub-force to run sponskrub anyway')
61 return [], information
62
1b77b347 63 self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
a9e7f546 64 if self.cutout:
f446cc66 65 self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
a9e7f546 66 if not information.get('__real_download', False):
f446cc66 67 self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
a9e7f546 68
477cf32f 69 temp_filename = prepend_extension(filename, self._temp_ext)
70 if os.path.exists(encodeFilename(temp_filename)):
71 os.remove(encodeFilename(temp_filename))
a9e7f546 72
fbced734 73 cmd = [self.path]
43820c03 74 if not self.cutout:
75 cmd += ['-chapter']
d034ab66 76 cmd += cli_option(self._downloader.params, '-proxy', 'proxy')
43820c03 77 cmd += compat_shlex_split(self.args) # For backward compatibility
e92caff5 78 cmd += self._configuration_args(self._exe_name, use_compat=False)
a9e7f546 79 cmd += ['--', information['id'], filename, temp_filename]
80 cmd = [encodeArgument(i) for i in cmd]
81
f446cc66 82 self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
584bab37 83 pipe = None if self.get_param('verbose') else subprocess.PIPE
d3c93ec2 84 p = Popen(cmd, stdout=pipe)
85 stdout = p.communicate_or_kill()[0]
a9e7f546 86
87 if p.returncode == 0:
d75201a8 88 os.replace(temp_filename, filename)
1b77b347 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:
584bab37 93 msg = stdout.decode('utf-8', 'replace').strip() if stdout else ''
94 msg = msg.split('\n')[0 if msg.lower().startswith('unrecognised') else -1]
fbced734 95 raise PostProcessingError(msg if msg else 'sponskrub failed with error code %s' % p.returncode)
a9e7f546 96 return [], information