Merge pull request #5521 from mrkrossxdx/mpv
[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 = [
39                 '-i', temp_thumbnail, '-c', 'copy', '-map', '0', '-map', '1',
40                 '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
41
42             self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
43
44             self.run_ffmpeg(filename, temp_filename, options)
45
46             os.remove(encodeFilename(temp_thumbnail))
47             os.remove(encodeFilename(filename))
48             os.rename(encodeFilename(temp_filename), encodeFilename(filename))
49
50         elif info['ext'] == 'm4a':
51             if not check_executable('AtomicParsley', ['-v']):
52                 raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
53
54             cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
55
56             self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
57
58             if self._downloader.params.get('verbose', False):
59                 self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
60
61             p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
62             stdout, stderr = p.communicate()
63
64             if p.returncode != 0:
65                 msg = stderr.decode('utf-8', 'replace').strip()
66                 raise EmbedThumbnailPPError(msg)
67
68             os.remove(encodeFilename(temp_thumbnail))
69             # for formats that don't support thumbnails (like 3gp) AtomicParsley
70             # won't create to the temporary file
71             if b'No changes' in stdout:
72                 self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
73             else:
74                 os.remove(encodeFilename(filename))
75                 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
76         else:
77             raise EmbedThumbnailPPError('Only mp3 and m4a are supported for thumbnail embedding for now.')
78
79         return [], info