[embedthumbnail] Add support for OGGVorbis, OGGOpus and FLAC using mutagen.

This commit is contained in:
Tryamid 2020-03-09 22:49:06 +05:30
parent 8e4d3f83ce
commit 87b94f55e6

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import os
import shutil
import subprocess
from .ffmpeg import FFmpegPostProcessor
@ -41,6 +42,8 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
'Skipping embedding the thumbnail because the file is missing.')
return [], info
self._downloader.to_screen("used format" + info['ext'])
if info['ext'] == 'mp3':
options = [
'-c', 'copy', '-map', '0', '-map', '1',
@ -87,7 +90,41 @@ 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
from base64 import b64encode
# 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.
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')
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 and m4a/mp4 are supported for thumbnail embedding for now.')
raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.')
return [], info