2020-11-15 01:28:41 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
|
|
|
from ..compat import compat_shlex_split
|
|
|
|
from ..utils import (
|
|
|
|
check_executable,
|
2021-03-15 01:57:21 +01:00
|
|
|
cli_option,
|
2020-11-15 01:28:41 +01:00
|
|
|
encodeArgument,
|
2021-01-18 00:52:47 +01:00
|
|
|
encodeFilename,
|
2020-11-15 01:28:41 +01:00
|
|
|
shell_quote,
|
2021-01-20 17:07:40 +01:00
|
|
|
str_or_none,
|
2021-10-20 18:19:40 +02:00
|
|
|
Popen,
|
2020-11-15 01:28:41 +01:00
|
|
|
PostProcessingError,
|
2021-01-18 00:52:47 +01:00
|
|
|
prepend_extension,
|
2020-11-15 01:28:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-01 22:55:16 +02:00
|
|
|
# Deprecated in favor of the native implementation
|
2020-11-15 01:28:41 +01:00
|
|
|
class SponSkrubPP(PostProcessor):
|
|
|
|
_temp_ext = 'spons'
|
|
|
|
_exe_name = 'sponskrub'
|
|
|
|
|
|
|
|
def __init__(self, downloader, path='', args=None, ignoreerror=False, cut=False, force=False):
|
|
|
|
PostProcessor.__init__(self, downloader)
|
|
|
|
self.force = force
|
|
|
|
self.cutout = cut
|
2021-01-20 17:07:40 +01:00
|
|
|
self.args = str_or_none(args) or '' # For backward compatibility
|
2020-11-15 01:28:41 +01:00
|
|
|
self.path = self.get_exe(path)
|
|
|
|
|
|
|
|
if not ignoreerror and self.path is None:
|
|
|
|
if path:
|
|
|
|
raise PostProcessingError('sponskrub not found in "%s"' % path)
|
|
|
|
else:
|
2021-03-20 04:20:08 +01:00
|
|
|
raise PostProcessingError('sponskrub not found. Please install or provide the path using --sponskrub-path')
|
2020-11-15 01:28:41 +01:00
|
|
|
|
|
|
|
def get_exe(self, path=''):
|
|
|
|
if not path or not check_executable(path, ['-h']):
|
|
|
|
path = os.path.join(path, self._exe_name)
|
|
|
|
if not check_executable(path, ['-h']):
|
|
|
|
return None
|
|
|
|
return path
|
|
|
|
|
2021-06-12 22:02:19 +02:00
|
|
|
@PostProcessor._restrict_to(images=False)
|
2020-11-15 01:28:41 +01:00
|
|
|
def run(self, information):
|
|
|
|
if self.path is None:
|
|
|
|
return [], information
|
|
|
|
|
2021-02-08 11:18:12 +01:00
|
|
|
filename = information['filepath']
|
|
|
|
if not os.path.exists(encodeFilename(filename)): # no download
|
|
|
|
return [], information
|
|
|
|
|
2020-11-15 01:28:41 +01:00
|
|
|
if information['extractor_key'].lower() != 'youtube':
|
2021-01-07 20:28:41 +01:00
|
|
|
self.to_screen('Skipping sponskrub since it is not a YouTube video')
|
2020-11-15 01:28:41 +01:00
|
|
|
return [], information
|
|
|
|
if self.cutout and not self.force and not information.get('__real_download', False):
|
2021-01-10 14:44:54 +01:00
|
|
|
self.report_warning(
|
|
|
|
'Skipping sponskrub since the video was already downloaded. '
|
2020-11-15 01:28:41 +01:00
|
|
|
'Use --sponskrub-force to run sponskrub anyway')
|
|
|
|
return [], information
|
|
|
|
|
2021-01-07 20:28:41 +01:00
|
|
|
self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
|
2020-11-15 01:28:41 +01:00
|
|
|
if self.cutout:
|
2021-01-10 14:44:54 +01:00
|
|
|
self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
|
2020-11-15 01:28:41 +01:00
|
|
|
if not information.get('__real_download', False):
|
2021-01-10 14:44:54 +01:00
|
|
|
self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
|
2020-11-15 01:28:41 +01:00
|
|
|
|
2021-01-18 00:52:47 +01:00
|
|
|
temp_filename = prepend_extension(filename, self._temp_ext)
|
|
|
|
if os.path.exists(encodeFilename(temp_filename)):
|
|
|
|
os.remove(encodeFilename(temp_filename))
|
2020-11-15 01:28:41 +01:00
|
|
|
|
2021-01-20 21:07:02 +01:00
|
|
|
cmd = [self.path]
|
2021-01-20 17:07:40 +01:00
|
|
|
if not self.cutout:
|
|
|
|
cmd += ['-chapter']
|
2021-03-15 01:57:21 +01:00
|
|
|
cmd += cli_option(self._downloader.params, '-proxy', 'proxy')
|
2021-01-20 17:07:40 +01:00
|
|
|
cmd += compat_shlex_split(self.args) # For backward compatibility
|
2021-03-09 03:17:21 +01:00
|
|
|
cmd += self._configuration_args(self._exe_name, use_compat=False)
|
2020-11-15 01:28:41 +01:00
|
|
|
cmd += ['--', information['id'], filename, temp_filename]
|
|
|
|
cmd = [encodeArgument(i) for i in cmd]
|
|
|
|
|
2021-01-10 14:44:54 +01:00
|
|
|
self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
|
2021-02-11 20:55:16 +01:00
|
|
|
pipe = None if self.get_param('verbose') else subprocess.PIPE
|
2021-10-20 18:19:40 +02:00
|
|
|
p = Popen(cmd, stdout=pipe)
|
|
|
|
stdout = p.communicate_or_kill()[0]
|
2020-11-15 01:28:41 +01:00
|
|
|
|
|
|
|
if p.returncode == 0:
|
2021-08-27 04:27:20 +02:00
|
|
|
os.replace(temp_filename, filename)
|
2021-01-07 20:28:41 +01:00
|
|
|
self.to_screen('Sponsor sections have been %s' % ('removed' if self.cutout else 'marked'))
|
|
|
|
elif p.returncode == 3:
|
|
|
|
self.to_screen('No segments in the SponsorBlock database')
|
|
|
|
else:
|
2021-02-11 20:55:16 +01:00
|
|
|
msg = stdout.decode('utf-8', 'replace').strip() if stdout else ''
|
|
|
|
msg = msg.split('\n')[0 if msg.lower().startswith('unrecognised') else -1]
|
2021-01-20 21:07:02 +01:00
|
|
|
raise PostProcessingError(msg if msg else 'sponskrub failed with error code %s' % p.returncode)
|
2020-11-15 01:28:41 +01:00
|
|
|
return [], information
|