mirror of https://github.com/yt-dlp/yt-dlp.git
Create `to_screen` and similar functions in postprocessor/common
`to_screen`, `report_warning`, `report_error`, `write_debug`, `get_param` This is a first step in standardizing these function. This has to be done eventually for extractors and downloaders too
This commit is contained in:
parent
ebdd9275c3
commit
f446cc6667
|
@ -37,7 +37,25 @@ class PostProcessor(object):
|
||||||
self.PP_NAME = self.__class__.__name__[:-2]
|
self.PP_NAME = self.__class__.__name__[:-2]
|
||||||
|
|
||||||
def to_screen(self, text, *args, **kwargs):
|
def to_screen(self, text, *args, **kwargs):
|
||||||
return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs)
|
if self._downloader:
|
||||||
|
return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs)
|
||||||
|
|
||||||
|
def report_warning(self, text, *args, **kwargs):
|
||||||
|
if self._downloader:
|
||||||
|
return self._downloader.report_warning(text, *args, **kwargs)
|
||||||
|
|
||||||
|
def report_error(self, text, *args, **kwargs):
|
||||||
|
if self._downloader:
|
||||||
|
return self._downloader.report_error(text, *args, **kwargs)
|
||||||
|
|
||||||
|
def write_debug(self, text, *args, **kwargs):
|
||||||
|
if self.get_param('verbose', False):
|
||||||
|
return self._downloader.to_screen('[debug] %s' % text, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_param(self, name, default=None, *args, **kwargs):
|
||||||
|
if self._downloader:
|
||||||
|
return self._downloader.params.get(name, default, *args, **kwargs)
|
||||||
|
return default
|
||||||
|
|
||||||
def set_downloader(self, downloader):
|
def set_downloader(self, downloader):
|
||||||
"""Sets the downloader for this PP."""
|
"""Sets the downloader for this PP."""
|
||||||
|
@ -64,10 +82,10 @@ class PostProcessor(object):
|
||||||
try:
|
try:
|
||||||
os.utime(encodeFilename(path), (atime, mtime))
|
os.utime(encodeFilename(path), (atime, mtime))
|
||||||
except Exception:
|
except Exception:
|
||||||
self._downloader.report_warning(errnote)
|
self.report_warning(errnote)
|
||||||
|
|
||||||
def _configuration_args(self, default=[]):
|
def _configuration_args(self, default=[]):
|
||||||
args = self._downloader.params.get('postprocessor_args', {})
|
args = self.get_param('postprocessor_args', {})
|
||||||
if isinstance(args, list): # for backward compatibility
|
if isinstance(args, list): # for backward compatibility
|
||||||
args = {'default': args, 'sponskrub': []}
|
args = {'default': args, 'sponskrub': []}
|
||||||
return cli_configuration_args(args, self.PP_NAME.lower(), args.get('default', []))
|
return cli_configuration_args(args, self.PP_NAME.lower(), args.get('default', []))
|
||||||
|
|
|
@ -41,8 +41,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
||||||
thumbnail_filename = info['thumbnails'][-1]['filename']
|
thumbnail_filename = info['thumbnails'][-1]['filename']
|
||||||
|
|
||||||
if not os.path.exists(encodeFilename(thumbnail_filename)):
|
if not os.path.exists(encodeFilename(thumbnail_filename)):
|
||||||
self._downloader.report_warning(
|
self.report_warning('Skipping embedding the thumbnail because the file is missing.')
|
||||||
'Skipping embedding the thumbnail because the file is missing.')
|
|
||||||
return [], info
|
return [], info
|
||||||
|
|
||||||
def is_webp(path):
|
def is_webp(path):
|
||||||
|
@ -125,8 +124,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
||||||
|
|
||||||
self.to_screen('Adding thumbnail to "%s"' % filename)
|
self.to_screen('Adding thumbnail to "%s"' % filename)
|
||||||
|
|
||||||
if self._downloader.params.get('verbose', False):
|
self.verbose_message('AtomicParsley command line: %s' % shell_quote(cmd))
|
||||||
self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
|
|
||||||
|
|
||||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
stdout, stderr = process_communicate_or_kill(p)
|
stdout, stderr = process_communicate_or_kill(p)
|
||||||
|
@ -140,7 +138,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
||||||
# for formats that don't support thumbnails (like 3gp) AtomicParsley
|
# for formats that don't support thumbnails (like 3gp) AtomicParsley
|
||||||
# won't create to the temporary file
|
# won't create to the temporary file
|
||||||
if b'No changes' in stdout:
|
if b'No changes' in stdout:
|
||||||
self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
|
self.report_warning('The file format doesn\'t support embedding a thumbnail')
|
||||||
else:
|
else:
|
||||||
os.remove(encodeFilename(filename))
|
os.remove(encodeFilename(filename))
|
||||||
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
|
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
|
||||||
|
|
|
@ -68,8 +68,7 @@ class FFmpegPostProcessor(PostProcessor):
|
||||||
self._versions[self.basename], required_version):
|
self._versions[self.basename], required_version):
|
||||||
warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % (
|
warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % (
|
||||||
self.basename, self.basename, required_version)
|
self.basename, self.basename, required_version)
|
||||||
if self._downloader:
|
self.report_warning(warning)
|
||||||
self._downloader.report_warning(warning)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_versions(downloader=None):
|
def get_versions(downloader=None):
|
||||||
|
@ -99,11 +98,11 @@ class FFmpegPostProcessor(PostProcessor):
|
||||||
self._paths = None
|
self._paths = None
|
||||||
self._versions = None
|
self._versions = None
|
||||||
if self._downloader:
|
if self._downloader:
|
||||||
prefer_ffmpeg = self._downloader.params.get('prefer_ffmpeg', True)
|
prefer_ffmpeg = self.get_param('prefer_ffmpeg', True)
|
||||||
location = self._downloader.params.get('ffmpeg_location')
|
location = self.get_param('ffmpeg_location')
|
||||||
if location is not None:
|
if location is not None:
|
||||||
if not os.path.exists(location):
|
if not os.path.exists(location):
|
||||||
self._downloader.report_warning(
|
self.report_warning(
|
||||||
'ffmpeg-location %s does not exist! '
|
'ffmpeg-location %s does not exist! '
|
||||||
'Continuing without avconv/ffmpeg.' % (location))
|
'Continuing without avconv/ffmpeg.' % (location))
|
||||||
self._versions = {}
|
self._versions = {}
|
||||||
|
@ -111,7 +110,7 @@ class FFmpegPostProcessor(PostProcessor):
|
||||||
elif not os.path.isdir(location):
|
elif not os.path.isdir(location):
|
||||||
basename = os.path.splitext(os.path.basename(location))[0]
|
basename = os.path.splitext(os.path.basename(location))[0]
|
||||||
if basename not in programs:
|
if basename not in programs:
|
||||||
self._downloader.report_warning(
|
self.report_warning(
|
||||||
'Cannot identify executable %s, its basename should be one of %s. '
|
'Cannot identify executable %s, its basename should be one of %s. '
|
||||||
'Continuing without avconv/ffmpeg.' %
|
'Continuing without avconv/ffmpeg.' %
|
||||||
(location, ', '.join(programs)))
|
(location, ', '.join(programs)))
|
||||||
|
@ -177,9 +176,7 @@ class FFmpegPostProcessor(PostProcessor):
|
||||||
encodeFilename(self.executable, True),
|
encodeFilename(self.executable, True),
|
||||||
encodeArgument('-i')]
|
encodeArgument('-i')]
|
||||||
cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
|
cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
|
||||||
if self._downloader.params.get('verbose', False):
|
self.write_debug('%s command line: %s' % (self.basename, shell_quote(cmd)))
|
||||||
self._downloader.to_screen(
|
|
||||||
'[debug] %s command line: %s' % (self.basename, shell_quote(cmd)))
|
|
||||||
handle = subprocess.Popen(
|
handle = subprocess.Popen(
|
||||||
cmd, stderr=subprocess.PIPE,
|
cmd, stderr=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
|
@ -228,8 +225,7 @@ class FFmpegPostProcessor(PostProcessor):
|
||||||
+ [encodeArgument(o) for o in opts]
|
+ [encodeArgument(o) for o in opts]
|
||||||
+ [encodeFilename(self._ffmpeg_filename_argument(out_path), True)])
|
+ [encodeFilename(self._ffmpeg_filename_argument(out_path), True)])
|
||||||
|
|
||||||
if self._downloader.params.get('verbose', False):
|
self.write_debug('ffmpeg command line: %s' % shell_quote(cmd))
|
||||||
self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd))
|
|
||||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
stdout, stderr = process_communicate_or_kill(p)
|
stdout, stderr = process_communicate_or_kill(p)
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
|
@ -566,8 +562,7 @@ class FFmpegMergerPP(FFmpegPostProcessor):
|
||||||
'youtube-dlc will download single file media. '
|
'youtube-dlc will download single file media. '
|
||||||
'Update %s to version %s or newer to fix this.') % (
|
'Update %s to version %s or newer to fix this.') % (
|
||||||
self.basename, self.basename, required_version)
|
self.basename, self.basename, required_version)
|
||||||
if self._downloader:
|
self.report_warning(warning)
|
||||||
self._downloader.report_warning(warning)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -656,7 +651,7 @@ class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor):
|
||||||
new_file = subtitles_filename(filename, lang, new_ext, info.get('ext'))
|
new_file = subtitles_filename(filename, lang, new_ext, info.get('ext'))
|
||||||
|
|
||||||
if ext in ('dfxp', 'ttml', 'tt'):
|
if ext in ('dfxp', 'ttml', 'tt'):
|
||||||
self._downloader.report_warning(
|
self.report_warning(
|
||||||
'You have requested to convert dfxp (TTML) subtitles into another format, '
|
'You have requested to convert dfxp (TTML) subtitles into another format, '
|
||||||
'which results in style information loss')
|
'which results in style information loss')
|
||||||
|
|
||||||
|
|
|
@ -46,16 +46,16 @@ class SponSkrubPP(PostProcessor):
|
||||||
self.to_screen('Skipping sponskrub since it is not a YouTube video')
|
self.to_screen('Skipping sponskrub since it is not a YouTube video')
|
||||||
return [], information
|
return [], information
|
||||||
if self.cutout and not self.force and not information.get('__real_download', False):
|
if self.cutout and not self.force and not information.get('__real_download', False):
|
||||||
self._downloader.to_screen(
|
self.report_warning(
|
||||||
'[sponskrub] Skipping sponskrub since the video was already downloaded. '
|
'Skipping sponskrub since the video was already downloaded. '
|
||||||
'Use --sponskrub-force to run sponskrub anyway')
|
'Use --sponskrub-force to run sponskrub anyway')
|
||||||
return [], information
|
return [], information
|
||||||
|
|
||||||
self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
|
self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
|
||||||
if self.cutout:
|
if self.cutout:
|
||||||
self._downloader.to_screen('WARNING: Cutting out sponsor segments will cause the subtitles to go out of sync.')
|
self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
|
||||||
if not information.get('__real_download', False):
|
if not information.get('__real_download', False):
|
||||||
self._downloader.to_screen('WARNING: If sponskrub is run multiple times, unintended parts of the video could be cut out.')
|
self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
|
||||||
|
|
||||||
filename = information['filepath']
|
filename = information['filepath']
|
||||||
temp_filename = filename + '.' + self._temp_ext + os.path.splitext(filename)[1]
|
temp_filename = filename + '.' + self._temp_ext + os.path.splitext(filename)[1]
|
||||||
|
@ -68,8 +68,7 @@ class SponSkrubPP(PostProcessor):
|
||||||
cmd += ['--', information['id'], filename, temp_filename]
|
cmd += ['--', information['id'], filename, temp_filename]
|
||||||
cmd = [encodeArgument(i) for i in cmd]
|
cmd = [encodeArgument(i) for i in cmd]
|
||||||
|
|
||||||
if self._downloader.params.get('verbose', False):
|
self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
|
||||||
self._downloader.to_screen('[debug] sponskrub command line: %s' % shell_quote(cmd))
|
|
||||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
stdout, stderr = p.communicate()
|
stdout, stderr = p.communicate()
|
||||||
|
|
||||||
|
|
|
@ -57,16 +57,16 @@ class XAttrMetadataPP(PostProcessor):
|
||||||
return [], info
|
return [], info
|
||||||
|
|
||||||
except XAttrUnavailableError as e:
|
except XAttrUnavailableError as e:
|
||||||
self._downloader.report_error(str(e))
|
self.report_error(str(e))
|
||||||
return [], info
|
return [], info
|
||||||
|
|
||||||
except XAttrMetadataError as e:
|
except XAttrMetadataError as e:
|
||||||
if e.reason == 'NO_SPACE':
|
if e.reason == 'NO_SPACE':
|
||||||
self._downloader.report_warning(
|
self.report_warning(
|
||||||
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
||||||
+ (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
|
+ (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
|
||||||
elif e.reason == 'VALUE_TOO_LONG':
|
elif e.reason == 'VALUE_TOO_LONG':
|
||||||
self._downloader.report_warning(
|
self.report_warning(
|
||||||
'Unable to write extended attributes due to too long values.')
|
'Unable to write extended attributes due to too long values.')
|
||||||
else:
|
else:
|
||||||
msg = 'This filesystem doesn\'t support extended attributes. '
|
msg = 'This filesystem doesn\'t support extended attributes. '
|
||||||
|
@ -74,5 +74,5 @@ class XAttrMetadataPP(PostProcessor):
|
||||||
msg += 'You need to use NTFS.'
|
msg += 'You need to use NTFS.'
|
||||||
else:
|
else:
|
||||||
msg += '(You may have to enable them in your /etc/fstab)'
|
msg += '(You may have to enable them in your /etc/fstab)'
|
||||||
self._downloader.report_error(msg)
|
self.report_error(msg)
|
||||||
return [], info
|
return [], info
|
||||||
|
|
Loading…
Reference in New Issue