]>
Commit | Line | Data |
---|---|---|
ddbed364 | 1 | # -*- coding: utf-8 -*- |
2 | from __future__ import unicode_literals | |
3 | ||
4 | ||
5 | import os | |
6 | import subprocess | |
7 | ||
31fd9c76 | 8 | from .ffmpeg import FFmpegPostProcessor |
9 | ||
ddbed364 | 10 | from ..utils import ( |
11 | check_executable, | |
12 | encodeFilename, | |
13 | PostProcessingError, | |
14 | prepend_extension, | |
15 | shell_quote | |
16 | ) | |
17 | ||
18 | ||
19 | class EmbedThumbnailPPError(PostProcessingError): | |
20 | pass | |
21 | ||
22 | ||
31fd9c76 | 23 | class EmbedThumbnailPP(FFmpegPostProcessor): |
8e595397 YCH |
24 | def __init__(self, downloader=None, already_have_thumbnail=False): |
25 | super(EmbedThumbnailPP, self).__init__(downloader) | |
26 | self._already_have_thumbnail = already_have_thumbnail | |
27 | ||
ddbed364 | 28 | def run(self, info): |
29 | filename = info['filepath'] | |
30 | temp_filename = prepend_extension(filename, 'temp') | |
ddbed364 | 31 | |
8e595397 | 32 | if not info.get('thumbnails'): |
ddbed364 | 33 | raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.') |
34 | ||
8e595397 | 35 | thumbnail_filename = info['thumbnails'][-1]['filename'] |
ddbed364 | 36 | |
37 | if info['ext'] == 'mp3': | |
92995e62 | 38 | options = [ |
bb8ca1d1 | 39 | '-c', 'copy', '-map', '0', '-map', '1', |
31fd9c76 | 40 | '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"'] |
ddbed364 | 41 | |
42 | self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) | |
43 | ||
bb8ca1d1 | 44 | self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options) |
ddbed364 | 45 | |
8e595397 YCH |
46 | if not self._already_have_thumbnail: |
47 | os.remove(encodeFilename(thumbnail_filename)) | |
ddbed364 | 48 | os.remove(encodeFilename(filename)) |
49 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
50 | ||
51 | elif info['ext'] == 'm4a': | |
52 | if not check_executable('AtomicParsley', ['-v']): | |
53 | raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.') | |
54 | ||
8e595397 | 55 | cmd = ['AtomicParsley', filename, '--artwork', thumbnail_filename, '-o', temp_filename] |
ddbed364 | 56 | |
57 | self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename) | |
58 | ||
59 | if self._downloader.params.get('verbose', False): | |
60 | self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd)) | |
61 | ||
62 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
63 | stdout, stderr = p.communicate() | |
64 | ||
65 | if p.returncode != 0: | |
66 | msg = stderr.decode('utf-8', 'replace').strip() | |
67 | raise EmbedThumbnailPPError(msg) | |
68 | ||
8e595397 YCH |
69 | if not self._already_have_thumbnail: |
70 | os.remove(encodeFilename(thumbnail_filename)) | |
ddbed364 | 71 | # for formats that don't support thumbnails (like 3gp) AtomicParsley |
72 | # won't create to the temporary file | |
73 | if b'No changes' in stdout: | |
74 | self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail') | |
75 | else: | |
76 | os.remove(encodeFilename(filename)) | |
77 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
78 | else: | |
79 | raise EmbedThumbnailPPError('Only mp3 and m4a are supported for thumbnail embedding for now.') | |
80 | ||
81 | return [], info |