1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
8 from .ffmpeg import FFmpegPostProcessor
10 from ..compat import (
23 class EmbedThumbnailPPError(PostProcessingError):
27 class EmbedThumbnailPP(FFmpegPostProcessor):
29 filename = info['filepath']
30 temp_filename = prepend_extension(filename, 'temp')
31 temp_thumbnail = filename + '.' + determine_ext(info['thumbnail'])
33 if not info.get('thumbnail'):
34 raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
36 compat_urlretrieve(info['thumbnail'], temp_thumbnail)
38 if info['ext'] == 'mp3':
40 '-i', temp_thumbnail, '-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(filename, temp_filename, options)
47 os.remove(encodeFilename(temp_thumbnail))
48 os.remove(encodeFilename(filename))
49 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
51 elif info['ext'] == 'm4a':
52 if not check_executable('AtomicParsley', ['-v']):
53 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
55 cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
57 self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
59 if self._downloader.params.get('verbose', False):
60 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
62 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63 stdout, stderr = p.communicate()
66 msg = stderr.decode('utf-8', 'replace').strip()
67 raise EmbedThumbnailPPError(msg)
69 os.remove(encodeFilename(temp_thumbnail))
70 # for formats that don't support thumbnails (like 3gp) AtomicParsley
71 # won't create to the temporary file
72 if b'No changes' in stdout:
73 self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
75 os.remove(encodeFilename(filename))
76 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
78 raise EmbedThumbnailPPError('Only mp3 and m4a are supported for thumbnail embedding for now.')