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.

79 lines
2.8 KiB

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