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