• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

How I use Groovy to analyze album art in my music directory

How I use Groovy to analyze album art in my music directory: How I use Groovy to analyze album art in my music directory Chris Hermansen Sun, 08/28/2022 – 03:00

Register or Login to like
Register or Login to like

In this series, I’m developing several scripts to help in cleaning up my music collection. In the last article, I used the framework I created for analyzing the directory and sub-directories of music files and carried out the analysis with the fine open source JAudiotagger library to analyze the tags of the music files in the music directory and subdirectories. In this article, I will do a simpler job:

  1. Use the framework we created in Part 1
  2. Make sure each album directory has a cover.jpg class
  3. Make a note of any other files in the album directory that aren’t FLAC, MP3 or OGG.

Music and metadata

If you haven’t read part 1 and part 2 of this series, do that now so you understand the intended structure of my music directory, the framework created in that article, and how to pick up FLAC, MP3, and OGG files.

One more thing. Most audio ripping applications and many downloads:

  • Don’t come with a useful cover.jpg file
  • Even if they do come with a useful cover.jpg file, they don’t link the media files to it
  • Carry in all sorts of other files of dubious utility (for example, playlist.m3u, which gets created by a tagging utility I’ve used in the past)

As I mentioned in my last article, the ultimate goal of this series is to create a few useful scripts to help identify missing or unusual tags and facilitate the creation of a work plan to fix tagging problems. This particular script looks for missing cover.jpg files and unwanted non-media files, and creates a CSV file that you can load into LibreOffice or OnlyOffice to look for problems. It won’t look at the media files themselves, nor does it look for extraneous files left in the artist subdirectories (those are exercises left for the reader).

More on Java
What is enterprise Java programming?
Red Hat build of OpenJDK
Java cheat sheet
Free online course: Developing cloud-native applications with microservices
Fresh Java articles

The framework and album files analysis

Start with the code. As before, I’ve incorporated comments in the script that reflect the (relatively abbreviated) “comment notes” that I typically leave for myself:

 
     1  // Define the music library directory
       
     2  def musicLibraryDirName = '/var/lib/mpd/music'
       
     3  // Print the CSV file header
       
     4  println "artist|album|cover|unwanted files"
       
     5  // Iterate over each directory in the music libary directory
     6  // These are assumed to be artist directories

     7  new File(musicLibraryDirName).eachDir { artistDir ->
       
     8      // Iterate over each directory in the artist directory
     9      // These are assumed to be album directories
       
    10      artistDir.eachDir { albumDir ->
       
    11          // Iterate over each file in the album directory
    12          // These are assumed to be content or related
    13          // (cover.jpg, PDFs with liner notes etc)
       
    14          // Initialize the counter for cover.jpg
    15          // and the list for unwanted file names
       
    16          def coverCounter = 0
    17          def unwantedFileNames = []
       
    18          albumDir.eachFile { contentFile ->
       
    19              // Analyze the file
       
    20              if (contentFile.name ==~ /.*\.(flac|mp3|ogg)/) {
    21                  // nothing to do here
    22              } else if (contentFile.name == 'cover.jpg') {
    23                  coverCounter++
    24              } else {
    25                  unwantedFileNames << contentFile.name
    26              }
       
    27          }
    28          println "$ {artistDir.name}|$ {albumDir.name}|$ coverCounter|$ {unwantedFileNames.join(',')}"
    29      }
    30  }

Lines 1-2 define the name of the music file directory.

Line 3-4 print the CSV file header.

Lines 5-13 come from the framework created in Part 1 of this article and get down to the album sub-subdirectories.

Lines 14-17 set up the cover.jpg counter (should only ever be zero or one) and the empty list in which we will accumulate unwanted file names.

Lines 18-27 analyze any files found in the album directories:

Lines 20-21 uses the Groovy match operator ==~ and a “slashy” regular expression to check file name patterns. Nothing is done with these files (see Part 2 for that information).

Lines 22-23 count the instances of cover.jpg (it should only ever be zero or one).

Lines 24-26 record the names of any non-media, non-cover.jpg files to show potential cruft or who-knows-what in the album directories.

Line 28 prints out the artist name, album name, cover.jpg count and list of unwanted file names.

That’s it!

Running the code

Typically, I run this as follows:

 $   groovy TagAnalyzer3.groovy > tagAnalysis3.csv

Then I load the resulting CSV into a spreadsheet. For example, with LibreOffice Calc , go to the Sheet menu and select Insert sheet from file. When prompted, set the delimiter character to |. In my case, the results look like this:

Image of a screenshot of LibreOffice showing tagAnalysis3
Image by:

(Chris Hermansen, CC BY-SA 4.0)

I’ve sorted this in increasing order of the column “cover” to show album sub-subsubdirectories that don’t have cover.jpg files. Note that some have cover.png instead. My experience with music players is that at least some don’t play well with PNG format cover images.

Also, note that some of these have PDF liner notes, extra image files, M3U playlists, and so on. In my next article, I’ll show you how to manage some of the cruft.

Here’s how I use open source tools to analyze my music directory including album cover files.

Record player with an LP spinning
Image by:

Opensource.com

Java
Audio and music

What to read next
How I analyze my music directory with Groovy
My favorite open source library for analyzing music files

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Chris Hermansen

Supporting Open Source.

Have you tried: Sailing in Africa ?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 28 August 2022 by Alan Category: SoftwareTag: Album, analyze, Directory, Groovy, Music

Women Talking – a radical film that reimagines how cinema can be made

Women Talking – a radical film that reimagines how cinema can be made: Words matter. They give …

Read moreWomen Talking – a radical film that reimagines how cinema can be made

Ekuseni Ngo 4 O Clock | Video

A Lalela Cape Town Choir video selected by www.Free-Mail.co.za: Provided to YouTube by The Orchard …

Read moreEkuseni Ngo 4 O Clock | Video

Enjoy a Different View at the Upside Down House

There is so much to do on the KZN South Coast this summer, from its many Blue Flag beaches and …

Read moreEnjoy a Different View at the Upside Down House

About Alan

Previous Post:The Palace of the Lost City, Sun City Resort, South Africa
Next Post:Trending Now: Zimbabwe vs Australia

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top