2 from __future__
import unicode_literals
8 from .ffmpeg
import FFmpegPostProcessor
21 class EmbedThumbnailPPError(PostProcessingError
):
25 class EmbedThumbnailPP(FFmpegPostProcessor
):
26 def __init__(self
, downloader
=None, already_have_thumbnail
=False):
27 super(EmbedThumbnailPP
, self
).__init
__(downloader
)
28 self
._already
_have
_thumbnail
= already_have_thumbnail
31 filename
= info
['filepath']
32 temp_filename
= prepend_extension(filename
, 'temp')
34 if not info
.get('thumbnails'):
35 self
._downloader
.to_screen('[embedthumbnail] There aren\'t any thumbnails to embed')
38 thumbnail_filename
= info
['thumbnails'][-1]['filename']
40 if not os
.path
.exists(encodeFilename(thumbnail_filename
)):
41 self
._downloader
.report_warning(
42 'Skipping embedding the thumbnail because the file is missing.')
46 with open(encodeFilename(path
), 'rb') as f
:
48 return b
[0:4] == b
'RIFF' and b
[8:] == b
'WEBP'
50 # Correct extension for WebP file with wrong extension (see #25687, #25717)
51 _
, thumbnail_ext
= os
.path
.splitext(thumbnail_filename
)
53 thumbnail_ext
= thumbnail_ext
[1:].lower()
54 if thumbnail_ext
!= 'webp' and is_webp(thumbnail_filename
):
55 self
._downloader
.to_screen(
56 '[ffmpeg] Correcting extension to webp and escaping path for thumbnail "%s"' % thumbnail_filename
)
57 thumbnail_webp_filename
= replace_extension(thumbnail_filename
, 'webp')
58 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename(thumbnail_webp_filename
))
59 thumbnail_filename
= thumbnail_webp_filename
60 thumbnail_ext
= 'webp'
62 # Convert unsupported thumbnail formats to JPEG (see #25687, #25717)
63 if thumbnail_ext
not in ['jpg', 'png']:
64 # NB: % is supposed to be escaped with %% but this does not work
65 # for input files so working around with standard substitution
66 escaped_thumbnail_filename
= thumbnail_filename
.replace('%', '#')
67 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename(escaped_thumbnail_filename
))
68 escaped_thumbnail_jpg_filename
= replace_extension(escaped_thumbnail_filename
, 'jpg')
69 self
._downloader
.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % escaped_thumbnail_filename
)
70 self
.run_ffmpeg(escaped_thumbnail_filename
, escaped_thumbnail_jpg_filename
, ['-bsf:v', 'mjpeg2jpeg'])
71 os
.remove(encodeFilename(escaped_thumbnail_filename
))
72 thumbnail_jpg_filename
= replace_extension(thumbnail_filename
, 'jpg')
73 # Rename back to unescaped for further processing
74 os
.rename(encodeFilename(escaped_thumbnail_jpg_filename
), encodeFilename(thumbnail_jpg_filename
))
75 thumbnail_filename
= thumbnail_jpg_filename
77 if info
['ext'] == 'mp3':
79 '-c', 'copy', '-map', '0:0', '-map', '1:0', '-id3v2_version', '3',
80 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"']
82 self
._downloader
.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename
)
84 self
.run_ffmpeg_multiple_files([filename
, thumbnail_filename
], temp_filename
, options
)
86 if not self
._already
_have
_thumbnail
:
87 os
.remove(encodeFilename(thumbnail_filename
))
88 os
.remove(encodeFilename(filename
))
89 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
91 elif info
['ext'] == 'mkv':
92 old_thumbnail_filename
= thumbnail_filename
93 thumbnail_filename
= os
.path
.join(os
.path
.dirname(old_thumbnail_filename
), 'cover.jpg')
94 if os
.path
.exists(thumbnail_filename
):
95 os
.remove(encodeFilename(thumbnail_filename
))
96 os
.rename(encodeFilename(old_thumbnail_filename
), encodeFilename(thumbnail_filename
))
99 '-c', 'copy', '-map', '0',
100 '-attach', thumbnail_filename
, '-metadata:s:t', 'mimetype=image/jpeg']
102 self
._downloader
.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename
)
104 self
.run_ffmpeg_multiple_files([filename
], temp_filename
, options
)
106 if not self
._already
_have
_thumbnail
:
107 os
.remove(encodeFilename(thumbnail_filename
))
109 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename(old_thumbnail_filename
))
110 os
.remove(encodeFilename(filename
))
111 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
113 elif info
['ext'] in ['m4a', 'mp4']:
114 if not check_executable('AtomicParsley', ['-v']):
115 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
117 cmd
= [encodeFilename('AtomicParsley', True),
118 encodeFilename(filename
, True),
119 encodeArgument('--artwork'),
120 encodeFilename(thumbnail_filename
, True),
121 encodeArgument('-o'),
122 encodeFilename(temp_filename
, True)]
124 self
._downloader
.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename
)
126 if self
._downloader
.params
.get('verbose', False):
127 self
._downloader
.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd
))
129 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
130 stdout
, stderr
= p
.communicate()
132 if p
.returncode
!= 0:
133 msg
= stderr
.decode('utf-8', 'replace').strip()
134 raise EmbedThumbnailPPError(msg
)
136 if not self
._already
_have
_thumbnail
:
137 os
.remove(encodeFilename(thumbnail_filename
))
138 # for formats that don't support thumbnails (like 3gp) AtomicParsley
139 # won't create to the temporary file
140 if b
'No changes' in stdout
:
141 self
._downloader
.report_warning('The file format doesn\'t support embedding a thumbnail')
143 os
.remove(encodeFilename(filename
))
144 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
146 raise EmbedThumbnailPPError('Only mp3, mkv, m4a and mp4 are supported for thumbnail embedding for now.')