1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
8 from .ffmpeg import FFmpegPostProcessor
10 from ..compat import (
22 class EmbedThumbnailPPError(PostProcessingError):
26 class EmbedThumbnailPP(FFmpegPostProcessor):
28 filename = info['filepath']
29 temp_filename = prepend_extension(filename, 'temp')
30 temp_thumbnail = prepend_extension(filename, 'thumb')
32 if not info.get('thumbnail'):
33 raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
35 compat_urlretrieve(info['thumbnail'], temp_thumbnail)
37 if info['ext'] == 'mp3':
38 options = ['-i', temp_thumbnail, '-c', 'copy', '-map', '0', '-map', '1',
39 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
41 self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
43 self.run_ffmpeg(filename, temp_filename, options)
45 os.remove(encodeFilename(temp_thumbnail))
46 os.remove(encodeFilename(filename))
47 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
49 elif info['ext'] == 'm4a':
50 if not check_executable('AtomicParsley', ['-v']):
51 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
53 cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
55 self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
57 if self._downloader.params.get('verbose', False):
58 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
60 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
61 stdout, stderr = p.communicate()
64 msg = stderr.decode('utf-8', 'replace').strip()
65 raise EmbedThumbnailPPError(msg)
67 os.remove(encodeFilename(temp_thumbnail))
68 # for formats that don't support thumbnails (like 3gp) AtomicParsley
69 # won't create to the temporary file
70 if b'No changes' in stdout:
71 self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
73 os.remove(encodeFilename(filename))
74 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
76 raise EmbedThumbnailPPError('Only mp3 and m4a are supported for thumbnail embedding for now.')