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