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