2 from __future__
import unicode_literals
8 from .ffmpeg
import FFmpegPostProcessor
20 class EmbedThumbnailPPError(PostProcessingError
):
24 class EmbedThumbnailPP(FFmpegPostProcessor
):
25 def __init__(self
, downloader
=None, already_have_thumbnail
=False):
26 super(EmbedThumbnailPP
, self
).__init
__(downloader
)
27 self
._already
_have
_thumbnail
= already_have_thumbnail
30 filename
= info
['filepath']
31 temp_filename
= prepend_extension(filename
, 'temp')
33 if not info
.get('thumbnails'):
34 self
._downloader
.to_screen('[embedthumbnail] There aren\'t any thumbnails to embed')
37 thumbnail_filename
= info
['thumbnails'][-1]['filename']
39 if not os
.path
.exists(encodeFilename(thumbnail_filename
)):
40 self
._downloader
.report_warning(
41 'Skipping embedding the thumbnail because the file is missing.')
44 # Check for mislabeled webp file
45 with open(encodeFilename(thumbnail_filename
), "rb") as f
:
47 if b
'\x57\x45\x42\x50' in b
: # Binary for WEBP
48 [thumbnail_filename_path
, thumbnail_filename_extension
] = os
.path
.splitext(thumbnail_filename
)
49 if not thumbnail_filename_extension
== ".webp":
50 webp_thumbnail_filename
= thumbnail_filename_path
+ ".webp"
51 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename(webp_thumbnail_filename
))
52 thumbnail_filename
= webp_thumbnail_filename
54 # If not a jpg or png thumbnail, convert it to jpg using ffmpeg
55 if not os
.path
.splitext(thumbnail_filename
)[1].lower() in ['.jpg', '.png']:
56 jpg_thumbnail_filename
= os
.path
.splitext(thumbnail_filename
)[0] + ".jpg"
57 jpg_thumbnail_filename
= os
.path
.join(os
.path
.dirname(jpg_thumbnail_filename
), os
.path
.basename(jpg_thumbnail_filename
).replace('%', '_')) # ffmpeg interprets % as image sequence
59 self
._downloader
.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % thumbnail_filename
)
61 self
.run_ffmpeg(thumbnail_filename
, jpg_thumbnail_filename
, ['-bsf:v', 'mjpeg2jpeg'])
63 os
.remove(encodeFilename(thumbnail_filename
))
64 thumbnail_filename
= jpg_thumbnail_filename
66 if info
['ext'] == 'mp3':
68 '-c', 'copy', '-map', '0', '-map', '1',
69 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
71 self
._downloader
.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename
)
73 self
.run_ffmpeg_multiple_files([filename
, thumbnail_filename
], temp_filename
, options
)
75 if not self
._already
_have
_thumbnail
:
76 os
.remove(encodeFilename(thumbnail_filename
))
77 os
.remove(encodeFilename(filename
))
78 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
80 elif info
['ext'] == 'mkv':
81 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename('cover.jpg'))
82 old_thumbnail_filename
= thumbnail_filename
83 thumbnail_filename
= 'cover.jpg'
86 '-c', 'copy', '-attach', thumbnail_filename
, '-metadata:s:t', 'mimetype=image/jpeg']
88 self
._downloader
.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename
)
90 self
.run_ffmpeg_multiple_files([filename
], temp_filename
, options
)
92 if not self
._already
_have
_thumbnail
:
93 os
.remove(encodeFilename(thumbnail_filename
))
95 os
.rename(encodeFilename(thumbnail_filename
), encodeFilename(old_thumbnail_filename
))
96 os
.remove(encodeFilename(filename
))
97 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
99 elif info
['ext'] in ['m4a', 'mp4']:
100 if not check_executable('AtomicParsley', ['-v']):
101 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
103 cmd
= [encodeFilename('AtomicParsley', True),
104 encodeFilename(filename
, True),
105 encodeArgument('--artwork'),
106 encodeFilename(thumbnail_filename
, True),
107 encodeArgument('-o'),
108 encodeFilename(temp_filename
, True)]
110 self
._downloader
.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename
)
112 if self
._downloader
.params
.get('verbose', False):
113 self
._downloader
.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd
))
115 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
116 stdout
, stderr
= p
.communicate()
118 if p
.returncode
!= 0:
119 msg
= stderr
.decode('utf-8', 'replace').strip()
120 raise EmbedThumbnailPPError(msg
)
122 if not self
._already
_have
_thumbnail
:
123 os
.remove(encodeFilename(thumbnail_filename
))
124 # for formats that don't support thumbnails (like 3gp) AtomicParsley
125 # won't create to the temporary file
126 if b
'No changes' in stdout
:
127 self
._downloader
.report_warning('The file format doesn\'t support embedding a thumbnail')
129 os
.remove(encodeFilename(filename
))
130 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
132 raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')