]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/embedthumbnail.py
Update upstream source from tag 'upstream/2019.09.28'
[youtubedl] / youtube_dl / postprocessor / embedthumbnail.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 import os
6 import subprocess
7
8 from .ffmpeg import FFmpegPostProcessor
9
10 from ..utils import (
11 check_executable,
12 encodeArgument,
13 encodeFilename,
14 PostProcessingError,
15 prepend_extension,
16 shell_quote
17 )
18
19
20 class EmbedThumbnailPPError(PostProcessingError):
21 pass
22
23
24 class EmbedThumbnailPP(FFmpegPostProcessor):
25 def __init__(self, downloader=None, already_have_thumbnail=False):
26 super(EmbedThumbnailPP, self).__init__(downloader)
27 self._already_have_thumbnail = already_have_thumbnail
28
29 def run(self, info):
30 filename = info['filepath']
31 temp_filename = prepend_extension(filename, 'temp')
32
33 if not info.get('thumbnails'):
34 self._downloader.to_screen('[embedthumbnail] There aren\'t any thumbnails to embed')
35 return [], info
36
37 thumbnail_filename = info['thumbnails'][-1]['filename']
38
39 if not os.path.exists(encodeFilename(thumbnail_filename)):
40 self._downloader.report_warning(
41 'Skipping embedding the thumbnail because the file is missing.')
42 return [], info
43
44 if info['ext'] == 'mp3':
45 options = [
46 '-c', 'copy', '-map', '0', '-map', '1',
47 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
48
49 self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
50
51 self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
52
53 if not self._already_have_thumbnail:
54 os.remove(encodeFilename(thumbnail_filename))
55 os.remove(encodeFilename(filename))
56 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
57
58 elif info['ext'] in ['m4a', 'mp4']:
59 if not check_executable('AtomicParsley', ['-v']):
60 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
61
62 cmd = [encodeFilename('AtomicParsley', True),
63 encodeFilename(filename, True),
64 encodeArgument('--artwork'),
65 encodeFilename(thumbnail_filename, True),
66 encodeArgument('-o'),
67 encodeFilename(temp_filename, True)]
68
69 self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
70
71 if self._downloader.params.get('verbose', False):
72 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
73
74 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75 stdout, stderr = p.communicate()
76
77 if p.returncode != 0:
78 msg = stderr.decode('utf-8', 'replace').strip()
79 raise EmbedThumbnailPPError(msg)
80
81 if not self._already_have_thumbnail:
82 os.remove(encodeFilename(thumbnail_filename))
83 # for formats that don't support thumbnails (like 3gp) AtomicParsley
84 # won't create to the temporary file
85 if b'No changes' in stdout:
86 self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
87 else:
88 os.remove(encodeFilename(filename))
89 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
90 else:
91 raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')
92
93 return [], info