Science and technology

How I exploit Groovy to research album artwork in my music listing

In this collection, I’m growing a number of scripts to assist in cleansing up my music assortment. In the final article, I used the framework I created for analyzing the listing and sub-directories of music recordsdata and carried out the evaluation with the effective open supply JAudiotagger library to research the tags of the music recordsdata within the music listing and subdirectories. In this text, I’ll do an easier job:

  1. Use the framework we created in Part 1
  2. Make certain every album listing has a cowl.jpg class
  3. Make a observe of every other recordsdata within the album listing that are not FLAC, MP3 or OGG.

Music and metadata

If you have not learn part 1 and part 2 of this collection, try this now so that you perceive the meant construction of my music listing, the framework created in that article, and the way to decide up FLAC, MP3, and OGG recordsdata.

One other thing. Most audio ripping functions and plenty of downloads:

  • Don’t include a helpful cowl.jpg file
  • Even in the event that they do include a helpful cowl.jpg file, they do not hyperlink the media recordsdata to it
  • Carry in all types of different recordsdata of doubtful utility (for instance, playlist.m3u, which will get created by a tagging utility I’ve used prior to now)

As I discussed in my final article, the last word objective of this collection is to create a couple of helpful scripts to assist establish lacking or uncommon tags and facilitate the creation of a piece plan to repair tagging issues. This explicit script appears to be like for lacking cowl.jpg recordsdata and undesirable non-media recordsdata, and creates a CSV file that you may load into LibreOffice or OnlyOffice to search for issues. It will not have a look at the media recordsdata themselves, nor does it search for extraneous recordsdata left within the artist subdirectories (these are workouts left for the reader).

The framework and album recordsdata evaluation

Start with the code. As earlier than, I’ve integrated feedback within the script that replicate the (comparatively abbreviated) “comment notes” that I usually go away for myself:

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

     7  new File(musicLibraryDirName).eachDir { artistDir ->
       
     8      // Iterate over every listing within the artist listing
     9      // These are assumed to be album directories
       
    10      artistDir.eachDir { albumDir ->
       
    11          // Iterate over every file within the album listing
    12          // These are assumed to be content material or associated
    13          // (cowl.jpg, PDFs with liner notes and many others)
       
    14          // Initialize the counter for canopy.jpg
    15          // and the record for undesirable file names
       
    16          def coverCounter = 0
    17          def undesirableFileNames = []
       
    18          albumDir.everyFile { content materialFile ->
       
    19              // Analyze the file
       
    20              if (content materialFile.title ==~ /.*.(flac|mp3|ogg)/) {
    21                  // nothing to do right here
    22              } else if (content materialFile.title == 'cowl.jpg') {
    23                  coverCounter++
    24              } else {
    25                  undesirableFileNames << content materialFile.title
    26              }
       
    27          }
    28          println "${artistDir.name}|${albumDir.name}|$coverCounter|${unwantedFileNames.join(',')}"
    29      }
    30  }

Lines 1-2 outline the title of the music file listing.

Line 3-4 print the CSV file header.

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

Lines 14-17 arrange the cowl.jpg counter (ought to solely ever be zero or one) and the empty record through which we are going to accumulate undesirable file names.

Lines 18-27 analyze any recordsdata discovered within the album directories:

Lines 20-21 makes use of the Groovy match operator ==~ and a “slashy” common expression to test file title patterns. Nothing is completed with these recordsdata (see Part 2 for that data).

Lines 22-23 rely the cases of cowl.jpg (it ought to solely ever be zero or one).

Lines 24-26 report the names of any non-media, non-cover.jpg recordsdata to point out potential cruft or who-knows-what within the album directories.

Line 28 prints out the artist title, album title, cowl.jpg rely and record of undesirable file names.

That’s it!

Running the code

Typically, I run this as follows:

$ groovy TagAnalyzer3.groovy > tagAnalysis3.csv

Then I load the ensuing CSV right into a spreadsheet. For instance, with LibreOffice Calc , go to the Sheet menu and choose Insert sheet from file. When prompted, set the delimiter character to |. In my case, the outcomes seem like this:

(Chris Hermansen, CC BY-SA 4.0)

I’ve sorted this in rising order of the column “cover” to point out album sub-subsubdirectories that do not have cowl.jpg recordsdata. Note that some have cowl.png as a substitute. My expertise with music gamers is that a minimum of some do not play nicely with PNG format cowl pictures.

Also, observe that a few of these have PDF liner notes, additional picture recordsdata, M3U playlists, and so forth. In my subsequent article, I’ll present you the way to handle a number of the cruft.

Most Popular

To Top