6 from .common
import PostProcessor
7 from .ffmpeg
import FFmpegPostProcessor
, FFmpegThumbnailsConvertorPP
8 from ..compat
import imghdr
9 from ..dependencies
import mutagen
22 from mutagen
.flac
import FLAC
, Picture
23 from mutagen
.mp4
import MP4
, MP4Cover
24 from mutagen
.oggopus
import OggOpus
25 from mutagen
.oggvorbis
import OggVorbis
28 class EmbedThumbnailPPError(PostProcessingError
):
32 class EmbedThumbnailPP(FFmpegPostProcessor
):
34 def __init__(self
, downloader
=None, already_have_thumbnail
=False):
35 FFmpegPostProcessor
.__init
__(self
, downloader
)
36 self
._already
_have
_thumbnail
= already_have_thumbnail
38 def _get_thumbnail_resolution(self
, filename
, thumbnail_dict
):
40 width
, height
= thumbnail_dict
.get('width'), thumbnail_dict
.get('height')
45 size_regex
= r
',\s*(?P<w>\d+)x(?P<h>\d+)\s*[,\[]'
46 size_result
= self
.run_ffmpeg(filename
, None, ['-hide_banner'], expected_retcodes
=(1,))
47 mobj
= re
.search(size_regex
, size_result
)
50 except PostProcessingError
as err
:
51 self
.report_warning('unable to find the thumbnail resolution; %s' % error_to_compat_str(err
))
53 return int(mobj
.group('w')), int(mobj
.group('h'))
55 def _report_run(self
, exe
, filename
):
56 self
.to_screen(f
'{exe}: Adding thumbnail to "{filename}"')
58 @PostProcessor._restrict_to(images
=False)
60 filename
= info
['filepath']
61 temp_filename
= prepend_extension(filename
, 'temp')
63 if not info
.get('thumbnails'):
64 self
.to_screen('There aren\'t any thumbnails to embed')
67 idx
= next((-i
for i
, t
in enumerate(info
['thumbnails'][::-1], 1) if t
.get('filepath')), None)
69 self
.to_screen('There are no thumbnails on disk')
71 thumbnail_filename
= info
['thumbnails'][idx
]['filepath']
72 if not os
.path
.exists(encodeFilename(thumbnail_filename
)):
73 self
.report_warning('Skipping embedding the thumbnail because the file is missing.')
76 # Correct extension for WebP file with wrong extension (see #25687, #25717)
77 convertor
= FFmpegThumbnailsConvertorPP(self
._downloader
)
78 convertor
.fixup_webp(info
, idx
)
80 original_thumbnail
= thumbnail_filename
= info
['thumbnails'][idx
]['filepath']
82 # Convert unsupported thumbnail formats (see #25687, #25717)
83 # PNG is preferred since JPEG is lossy
84 thumbnail_ext
= os
.path
.splitext(thumbnail_filename
)[1][1:]
85 if info
['ext'] not in ('mkv', 'mka') and thumbnail_ext
not in ('jpg', 'jpeg', 'png'):
86 thumbnail_filename
= convertor
.convert_thumbnail(thumbnail_filename
, 'png')
89 mtime
= os
.stat(encodeFilename(filename
)).st_mtime
92 if info
['ext'] == 'mp3':
94 '-c', 'copy', '-map', '0:0', '-map', '1:0', '-write_id3v1', '1', '-id3v2_version', '3',
95 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"']
97 self
._report
_run
('ffmpeg', filename
)
98 self
.run_ffmpeg_multiple_files([filename
, thumbnail_filename
], temp_filename
, options
)
100 elif info
['ext'] in ['mkv', 'mka']:
101 options
= list(self
.stream_copy_opts())
103 mimetype
= f
'image/{thumbnail_ext.replace("jpg", "jpeg")}'
104 old_stream
, new_stream
= self
.get_stream_number(
105 filename
, ('tags', 'mimetype'), mimetype
)
106 if old_stream
is not None:
107 options
.extend(['-map', '-0:%d' % old_stream
])
110 '-attach', thumbnail_filename
,
111 '-metadata:s:%d' % new_stream
, 'mimetype=%s' % mimetype
,
112 '-metadata:s:%d' % new_stream
, 'filename=cover.%s' % thumbnail_ext
])
114 self
._report
_run
('ffmpeg', filename
)
115 self
.run_ffmpeg(filename
, temp_filename
, options
)
117 elif info
['ext'] in ['m4a', 'mp4', 'mov']:
118 prefer_atomicparsley
= 'embed-thumbnail-atomicparsley' in self
.get_param('compat_opts', [])
119 # Method 1: Use mutagen
120 if not mutagen
or prefer_atomicparsley
:
124 self
._report
_run
('mutagen', filename
)
126 # NOTE: the 'covr' atom is a non-standard MPEG-4 atom,
127 # Apple iTunes 'M4A' files include the 'moov.udta.meta.ilst' atom.
128 f
= {'jpeg': MP4Cover.FORMAT_JPEG, 'png': MP4Cover.FORMAT_PNG}
[imghdr
.what(thumbnail_filename
)]
129 with open(thumbnail_filename
, 'rb') as thumbfile
:
130 thumb_data
= thumbfile
.read()
131 meta
.tags
['covr'] = [MP4Cover(data
=thumb_data
, imageformat
=f
)]
133 temp_filename
= filename
134 except Exception as err
:
135 self
.report_warning('unable to embed using mutagen; %s' % error_to_compat_str(err
))
138 # Method 2: Use AtomicParsley
141 atomicparsley
= next((
142 x
for x
in ['AtomicParsley', 'atomicparsley']
143 if check_executable(x
, ['-v'])), None)
144 if atomicparsley
is None:
145 self
.to_screen('Neither mutagen nor AtomicParsley was found. Falling back to ffmpeg')
148 if not prefer_atomicparsley
:
149 self
.to_screen('mutagen was not found. Falling back to AtomicParsley')
150 cmd
= [encodeFilename(atomicparsley
, True),
151 encodeFilename(filename
, True),
152 encodeArgument('--artwork'),
153 encodeFilename(thumbnail_filename
, True),
154 encodeArgument('-o'),
155 encodeFilename(temp_filename
, True)]
156 cmd
+= [encodeArgument(o
) for o
in self
._configuration
_args
('AtomicParsley')]
158 self
._report
_run
('atomicparsley', filename
)
159 self
.write_debug('AtomicParsley command line: %s' % shell_quote(cmd
))
160 stdout
, stderr
, returncode
= Popen
.run(cmd
, text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
162 self
.report_warning(f
'Unable to embed thumbnails using AtomicParsley; {stderr.strip()}')
163 # for formats that don't support thumbnails (like 3gp) AtomicParsley
164 # won't create to the temporary file
165 if 'No changes' in stdout
:
166 self
.report_warning('The file format doesn\'t support embedding a thumbnail')
169 # Method 3: Use ffmpeg+ffprobe
170 # Thumbnails attached using this method doesn't show up as cover in some cases
171 # See https://github.com/yt-dlp/yt-dlp/issues/2125, https://github.com/yt-dlp/yt-dlp/issues/411
175 options
= [*self
.stream_copy_opts(), '-map', '1']
177 old_stream
, new_stream
= self
.get_stream_number(
178 filename
, ('disposition', 'attached_pic'), 1)
179 if old_stream
is not None:
180 options
.extend(['-map', '-0:%d' % old_stream
])
182 options
.extend(['-disposition:%s' % new_stream
, 'attached_pic'])
184 self
._report
_run
('ffmpeg', filename
)
185 self
.run_ffmpeg_multiple_files([filename
, thumbnail_filename
], temp_filename
, options
)
186 except PostProcessingError
as err
:
188 raise EmbedThumbnailPPError(f
'Unable to embed using ffprobe & ffmpeg; {err}')
190 elif info
['ext'] in ['ogg', 'opus', 'flac']:
192 raise EmbedThumbnailPPError('module mutagen was not found. Please install using `python -m pip install mutagen`')
194 self
._report
_run
('mutagen', filename
)
195 f
= {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}
[info
['ext']](filename
)
198 pic
.mime
= 'image/%s' % imghdr
.what(thumbnail_filename
)
199 with open(thumbnail_filename
, 'rb') as thumbfile
:
200 pic
.data
= thumbfile
.read()
201 pic
.type = 3 # front cover
202 res
= self
._get
_thumbnail
_resolution
(thumbnail_filename
, info
['thumbnails'][idx
])
204 pic
.width
, pic
.height
= res
206 if info
['ext'] == 'flac':
209 # https://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE
210 f
['METADATA_BLOCK_PICTURE'] = base64
.b64encode(pic
.write()).decode('ascii')
212 temp_filename
= filename
215 raise EmbedThumbnailPPError('Supported filetypes for thumbnail embedding are: mp3, mkv/mka, ogg/opus/flac, m4a/mp4/mov')
217 if success
and temp_filename
!= filename
:
218 os
.replace(temp_filename
, filename
)
220 self
.try_utime(filename
, mtime
, mtime
)
221 converted
= original_thumbnail
!= thumbnail_filename
222 self
._delete
_downloaded
_files
(
223 thumbnail_filename
if converted
or not self
._already
_have
_thumbnail
else None,
224 original_thumbnail
if converted
and not self
._already
_have
_thumbnail
else None,