1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
8 from .common import PostProcessor
21 class EmbedThumbnailPPError(PostProcessingError):
25 class EmbedThumbnailPP(PostProcessor):
27 filename = info['filepath']
28 temp_filename = prepend_extension(filename, 'temp')
29 temp_thumbnail = prepend_extension(filename, 'thumb')
31 if not info.get('thumbnail'):
32 raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
34 compat_urlretrieve(info['thumbnail'], temp_thumbnail)
36 if info['ext'] == 'mp3':
37 if not check_executable('ffmpeg', ['-version']):
38 raise AtomicParsleyPPError('FFmpeg was not found. Please install.')
40 cmd = ['ffmpeg', '-i', filename, '-i', temp_thumbnail, '-c', 'copy', '-map', '0', '-map', '1', '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"', temp_filename]
42 self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
44 if self._downloader.params.get('verbose', False):
45 self._downloader.to_screen('[debug] FFmpeg command line: %s' % shell_quote(cmd))
47 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48 stdout, stderr = p.communicate()
51 msg = stderr.decode('utf-8', 'replace').strip()
52 raise EmbedThumbnailPPError(msg)
54 os.remove(encodeFilename(temp_thumbnail))
55 os.remove(encodeFilename(filename))
56 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
58 elif info['ext'] == 'm4a':
59 if not check_executable('AtomicParsley', ['-v']):
60 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
62 cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
64 self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
66 if self._downloader.params.get('verbose', False):
67 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
69 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
70 stdout, stderr = p.communicate()
73 msg = stderr.decode('utf-8', 'replace').strip()
74 raise EmbedThumbnailPPError(msg)
76 os.remove(encodeFilename(temp_thumbnail))
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 are supported for thumbnail embedding for now.')