本文整理汇总了Python中mutagen.File.add_picture方法的典型用法代码示例。如果您正苦于以下问题:Python File.add_picture方法的具体用法?Python File.add_picture怎么用?Python File.add_picture使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mutagen.File
的用法示例。
在下文中一共展示了File.add_picture方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: split_item
# 需要导入模块: from mutagen import File [as 别名]
# 或者: from mutagen.File import add_picture [as 别名]
def split_item(item, DIGITAL_FOLDER=mp3_folder, DROPBOX_COPY=False):
logging.info('BLOOOAAARRRRGHHGGGGHHHHH!!! (Please hold...)')
audio = wave.open(item.path)
audio.params = audio.getparams()
# Load AudioSegment for encoded segments
if item.digital_ext is not 'wav':
audio_segment = AudioSegment.from_wav(item.path)
# Loop through cues and write regions (assumes start and end markers)
for i, track in enumerate(item.tracks):
# Build track filename
digital_track_name = '{0} - {1} - {2}'.format(item.digital_file_name, str(i + 1), track.title)
digital_track_name = ''.join([c for c in digital_track_name if c.isalpha() or c.isdigit() or c in "'- ._()[email protected]#$%^&*"]).rstrip('. ')
digital_track_path = os.path.join(DIGITAL_FOLDER, digital_track_name) + '.' + item.digital_ext
logging.info('Region {0} | {1} -> {2}'.format(i + 1, track.duration, digital_track_path))
# Split, export track
if 'wav' not in item.digital_ext:
digital_track = audio_segment[(item.cues[i] / 44.1):(item.cues[i + 1] / 44.1)]
tags = {'title':track.title, 'artist':item.artist, 'albumartist':item.artist, 'album':(item.album or item.artist), 'track':(i + 1)}
digital_track.export(out_f=digital_track_path, format=item.digital_ext, bitrate='192k', tags=tags)
# Add cover art
if item.thumb and (item.digital_ext == 'mp3'):
mutagen_audio = MP3(digital_track_path, ID3=ID3)
try:
# Add ID3 tag if it doesn't exist
mutagen_audio.add_tags()
except error:
pass
mutagen_audio.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime='image/jpeg', # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u'Cover',
data=open(item.thumb_path, 'rb').read()
)
)
mutagen_audio.save()
elif item.thumb and (item.digital_ext == 'flac'):
mutagen_audio = File(digital_track_path)
flac_image = Picture()
flac_image.type = 3
mime = 'image/jpeg'
flac_image.desc = 'Cover'
with open(item.thumb_path, 'rb') as f:
flac_image.data = f.read()
mutagen_audio.add_picture(flac_image)
mutagen_audio.save()
else:
logging.warning('No cover found for item {0}'.format(item.name))
else:
digital_track = wave.open(digital_track_path, 'w')
digital_track.setparams(audio.params)
region_length = item.cues[i + 1] - item.cues[i]
digital_track.writeframes(audio.readframes(region_length))
digital_track.close()
if DROPBOX_COPY:
# Copy finished digital file to Dropbox order folder
shutil.copy2(digital_track_path, item.dropbox_order_folder)
audio.close()
示例2: split_item
# 需要导入模块: from mutagen import File [as 别名]
# 或者: from mutagen.File import add_picture [as 别名]
def split_item(item, digital_folder=digital_folder, dropbox_move=True):
logging.info("BLOOOAAARRRRGHHGGGGHHHHH!!! (Please hold...)")
audio = wave.open(item.path)
audio.params = audio.getparams()
# Load AudioSegment for encoded segments
if item.digital_ext is not "wav":
audio_segment = AudioSegment.from_wav(item.path)
# Loop through cues and write regions (assumes start and end markers)
for i, track in enumerate(item.tracks):
# Build track filename
digital_track_name = "{0} - {1} - {2}".format(item.digital_file_name, str(i + 1), track.title)
digital_track_name = "".join(
[c for c in digital_track_name if c.isalpha() or c.isdigit() or c in "'- ._()[email protected]#$%^&*"]
).rstrip(". ")
if dropbox_move:
digital_track_path = os.path.join(item.dropbox_order_folder, digital_track_name) + "." + item.digital_ext
else:
digital_track_path = os.path.join(digital_folder, digital_track_name) + "." + item.digital_ext
logging.info("Region {0} | {1} -> {2}".format(i + 1, track.duration, digital_track_path))
# Split, export track
if "wav" not in item.digital_ext:
digital_track = audio_segment[(item.cues[i] / 44.1) : (item.cues[i + 1] / 44.1)]
tags = {
"title": track.title or "Track {0}".format(i + 1),
"artist": item.artist,
"albumartist": item.artist,
"album": (item.album or item.artist),
"track": (i + 1),
}
digital_track.export(
out_f=digital_track_path, format=item.digital_ext, bitrate=item.bitrate, tags=tags, id3v2_version="3"
)
# Add cover art
if item.thumb and (item.digital_ext == "mp3"):
mutagen_audio = MP3(digital_track_path, ID3=ID3)
try:
# Add ID3 tag if it doesn't exist
mutagen_audio.add_tags()
except error:
pass
mutagen_audio.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime="image/jpeg", # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u"Cover",
data=open(item.thumb_path, "rb").read(),
)
)
mutagen_audio.save(v2_version=3)
elif item.thumb and (item.digital_ext == "flac"):
mutagen_audio = File(digital_track_path)
flac_image = Picture()
flac_image.type = 3
mime = "image/jpeg"
flac_image.desc = "Cover"
with open(item.thumb_path, "rb") as f:
flac_image.data = f.read()
mutagen_audio.add_picture(flac_image)
mutagen_audio.save()
else:
logging.warning("No cover found for item {0}".format(item.name))
else:
digital_track = wave.open(digital_track_path, "w")
digital_track.setparams(audio.params)
region_length = item.cues[i + 1] - item.cues[i]
digital_track.writeframes(audio.readframes(region_length))
digital_track.close()
audio.close()