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.

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