]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/postprocessor/atomicparsley.py
[srf] Extract subtitles
[youtube-dl.git] / youtube_dl / postprocessor / atomicparsley.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 AtomicParsleyPPError(PostProcessingError):
22     pass
23
24
25 class AtomicParsleyPP(PostProcessor):
26     def run(self, info):
27         if not check_executable('AtomicParsley', ['-v']):
28             raise AtomicParsleyPPError('AtomicParsley was not found. Please install.')
29
30         filename = info['filepath']
31         temp_filename = prepend_extension(filename, 'temp')
32         temp_thumbnail = prepend_extension(filename, 'thumb')
33
34         if not info.get('thumbnail'):
35             raise AtomicParsleyPPError('Thumbnail was not found. Nothing to do.')
36
37         compat_urlretrieve(info['thumbnail'], temp_thumbnail)
38
39         cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
40
41         self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
42
43         if self._downloader.params.get('verbose', False):
44             self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
45
46         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
47         stdout, stderr = p.communicate()
48
49         if p.returncode != 0:
50             msg = stderr.decode('utf-8', 'replace').strip()
51             raise AtomicParsleyPPError(msg)
52
53         os.remove(encodeFilename(temp_thumbnail))
54         # for formats that don't support thumbnails (like 3gp) AtomicParsley
55         # won't create to the temporary file
56         if b'No changes' in stdout:
57             self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
58         else:
59             os.remove(encodeFilename(filename))
60             os.rename(encodeFilename(temp_filename), encodeFilename(filename))
61
62         return True, info