Extract Album Covers from Audio Tracks

How did I end up in this situation?

My music library consists mostly of Bandcamp Albums and stuff that has been accumulated from CD Rips and friends over the years. Bandcamp Albums are tagged cleanly and usually include the covers for the Albums directly in the tracks.

I liked this way of handling the files so I added all cover.jpg directly into the audio files of the Albums I own, keeping the cover.jpg file around in the folders as well.

Now as it happens during late night working sessions you make mistakes. My mistake was a for file in **/Cover.jpg; do mv $file cover.jpg. The idea? Rename all Cover.jpg to cover.jpg.

After hitting enter on the command I realized that all files are being moved into the execution directory, overwriting each other. What a fail. Fortunately with ffmpeg there is a way to extract the images from the Audio files: ffmpeg -i file.mp3 -an -c:v copy file.jpg.

While I could go into every single Album directory and execute this manually that would be a major pain. So here is a script that made the restoration painless:

#! /bin/python
# Python > 3.5 required
# https://stackoverflow.com/questions/2212643/python-recursive-folder-read
import os
import glob
root_dir = "./"

# extract cover.jpg from audio tracks.
# This will only do it once per folder
# And assumes that each album is in its own folder
string = "ffmpeg -n -i '{b1}' -an -c:v copy '{b2}/cover.jpg'"
  
for filename in glob.iglob(root_dir + '**/**', recursive=True):
       dirname = os.path.dirname(filename)
       print(string.format(b1=filename,b2=dirname))
       os.system(string.format(b1=filename,b2=dirname))

Guess it was a good thing to inject the covers into the Audio files in the first place.

Lesson learned? Have clean data, have options

Back to overview