diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-08-05 09:06:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-08-05 09:06:11 +0000 |
commit | fd5a06560caab95c71a2e2e805efa8d0f3a696a0 (patch) | |
tree | e1c600b8612bc4b301e2f51b875fcd835c5008cc /yt_dlp/postprocessor/embedthumbnail.py | |
parent | Releasing progress-linux version 2024.05.27-1~progress7.99u1. (diff) | |
download | yt-dlp-fd5a06560caab95c71a2e2e805efa8d0f3a696a0.tar.xz yt-dlp-fd5a06560caab95c71a2e2e805efa8d0f3a696a0.zip |
Merging upstream version 2024.07.01.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'yt_dlp/postprocessor/embedthumbnail.py')
-rw-r--r-- | yt_dlp/postprocessor/embedthumbnail.py | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/yt_dlp/postprocessor/embedthumbnail.py b/yt_dlp/postprocessor/embedthumbnail.py index 9c53729..f2228ac 100644 --- a/yt_dlp/postprocessor/embedthumbnail.py +++ b/yt_dlp/postprocessor/embedthumbnail.py @@ -13,7 +13,6 @@ from ..utils import ( check_executable, encodeArgument, encodeFilename, - error_to_compat_str, prepend_extension, shell_quote, ) @@ -48,7 +47,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): if mobj is None: return guess() except PostProcessingError as err: - self.report_warning('unable to find the thumbnail resolution; %s' % error_to_compat_str(err)) + self.report_warning(f'unable to find the thumbnail resolution; {err}') return guess() return int(mobj.group('w')), int(mobj.group('h')) @@ -104,12 +103,12 @@ class EmbedThumbnailPP(FFmpegPostProcessor): old_stream, new_stream = self.get_stream_number( filename, ('tags', 'mimetype'), mimetype) if old_stream is not None: - options.extend(['-map', '-0:%d' % old_stream]) + options.extend(['-map', f'-0:{old_stream}']) new_stream -= 1 options.extend([ '-attach', self._ffmpeg_filename_argument(thumbnail_filename), - '-metadata:s:%d' % new_stream, 'mimetype=%s' % mimetype, - '-metadata:s:%d' % new_stream, 'filename=cover.%s' % thumbnail_ext]) + f'-metadata:s:{new_stream}', f'mimetype={mimetype}', + f'-metadata:s:{new_stream}', f'filename=cover.{thumbnail_ext}']) self._report_run('ffmpeg', filename) self.run_ffmpeg(filename, temp_filename, options) @@ -120,19 +119,26 @@ class EmbedThumbnailPP(FFmpegPostProcessor): if not mutagen or prefer_atomicparsley: success = False else: + self._report_run('mutagen', filename) + f = {'jpeg': MP4Cover.FORMAT_JPEG, 'png': MP4Cover.FORMAT_PNG} try: - self._report_run('mutagen', filename) + with open(thumbnail_filename, 'rb') as thumbfile: + thumb_data = thumbfile.read() + + type_ = imghdr.what(h=thumb_data) + if not type_: + raise ValueError('could not determine image type') + elif type_ not in f: + raise ValueError(f'incompatible image type: {type_}') + meta = MP4(filename) # NOTE: the 'covr' atom is a non-standard MPEG-4 atom, # Apple iTunes 'M4A' files include the 'moov.udta.meta.ilst' atom. - f = {'jpeg': MP4Cover.FORMAT_JPEG, 'png': MP4Cover.FORMAT_PNG}[imghdr.what(thumbnail_filename)] - with open(thumbnail_filename, 'rb') as thumbfile: - thumb_data = thumbfile.read() meta.tags['covr'] = [MP4Cover(data=thumb_data, imageformat=f)] meta.save() temp_filename = filename except Exception as err: - self.report_warning('unable to embed using mutagen; %s' % error_to_compat_str(err)) + self.report_warning(f'unable to embed using mutagen; {err}') success = False # Method 2: Use AtomicParsley @@ -157,13 +163,14 @@ class EmbedThumbnailPP(FFmpegPostProcessor): cmd += [encodeArgument(o) for o in self._configuration_args('AtomicParsley')] self._report_run('atomicparsley', filename) - self.write_debug('AtomicParsley command line: %s' % shell_quote(cmd)) + self.write_debug(f'AtomicParsley command line: {shell_quote(cmd)}') stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if returncode: self.report_warning(f'Unable to embed thumbnails using AtomicParsley; {stderr.strip()}') + success = False # for formats that don't support thumbnails (like 3gp) AtomicParsley # won't create to the temporary file - if 'No changes' in stdout: + elif 'No changes' in stdout: self.report_warning('The file format doesn\'t support embedding a thumbnail') success = False @@ -178,9 +185,9 @@ class EmbedThumbnailPP(FFmpegPostProcessor): old_stream, new_stream = self.get_stream_number( filename, ('disposition', 'attached_pic'), 1) if old_stream is not None: - options.extend(['-map', '-0:%d' % old_stream]) + options.extend(['-map', f'-0:{old_stream}']) new_stream -= 1 - options.extend(['-disposition:%s' % new_stream, 'attached_pic']) + options.extend([f'-disposition:{new_stream}', 'attached_pic']) self._report_run('ffmpeg', filename) self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options) @@ -196,7 +203,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): f = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](filename) pic = Picture() - pic.mime = 'image/%s' % imghdr.what(thumbnail_filename) + pic.mime = f'image/{imghdr.what(thumbnail_filename)}' with open(thumbnail_filename, 'rb') as thumbfile: pic.data = thumbfile.read() pic.type = 3 # front cover |