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.

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