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'] in ['m4a', 'mp4']:
81 if not check_executable('AtomicParsley', ['-v']):
82 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
84 cmd
= [encodeFilename('AtomicParsley', True),
85 encodeFilename(filename
, True),
86 encodeArgument('--artwork'),
87 encodeFilename(thumbnail_filename
, True),
89 encodeFilename(temp_filename
, True)]
91 self
._downloader
.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename
)
93 if self
._downloader
.params
.get('verbose', False):
94 self
._downloader
.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd
))
96 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
97 stdout
, stderr
= p
.communicate()
100 msg
= stderr
.decode('utf-8', 'replace').strip()
101 raise EmbedThumbnailPPError(msg
)
103 if not self
._already
_have
_thumbnail
:
104 os
.remove(encodeFilename(thumbnail_filename
))
105 # for formats that don't support thumbnails (like 3gp) AtomicParsley
106 # won't create to the temporary file
107 if b
'No changes' in stdout
:
108 self
._downloader
.report_warning('The file format doesn\'t support embedding a thumbnail')
110 os
.remove(encodeFilename(filename
))
111 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
113 raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')