]> jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/embedthumbnail.py
Update to ytdl-commit-cf2dbec
[yt-dlp.git] / youtube_dlc / postprocessor / embedthumbnail.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 import os
6 import subprocess
7 import struct
8 import re
9 import base64
10
11 try:
12 import mutagen
13 has_mutagen = True
14 except ImportError:
15 has_mutagen = False
16
17 from .ffmpeg import FFmpegPostProcessor
18
19 from ..utils import (
20 check_executable,
21 encodeArgument,
22 encodeFilename,
23 error_to_compat_str,
24 PostProcessingError,
25 prepend_extension,
26 process_communicate_or_kill,
27 replace_extension,
28 shell_quote,
29 )
30
31
32 class EmbedThumbnailPPError(PostProcessingError):
33 pass
34
35
36 class EmbedThumbnailPP(FFmpegPostProcessor):
37
38 def __init__(self, downloader=None, already_have_thumbnail=False):
39 super(EmbedThumbnailPP, self).__init__(downloader)
40 self._already_have_thumbnail = already_have_thumbnail
41
42 def run(self, info):
43 filename = info['filepath']
44 temp_filename = prepend_extension(filename, 'temp')
45
46 if not info.get('thumbnails'):
47 self.to_screen('There aren\'t any thumbnails to embed')
48 return [], info
49
50 original_thumbnail = thumbnail_filename = info['thumbnails'][-1]['filename']
51
52 if not os.path.exists(encodeFilename(thumbnail_filename)):
53 self.report_warning('Skipping embedding the thumbnail because the file is missing.')
54 return [], info
55
56 def is_webp(path):
57 with open(encodeFilename(path), 'rb') as f:
58 b = f.read(12)
59 return b[0:4] == b'RIFF' and b[8:] == b'WEBP'
60
61 # Correct extension for WebP file with wrong extension (see #25687, #25717)
62 _, thumbnail_ext = os.path.splitext(thumbnail_filename)
63 if thumbnail_ext:
64 thumbnail_ext = thumbnail_ext[1:].lower()
65 if thumbnail_ext != 'webp' and is_webp(thumbnail_filename):
66 self.to_screen('Correcting extension to webp and escaping path for thumbnail "%s"' % thumbnail_filename)
67 thumbnail_webp_filename = replace_extension(thumbnail_filename, 'webp')
68 os.rename(encodeFilename(thumbnail_filename), encodeFilename(thumbnail_webp_filename))
69 original_thumbnail = thumbnail_filename = thumbnail_webp_filename
70 thumbnail_ext = 'webp'
71
72 # Convert unsupported thumbnail formats to JPEG (see #25687, #25717)
73 if thumbnail_ext not in ['jpg', 'png']:
74 # NB: % is supposed to be escaped with %% but this does not work
75 # for input files so working around with standard substitution
76 escaped_thumbnail_filename = thumbnail_filename.replace('%', '#')
77 os.rename(encodeFilename(thumbnail_filename), encodeFilename(escaped_thumbnail_filename))
78 escaped_thumbnail_jpg_filename = replace_extension(escaped_thumbnail_filename, 'jpg')
79 self.to_screen('Converting thumbnail "%s" to JPEG' % escaped_thumbnail_filename)
80 self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_jpg_filename, ['-bsf:v', 'mjpeg2jpeg'])
81 thumbnail_jpg_filename = replace_extension(thumbnail_filename, 'jpg')
82 # Rename back to unescaped for further processing
83 os.rename(encodeFilename(escaped_thumbnail_filename), encodeFilename(thumbnail_filename))
84 os.rename(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename))
85 thumbnail_filename = thumbnail_jpg_filename
86 thumbnail_ext = 'jpg'
87
88 success = True
89 if info['ext'] == 'mp3':
90 options = [
91 '-c', 'copy', '-map', '0:0', '-map', '1:0', '-id3v2_version', '3',
92 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"']
93
94 self.to_screen('Adding thumbnail to "%s"' % filename)
95 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
96
97 elif info['ext'] in ['mkv', 'mka']:
98 options = ['-c', 'copy', '-map', '0', '-dn']
99
100 mimetype = 'image/%s' % ('png' if thumbnail_ext == 'png' else 'jpeg')
101 old_stream, new_stream = self.get_stream_number(
102 filename, ('tags', 'mimetype'), mimetype)
103 if old_stream is not None:
104 options.extend(['-map', '-0:%d' % old_stream])
105 new_stream -= 1
106 options.extend([
107 '-attach', thumbnail_filename,
108 '-metadata:s:%d' % new_stream, 'mimetype=%s' % mimetype,
109 '-metadata:s:%d' % new_stream, 'filename=cover.%s' % thumbnail_ext])
110
111 self.to_screen('Adding thumbnail to "%s"' % filename)
112 self.run_ffmpeg(filename, temp_filename, options)
113
114 elif info['ext'] in ['m4a', 'mp4', 'mov']:
115 try:
116 options = ['-c', 'copy', '-map', '0', '-dn', '-map', '1']
117
118 old_stream, new_stream = self.get_stream_number(
119 filename, ('disposition', 'attached_pic'), 1)
120 if old_stream is not None:
121 options.extend(['-map', '-0:%d' % old_stream])
122 new_stream -= 1
123 options.extend(['-disposition:%s' % new_stream, 'attached_pic'])
124
125 self.to_screen('Adding thumbnail to "%s"' % filename)
126 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
127
128 except PostProcessingError as err:
129 self.report_warning('unable to embed using ffprobe & ffmpeg; %s' % error_to_compat_str(err))
130 atomicparsley = next((
131 x for x in ['AtomicParsley', 'atomicparsley']
132 if check_executable(x, ['-v'])), None)
133 if atomicparsley is None:
134 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
135
136 cmd = [encodeFilename(atomicparsley, True),
137 encodeFilename(filename, True),
138 encodeArgument('--artwork'),
139 encodeFilename(thumbnail_filename, True),
140 encodeArgument('-o'),
141 encodeFilename(temp_filename, True)]
142 cmd += [encodeArgument(o) for o in self._configuration_args(exe='AtomicParsley')]
143
144 self.to_screen('Adding thumbnail to "%s"' % filename)
145 self.write_debug('AtomicParsley command line: %s' % shell_quote(cmd))
146 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
147 stdout, stderr = process_communicate_or_kill(p)
148 if p.returncode != 0:
149 msg = stderr.decode('utf-8', 'replace').strip()
150 raise EmbedThumbnailPPError(msg)
151 # for formats that don't support thumbnails (like 3gp) AtomicParsley
152 # won't create to the temporary file
153 if b'No changes' in stdout:
154 self.report_warning('The file format doesn\'t support embedding a thumbnail')
155 success = False
156
157 elif info['ext'] in ['ogg', 'opus']:
158 if not has_mutagen:
159 raise EmbedThumbnailPPError('module mutagen was not found. Please install using `python -m pip install mutagen`')
160 self.to_screen('Adding thumbnail to "%s"' % filename)
161
162 size_regex = r',\s*(?P<w>\d+)x(?P<h>\d+)\s*[,\[]'
163 size_result = self.run_ffmpeg(thumbnail_filename, thumbnail_filename, ['-hide_banner'])
164 mobj = re.search(size_regex, size_result)
165 width, height = int(mobj.group('w')), int(mobj.group('h'))
166 mimetype = ('image/%s' % ('png' if thumbnail_ext == 'png' else 'jpeg')).encode('ascii')
167
168 # https://xiph.org/flac/format.html#metadata_block_picture
169 data = bytearray()
170 data += struct.pack('>II', 3, len(mimetype))
171 data += mimetype
172 data += struct.pack('>IIIIII', 0, width, height, 8, 0, os.stat(thumbnail_filename).st_size) # 32 if png else 24
173
174 fin = open(thumbnail_filename, "rb")
175 data += fin.read()
176 fin.close()
177
178 temp_filename = filename
179 f = mutagen.File(temp_filename)
180 f.tags['METADATA_BLOCK_PICTURE'] = base64.b64encode(data).decode('ascii')
181 f.save()
182
183 else:
184 raise EmbedThumbnailPPError('Supported filetypes for thumbnail embedding are: mp3, mkv/mka, ogg/opus, m4a/mp4/mov')
185
186 if success and temp_filename != filename:
187 os.remove(encodeFilename(filename))
188 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
189
190 files_to_delete = [thumbnail_filename]
191 if self._already_have_thumbnail:
192 info['__files_to_move'][original_thumbnail] = replace_extension(
193 info['__thumbnail_filename'], os.path.splitext(original_thumbnail)[1][1:])
194 if original_thumbnail == thumbnail_filename:
195 files_to_delete = []
196 return files_to_delete, info