Created
May 31, 2021 12:56
-
-
Save intothevil/e78b2b65cffb2f8430348f0230a69540 to your computer and use it in GitHub Desktop.
FLACファイルに同じフォルダのカバー画像を埋め込む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import mutagen | |
from mutagen import File | |
from mutagen.flac import Picture, FLAC | |
def add_cover(filename, albumart): | |
audio = File(filename) | |
if len(audio.pictures) == 0: | |
image = Picture() | |
image.type = 3 | |
if os.path.splitext(albumart)[1] == '.jpg': | |
mime = 'image/jpeg' | |
elif os.path.splitext(albumart)[1] == '.png': | |
mime = 'image/png' | |
image.desc = 'front cover' | |
with open(albumart, 'rb') as pic: | |
image.data = pic.read() | |
audio.add_picture(image) | |
audio.save() | |
#フォルダ指定 | |
parent = input(r'親フォルダのパスは? ') | |
print(parent) | |
for (root, dirs, files) in os.walk(parent): | |
if len(dirs) > 0: | |
for dir_name in dirs: | |
pass | |
if len(files) > 0: | |
for file_name in files: | |
if file_name.endswith('.flac'): | |
audio = root + '/' + file_name | |
img = 'none' | |
if os.path.exists(root + '/' + 'cover' + '.jpg'): | |
img = root + '/' + 'cover' + '.jpg' | |
elif os.path.exists(root + '/' + 'cover' + '.png'): | |
img = root + '/' + 'cover' + '.png' | |
if img != 'none': | |
print(audio) | |
add_cover(audio,img) | |
else: | |
print(img + ':' + file_name) | |
input('enterを押すと終わる') | |
print('END') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment