]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/embedthumbnail.py
[ie/cloudflarestream] Fix `_VALID_URL` and embed extraction (#10215)
[yt-dlp.git] / yt_dlp / postprocessor / embedthumbnail.py
1 import base64
2 import os
3 import re
4 import subprocess
5
6 from .common import PostProcessor
7 from .ffmpeg import FFmpegPostProcessor, FFmpegThumbnailsConvertorPP
8 from ..compat import imghdr
9 from ..dependencies import mutagen
10 from ..utils import (
11 Popen,
12 PostProcessingError,
13 check_executable,
14 encodeArgument,
15 encodeFilename,
16 prepend_extension,
17 shell_quote,
18 )
19
20 if mutagen:
21 from mutagen.flac import FLAC, Picture
22 from mutagen.mp4 import MP4, MP4Cover
23 from mutagen.oggopus import OggOpus
24 from mutagen.oggvorbis import OggVorbis
25
26
27 class EmbedThumbnailPPError(PostProcessingError):
28 pass
29
30
31 class EmbedThumbnailPP(FFmpegPostProcessor):
32
33 def __init__(self, downloader=None, already_have_thumbnail=False):
34 FFmpegPostProcessor.__init__(self, downloader)
35 self._already_have_thumbnail = already_have_thumbnail
36
37 def _get_thumbnail_resolution(self, filename, thumbnail_dict):
38 def guess():
39 width, height = thumbnail_dict.get('width'), thumbnail_dict.get('height')
40 if width and height:
41 return width, height
42
43 try:
44 size_regex = r',\s*(?P<w>\d+)x(?P<h>\d+)\s*[,\[]'
45 size_result = self.run_ffmpeg(filename, None, ['-hide_banner'], expected_retcodes=(1,))
46 mobj = re.search(size_regex, size_result)
47 if mobj is None:
48 return guess()
49 except PostProcessingError as err:
50 self.report_warning(f'unable to find the thumbnail resolution; {err}')
51 return guess()
52 return int(mobj.group('w')), int(mobj.group('h'))
53
54 def _report_run(self, exe, filename):
55 self.to_screen(f'{exe}: Adding thumbnail to "{filename}"')
56
57 @PostProcessor._restrict_to(images=False)
58 def run(self, info):
59 filename = info['filepath']
60 temp_filename = prepend_extension(filename, 'temp')
61
62 if not info.get('thumbnails'):
63 self.to_screen('There aren\'t any thumbnails to embed')
64 return [], info
65
66 idx = next((-i for i, t in enumerate(info['thumbnails'][::-1], 1) if t.get('filepath')), None)
67 if idx is None:
68 self.to_screen('There are no thumbnails on disk')
69 return [], info
70 thumbnail_filename = info['thumbnails'][idx]['filepath']
71 if not os.path.exists(encodeFilename(thumbnail_filename)):
72 self.report_warning('Skipping embedding the thumbnail because the file is missing.')
73 return [], info
74
75 # Correct extension for WebP file with wrong extension (see #25687, #25717)
76 convertor = FFmpegThumbnailsConvertorPP(self._downloader)
77 convertor.fixup_webp(info, idx)
78
79 original_thumbnail = thumbnail_filename = info['thumbnails'][idx]['filepath']
80
81 # Convert unsupported thumbnail formats (see #25687, #25717)
82 # PNG is preferred since JPEG is lossy
83 thumbnail_ext = os.path.splitext(thumbnail_filename)[1][1:]
84 if info['ext'] not in ('mkv', 'mka') and thumbnail_ext not in ('jpg', 'jpeg', 'png'):
85 thumbnail_filename = convertor.convert_thumbnail(thumbnail_filename, 'png')
86 thumbnail_ext = 'png'
87
88 mtime = os.stat(encodeFilename(filename)).st_mtime
89
90 success = True
91 if info['ext'] == 'mp3':
92 options = [
93 '-c', 'copy', '-map', '0:0', '-map', '1:0', '-write_id3v1', '1', '-id3v2_version', '3',
94 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment=Cover (front)']
95
96 self._report_run('ffmpeg', filename)
97 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
98
99 elif info['ext'] in ['mkv', 'mka']:
100 options = list(self.stream_copy_opts())
101
102 mimetype = f'image/{thumbnail_ext.replace("jpg", "jpeg")}'
103 old_stream, new_stream = self.get_stream_number(
104 filename, ('tags', 'mimetype'), mimetype)
105 if old_stream is not None:
106 options.extend(['-map', f'-0:{old_stream}'])
107 new_stream -= 1
108 options.extend([
109 '-attach', self._ffmpeg_filename_argument(thumbnail_filename),
110 f'-metadata:s:{new_stream}', f'mimetype={mimetype}',
111 f'-metadata:s:{new_stream}', f'filename=cover.{thumbnail_ext}'])
112
113 self._report_run('ffmpeg', filename)
114 self.run_ffmpeg(filename, temp_filename, options)
115
116 elif info['ext'] in ['m4a', 'mp4', 'm4v', 'mov']:
117 prefer_atomicparsley = 'embed-thumbnail-atomicparsley' in self.get_param('compat_opts', [])
118 # Method 1: Use mutagen
119 if not mutagen or prefer_atomicparsley:
120 success = False
121 else:
122 try:
123 self._report_run('mutagen', filename)
124 meta = MP4(filename)
125 # NOTE: the 'covr' atom is a non-standard MPEG-4 atom,
126 # Apple iTunes 'M4A' files include the 'moov.udta.meta.ilst' atom.
127 f = {'jpeg': MP4Cover.FORMAT_JPEG, 'png': MP4Cover.FORMAT_PNG}[imghdr.what(thumbnail_filename)]
128 with open(thumbnail_filename, 'rb') as thumbfile:
129 thumb_data = thumbfile.read()
130 meta.tags['covr'] = [MP4Cover(data=thumb_data, imageformat=f)]
131 meta.save()
132 temp_filename = filename
133 except Exception as err:
134 self.report_warning(f'unable to embed using mutagen; {err}')
135 success = False
136
137 # Method 2: Use AtomicParsley
138 if not success:
139 success = True
140 atomicparsley = next((
141 # libatomicparsley.so : See https://github.com/xibr/ytdlp-lazy/issues/1
142 x for x in ['AtomicParsley', 'atomicparsley', 'libatomicparsley.so']
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')
146 success = False
147 else:
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')]
157
158 self._report_run('atomicparsley', filename)
159 self.write_debug(f'AtomicParsley command line: {shell_quote(cmd)}')
160 stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
161 if returncode:
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')
167 success = False
168
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
172 if not success:
173 success = True
174 try:
175 options = [*self.stream_copy_opts(), '-map', '1']
176
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', f'-0:{old_stream}'])
181 new_stream -= 1
182 options.extend([f'-disposition:{new_stream}', 'attached_pic'])
183
184 self._report_run('ffmpeg', filename)
185 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
186 except PostProcessingError as err:
187 success = False
188 raise EmbedThumbnailPPError(f'Unable to embed using ffprobe & ffmpeg; {err}')
189
190 elif info['ext'] in ['ogg', 'opus', 'flac']:
191 if not mutagen:
192 raise EmbedThumbnailPPError('module mutagen was not found. Please install using `python3 -m pip install mutagen`')
193
194 self._report_run('mutagen', filename)
195 f = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](filename)
196
197 pic = Picture()
198 pic.mime = f'image/{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])
203 if res is not None:
204 pic.width, pic.height = res
205
206 if info['ext'] == 'flac':
207 f.add_picture(pic)
208 else:
209 # https://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE
210 f['METADATA_BLOCK_PICTURE'] = base64.b64encode(pic.write()).decode('ascii')
211 f.save()
212 temp_filename = filename
213
214 else:
215 raise EmbedThumbnailPPError('Supported filetypes for thumbnail embedding are: mp3, mkv/mka, ogg/opus/flac, m4a/mp4/m4v/mov')
216
217 if success and temp_filename != filename:
218 os.replace(temp_filename, filename)
219
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,
225 info=info)
226 return [], info