]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
ddbed364 | 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, | |
2cc6d135 | 12 | encodeArgument, |
ddbed364 | 13 | encodeFilename, |
14 | PostProcessingError, | |
15 | prepend_extension, | |
bff857a8 | 16 | replace_extension, |
f5b1bca9 | 17 | shell_quote, |
18 | process_communicate_or_kill, | |
ddbed364 | 19 | ) |
20 | ||
21 | ||
22 | class EmbedThumbnailPPError(PostProcessingError): | |
23 | pass | |
24 | ||
25 | ||
31fd9c76 | 26 | class EmbedThumbnailPP(FFmpegPostProcessor): |
1b77b347 | 27 | |
8e595397 YCH |
28 | def __init__(self, downloader=None, already_have_thumbnail=False): |
29 | super(EmbedThumbnailPP, self).__init__(downloader) | |
30 | self._already_have_thumbnail = already_have_thumbnail | |
31 | ||
ddbed364 | 32 | def run(self, info): |
33 | filename = info['filepath'] | |
34 | temp_filename = prepend_extension(filename, 'temp') | |
ddbed364 | 35 | |
8e595397 | 36 | if not info.get('thumbnails'): |
1b77b347 | 37 | self.to_screen('There aren\'t any thumbnails to embed') |
b5cbe3d6 | 38 | return [], info |
ddbed364 | 39 | |
8e595397 | 40 | thumbnail_filename = info['thumbnails'][-1]['filename'] |
ddbed364 | 41 | |
c33a8639 | 42 | if not os.path.exists(encodeFilename(thumbnail_filename)): |
f446cc66 | 43 | self.report_warning('Skipping embedding the thumbnail because the file is missing.') |
c33a8639 YCH |
44 | return [], info |
45 | ||
bff857a8 S |
46 | def is_webp(path): |
47 | with open(encodeFilename(path), 'rb') as f: | |
48 | b = f.read(12) | |
49 | return b[0:4] == b'RIFF' and b[8:] == b'WEBP' | |
50 | ||
51 | # Correct extension for WebP file with wrong extension (see #25687, #25717) | |
52 | _, thumbnail_ext = os.path.splitext(thumbnail_filename) | |
53 | if thumbnail_ext: | |
54 | thumbnail_ext = thumbnail_ext[1:].lower() | |
55 | if thumbnail_ext != 'webp' and is_webp(thumbnail_filename): | |
1b77b347 | 56 | self.to_screen('Correcting extension to webp and escaping path for thumbnail "%s"' % thumbnail_filename) |
bff857a8 S |
57 | thumbnail_webp_filename = replace_extension(thumbnail_filename, 'webp') |
58 | os.rename(encodeFilename(thumbnail_filename), encodeFilename(thumbnail_webp_filename)) | |
59 | thumbnail_filename = thumbnail_webp_filename | |
60 | thumbnail_ext = 'webp' | |
61 | ||
62 | # Convert unsupported thumbnail formats to JPEG (see #25687, #25717) | |
63 | if thumbnail_ext not in ['jpg', 'png']: | |
64 | # NB: % is supposed to be escaped with %% but this does not work | |
65 | # for input files so working around with standard substitution | |
66 | escaped_thumbnail_filename = thumbnail_filename.replace('%', '#') | |
67 | os.rename(encodeFilename(thumbnail_filename), encodeFilename(escaped_thumbnail_filename)) | |
68 | escaped_thumbnail_jpg_filename = replace_extension(escaped_thumbnail_filename, 'jpg') | |
1b77b347 | 69 | self.to_screen('Converting thumbnail "%s" to JPEG' % escaped_thumbnail_filename) |
bff857a8 S |
70 | self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_jpg_filename, ['-bsf:v', 'mjpeg2jpeg']) |
71 | os.remove(encodeFilename(escaped_thumbnail_filename)) | |
72 | thumbnail_jpg_filename = replace_extension(thumbnail_filename, 'jpg') | |
73 | # Rename back to unescaped for further processing | |
74 | os.rename(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename)) | |
75 | thumbnail_filename = thumbnail_jpg_filename | |
777d5a45 | 76 | |
67002a5a | 77 | success = True |
8e2915d7 | 78 | if info['ext'] == 'mp3': |
92995e62 | 79 | options = [ |
c76eb41b | 80 | '-c', 'copy', '-map', '0:0', '-map', '1:0', '-id3v2_version', '3', |
e51f368c | 81 | '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"'] |
ddbed364 | 82 | |
1b77b347 | 83 | self.to_screen('Adding thumbnail to "%s"' % filename) |
bb8ca1d1 | 84 | self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options) |
ddbed364 | 85 | |
62566a41 | 86 | elif info['ext'] == 'mkv': |
62566a41 | 87 | options = [ |
67002a5a | 88 | '-c', 'copy', '-map', '0', '-dn', '-attach', thumbnail_filename, |
89 | '-metadata:s:t', 'mimetype=image/jpeg', '-metadata:s:t', 'filename=cover.jpg'] | |
62566a41 | 90 | |
1b77b347 | 91 | self.to_screen('Adding thumbnail to "%s"' % filename) |
62566a41 | 92 | self.run_ffmpeg_multiple_files([filename], temp_filename, options) |
93 | ||
d6aa68ce | 94 | elif info['ext'] in ['m4a', 'mp4']: |
ddbed364 | 95 | if not check_executable('AtomicParsley', ['-v']): |
96 | raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.') | |
97 | ||
2cc6d135 YCH |
98 | cmd = [encodeFilename('AtomicParsley', True), |
99 | encodeFilename(filename, True), | |
100 | encodeArgument('--artwork'), | |
101 | encodeFilename(thumbnail_filename, True), | |
102 | encodeArgument('-o'), | |
103 | encodeFilename(temp_filename, True)] | |
43820c03 | 104 | cmd += [encodeArgument(o) for o in self._configuration_args(exe='AtomicParsley')] |
ddbed364 | 105 | |
1b77b347 | 106 | self.to_screen('Adding thumbnail to "%s"' % filename) |
806b05cf | 107 | self.write_debug('AtomicParsley command line: %s' % shell_quote(cmd)) |
ddbed364 | 108 | |
109 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
f5b1bca9 | 110 | stdout, stderr = process_communicate_or_kill(p) |
ddbed364 | 111 | |
112 | if p.returncode != 0: | |
113 | msg = stderr.decode('utf-8', 'replace').strip() | |
114 | raise EmbedThumbnailPPError(msg) | |
ddbed364 | 115 | # for formats that don't support thumbnails (like 3gp) AtomicParsley |
116 | # won't create to the temporary file | |
117 | if b'No changes' in stdout: | |
f446cc66 | 118 | self.report_warning('The file format doesn\'t support embedding a thumbnail') |
67002a5a | 119 | success = False |
120 | ||
ddbed364 | 121 | else: |
958804ad | 122 | raise EmbedThumbnailPPError('Only mp3, mkv, m4a and mp4 are supported for thumbnail embedding for now.') |
ddbed364 | 123 | |
67002a5a | 124 | if success: |
125 | os.remove(encodeFilename(filename)) | |
126 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
127 | ||
128 | files_to_delete = [] if self._already_have_thumbnail else [thumbnail_filename] | |
129 | return files_to_delete, info |