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.

81 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import subprocess
  5. from .ffmpeg import FFmpegPostProcessor
  6. from ..utils import (
  7. check_executable,
  8. encodeFilename,
  9. PostProcessingError,
  10. prepend_extension,
  11. shell_quote
  12. )
  13. class EmbedThumbnailPPError(PostProcessingError):
  14. pass
  15. class EmbedThumbnailPP(FFmpegPostProcessor):
  16. def __init__(self, downloader=None, already_have_thumbnail=False):
  17. super(EmbedThumbnailPP, self).__init__(downloader)
  18. self._already_have_thumbnail = already_have_thumbnail
  19. def run(self, info):
  20. filename = info['filepath']
  21. temp_filename = prepend_extension(filename, 'temp')
  22. if not info.get('thumbnails'):
  23. raise EmbedThumbnailPPError('Thumbnail was not found. Nothing to do.')
  24. thumbnail_filename = info['thumbnails'][-1]['filename']
  25. if info['ext'] == 'mp3':
  26. options = [
  27. '-c', 'copy', '-map', '0', '-map', '1',
  28. '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
  29. self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
  30. self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
  31. if not self._already_have_thumbnail:
  32. os.remove(encodeFilename(thumbnail_filename))
  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', thumbnail_filename, '-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. if not self._already_have_thumbnail:
  48. os.remove(encodeFilename(thumbnail_filename))
  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