If there is no ID3 tag in mp3 files, trying to add one.

This commit is contained in:
codezjx 2017-07-07 00:59:47 +08:00
parent b34b7d5df4
commit 15f12dc9f2
2 changed files with 14 additions and 29 deletions

View File

@ -1,9 +1,22 @@
# -*- coding: utf-8 -*-
from mutagen.id3 import ID3, APIC, TPE1, TIT2, TALB
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TPE1, TIT2, TALB, error
def add_metadata_to_song(file_path, cover_path, song):
# If no ID3 tags in mp3 file
audio = MP3(file_path, ID3=ID3)
if audio.tags is None:
print('No ID3 tag, trying to add one!')
try:
audio.add_tags()
audio.save()
except error as e:
print('Error occur when add tags:', str(e))
return
# Modify ID3 tags
id3 = ID3(file_path)
# add album cover
id3.add(

View File

@ -67,31 +67,3 @@ def main():
if __name__ == '__main__':
main()
# song = api.get_song('464035731')
# print('song id:{}, song name:{}, album:{}'.format(song['id'], song['name'], song['album']['name']))
# from mutagen.mp3 import MP3
# from mutagen.id3 import ID3, APIC, error
#
#
# file_path = '/Users/codezjx/Downloads/test.mp3'
# cover_path = '/Users/codezjx/Downloads/test.jpg'
#
# audio = MP3(file_path, ID3=ID3)
# if audio.tags is None:
# print('No ID3 tag, try to add one!')
# try:
# audio.add_tags()
# except error:
# pass
# audio.tags.add(
# APIC(
# encoding=3, # 3 is for utf-8
# mime='image/jpg', # image/jpeg or image/png
# type=3, # 3 is for the cover(front) image
# data=open(cover_path, 'rb').read()
# )
# )
# audio.save()