1 # -*- coding: utf-8 -*-
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 raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
36 thumbnail_filename = info['thumbnails'][-1]['filename']
38 if info['ext'] == 'mp3':
40 '-c', 'copy', '-map', '0', '-map', '1',
41 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
43 self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
45 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
47 if not self._already_have_thumbnail:
48 os.remove(encodeFilename(thumbnail_filename))
49 os.remove(encodeFilename(filename))
50 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
52 elif info['ext'] in ['m4a', 'mp4']:
53 if not check_executable('AtomicParsley', ['-v']):
54 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
56 cmd = [encodeFilename('AtomicParsley', True),
57 encodeFilename(filename, True),
58 encodeArgument('--artwork'),
59 encodeFilename(thumbnail_filename, True),
61 encodeFilename(temp_filename, True)]
63 self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
65 if self._downloader.params.get('verbose', False):
66 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
68 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69 stdout, stderr = p.communicate()
72 msg = stderr.decode('utf-8', 'replace').strip()
73 raise EmbedThumbnailPPError(msg)
75 if not self._already_have_thumbnail:
76 os.remove(encodeFilename(thumbnail_filename))
77 # for formats that don't support thumbnails (like 3gp) AtomicParsley
78 # won't create to the temporary file
79 if b'No changes' in stdout:
80 self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
82 os.remove(encodeFilename(filename))
83 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
85 raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')