Merge 80a88a15ab7e3fcdc76d40aa733ab8f8114df478 into f24bc9272e9b74efc4c4af87c862f5f78921d424

This commit is contained in:
tripulse 2023-07-11 15:56:58 +03:30 committed by GitHub
commit 09291a59a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
import os
import subprocess
import imghdr
from .ffmpeg import FFmpegPostProcessor
@ -18,6 +19,8 @@ from ..utils import (
shell_quote,
)
from base64 import b64encode
class EmbedThumbnailPPError(PostProcessingError):
pass
@ -125,7 +128,35 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
else:
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
elif info['ext'] in ['opus', 'ogg', 'flac']:
try:
from mutagen.oggvorbis import OggVorbis
from mutagen.oggopus import OggOpus
from mutagen.flac import Picture, FLAC
except ImportError:
raise EmbedThumbnailPPError('mutagen was not found. Please install.')
aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](filename)
thumbfile = open(thumbnail_filename, 'rb')
covart = Picture()
covart.mime = 'image/' + imghdr.what(thumbfile)
covart.data = thumbfile.read()
covart.type = 3 # as the front cover
if info['ext'] == 'flac':
aufile.add_picture(covart)
else:
raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')
# https://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE
aufile['metadata_block_picture'] = b64encode(covart.write()).decode('ascii')
self._downloader.to_screen('[mutagen] Adding thumbnail to "%s"' % temp_filename)
aufile.save()
if not self._already_have_thumbnail:
os.remove(encodeFilename(thumbnail_filename))
else:
raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.')
return [], info