[postprocessor/embedthumbnail] Fix mp3 embedding with avconv (fixes #5526)
[youtube-dl] / 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 ..compat import (
11     compat_urlretrieve,
12 )
13 from ..utils import (
14     determine_ext,
15     check_executable,
16     encodeFilename,
17     PostProcessingError,
18     prepend_extension,
19     shell_quote
20 )
21
22
23 class EmbedThumbnailPPError(PostProcessingError):
24     pass
25
26
27 class EmbedThumbnailPP(FFmpegPostProcessor):
28     def run(self, info):
29         filename = info['filepath']
30         temp_filename = prepend_extension(filename, 'temp')
31         temp_thumbnail = filename + '.' + determine_ext(info['thumbnail'])
32
33         if not info.get('thumbnail'):
34             raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
35
36         compat_urlretrieve(info['thumbnail'], temp_thumbnail)
37
38         if info['ext'] == 'mp3':
39             options = [
40                 '-i', temp_thumbnail, '-c', 'copy', '-map', '0', '-map', '1',
41                 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
42
43             self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
44
45             self.run_ffmpeg(filename, temp_filename, options)
46
47             os.remove(encodeFilename(temp_thumbnail))
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
55             cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
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
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')
74             else:
75                 os.remove(encodeFilename(filename))
76                 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
77         else:
78             raise EmbedThumbnailPPError('Only mp3 and m4a are supported for thumbnail embedding for now.')
79
80         return [], info