You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import subprocess
  5. from .common import PostProcessor
  6. from ..compat import (
  7. compat_urlretrieve,
  8. )
  9. from ..utils import (
  10. check_executable,
  11. encodeFilename,
  12. PostProcessingError,
  13. prepend_extension,
  14. shell_quote
  15. )
  16. class AtomicParsleyPPError(PostProcessingError):
  17. pass
  18. class AtomicParsleyPP(PostProcessor):
  19. def run(self, info):
  20. if not check_executable('AtomicParsley', ['-v']):
  21. raise AtomicParsleyPPError('AtomicParsley was not found. Please install.')
  22. filename = info['filepath']
  23. temp_filename = prepend_extension(filename, 'temp')
  24. temp_thumbnail = prepend_extension(filename, 'thumb')
  25. if not info.get('thumbnail'):
  26. raise AtomicParsleyPPError('Thumbnail was not found. Nothing to do.')
  27. compat_urlretrieve(info['thumbnail'], temp_thumbnail)
  28. cmd = ['AtomicParsley', filename, '--artwork', temp_thumbnail, '-o', temp_filename]
  29. self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
  30. if self._downloader.params.get('verbose', False):
  31. self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
  32. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  33. stdout, stderr = p.communicate()
  34. if p.returncode != 0:
  35. msg = stderr.decode('utf-8', 'replace').strip()
  36. raise AtomicParsleyPPError(msg)
  37. os.remove(encodeFilename(temp_thumbnail))
  38. # for formats that don't support thumbnails (like 3gp) AtomicParsley
  39. # won't create to the temporary file
  40. if b'No changes' in stdout:
  41. self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
  42. else:
  43. os.remove(encodeFilename(filename))
  44. os.rename(encodeFilename(temp_filename), encodeFilename(filename))
  45. return True, info