[embedthumbnail] Only wrapped import statements with try..except.

Some comments are added previous ones are improvised.
This commit is contained in:
Tryamid 2020-03-10 07:00:35 +05:30
parent 05899a47d4
commit 01d84b7db5

View File

@ -97,33 +97,33 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
from mutagen.oggopus import OggOpus
from mutagen.flac import Picture, FLAC
from base64 import b64encode
except ImportError:
raise EmbedThumbnailPPError('mutagen was not found. Please install.')
# to prevent the behaviour of in-place modification of Mutagen
shutil.copyfile(filename, temp_filename)
aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis} \
[info['ext']](temp_filename)
covart = Picture()
covart.data = open(thumbnail_filename, 'rb').read()
covart.type = 3 #< use as front cover.
covart.type = 3 # Cover (front)
# Since, OGGOpus and OGGVorbis doesn't natively support
# (coverart / thumbnail)s, it's wrapped in if..else
if info['ext'] == 'flac':
aufile.add_picture(covart)
else:
# VorbisComments don't allow raw-bytes so it's encoded
# in base64 and converted to a string.
aufile['metadata_block_picture'] = \
b64encode(covart.write()).decode('ascii')
# Save changes to temporary file, it'd be overlapped as the
# original one.
aufile.save()
if not self._already_have_thumbnail:
os.remove(encodeFilename(thumbnail_filename))
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
except ImportError:
raise EmbedThumbnailPPError('mutagen was not found. Please install.')
else:
raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.')