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.

93 lines
3.5 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. encodeArgument,
  9. encodeFilename,
  10. PostProcessingError,
  11. prepend_extension,
  12. shell_quote
  13. )
  14. class EmbedThumbnailPPError(PostProcessingError):
  15. pass
  16. class EmbedThumbnailPP(FFmpegPostProcessor):
  17. def __init__(self, downloader=None, already_have_thumbnail=False):
  18. super(EmbedThumbnailPP, self).__init__(downloader)
  19. self._already_have_thumbnail = already_have_thumbnail
  20. def run(self, info):
  21. filename = info['filepath']
  22. temp_filename = prepend_extension(filename, 'temp')
  23. if not info.get('thumbnails'):
  24. self._downloader.to_screen('[embedthumbnail] There aren\'t any thumbnails to embed')
  25. return [], info
  26. thumbnail_filename = info['thumbnails'][-1]['filename']
  27. if not os.path.exists(encodeFilename(thumbnail_filename)):
  28. self._downloader.report_warning(
  29. 'Skipping embedding the thumbnail because the file is missing.')
  30. return [], info
  31. if info['ext'] == 'mp3':
  32. options = [
  33. '-c', 'copy', '-map', '0', '-map', '1',
  34. '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']
  35. self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename)
  36. self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
  37. if not self._already_have_thumbnail:
  38. os.remove(encodeFilename(thumbnail_filename))
  39. os.remove(encodeFilename(filename))
  40. os.rename(encodeFilename(temp_filename), encodeFilename(filename))
  41. elif info['ext'] in ['m4a', 'mp4']:
  42. if not check_executable('AtomicParsley', ['-v']):
  43. raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.')
  44. cmd = [encodeFilename('AtomicParsley', True),
  45. encodeFilename(filename, True),
  46. encodeArgument('--artwork'),
  47. encodeFilename(thumbnail_filename, True),
  48. encodeArgument('-o'),
  49. encodeFilename(temp_filename, True)]
  50. self._downloader.to_screen('[atomicparsley] Adding thumbnail to "%s"' % filename)
  51. if self._downloader.params.get('verbose', False):
  52. self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
  53. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  54. stdout, stderr = p.communicate()
  55. if p.returncode != 0:
  56. msg = stderr.decode('utf-8', 'replace').strip()
  57. raise EmbedThumbnailPPError(msg)
  58. if not self._already_have_thumbnail:
  59. os.remove(encodeFilename(thumbnail_filename))
  60. # for formats that don't support thumbnails (like 3gp) AtomicParsley
  61. # won't create to the temporary file
  62. if b'No changes' in stdout:
  63. self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
  64. else:
  65. os.remove(encodeFilename(filename))
  66. os.rename(encodeFilename(temp_filename), encodeFilename(filename))
  67. else:
  68. raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')
  69. return [], info