Skip to content

Instantly share code, notes, and snippets.

@intothevil
Created May 31, 2021 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intothevil/e78b2b65cffb2f8430348f0230a69540 to your computer and use it in GitHub Desktop.
Save intothevil/e78b2b65cffb2f8430348f0230a69540 to your computer and use it in GitHub Desktop.
FLACファイルに同じフォルダのカバー画像を埋め込む
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