Skip to content

Instantly share code, notes, and snippets.

@SapphicCode
Created August 24, 2019 16:33
Show Gist options
  • Save SapphicCode/82e12f839301fa9c33ecf32d83c7c9f2 to your computer and use it in GitHub Desktop.
Save SapphicCode/82e12f839301fa9c33ecf32d83c7c9f2 to your computer and use it in GitHub Desktop.
A simple program to walk a filesystem for cover.jpg files and attach them to .mp3 files in the same folder
#!/usr/bin/env python
import argparse
import os
from mutagen import id3
def read_file(path) -> bytes:
with open(path, 'rb') as f:
return f.read()
def run(path, dry=False):
for curpath, _, filenames in os.walk(path):
if 'cover.jpg' not in filenames:
print(f'SKIP: {curpath}')
continue # we're not in a path with a cover, immediately skip
cover_data = read_file(os.path.join(curpath, 'cover.jpg'))
for filename in filenames:
if not filename.endswith('.mp3'):
continue
fullpath = os.path.join(curpath, filename)
if not dry:
tags = id3.ID3()
tags.load(fullpath)
if not tags.getall('APIC'):
tags.add(id3.APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=cover_data))
tags.save()
print(f'OK: {fullpath}')
else:
print(f'SKIP: {fullpath}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='An album art embedder.')
parser.add_argument('-p', '--path', required=True, help='the path to work on.')
parser.add_argument('-d', '--dry', action='store_true', help='enables dry run mode.')
args = parser.parse_args()
run(args.path, dry=args.dry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment