Categories
geek music mystuff tips tools

download cover art for all your albums, with powershell

Album covers are nice eye candy when you’re using a media player like Foobar2000 which automatically picks up a cover.jpg file in an album folder. The problem is that I have a lot of mp3 albums I ripped from my CD’s from way back and those don’t have any fancy cover art files.

I looked around for some tools that could automagically download covers for my albums but didn’t find anything handy. Since my music is structured in sub-folders like \ I thought this should be easy enough to parse and get pictures for.
If only there was a service that could easily provide those…

I tried the Musicbrainz API’s but that turned out to be hard to use and didn’t give me any covers for some test albums either. Then I thought of Last.fm. They have a lot of cover art, and their URL structure is the same as my folder structure… hmmm.

And here it is, a Powershell script which runs over your folder structure, tries to get the album page from Last.fm and then saves a cover.jpg image from the album page metadata.

A few things to know:

  • Your mp3’s are expected to be in a folder structure like (artist)\(album)\*.mp3
    E.g. The Prodigy\The Fat of the Land
  • If a folder contains any JPG or PNG image, it will be skipped. So that means you can run the script multiple times, and it will only download images once.
  • The “Various artists” folder is skipped by default because it didn’t fit the search pattern. If you store these type of albums in another folder, you might want to update that line with the correct folder name. If it does happen to process that folder in your case because of a different name, nothing will go wrong. It simply won’t find any album matches.

To use it, copy the code below in a file called get-albumart.ps1, or whatever name you fancy. Then run it as follows to get those pretty cover albums:

.\get-albumart.ps1 d:\music

And as always, this script comes as is, without any kind of warranty and you’re free to try it at your own risk. I wrote and used it and it worked great for me. I hope it works for you too. If Last.fm sues you because you’re downloading every image they have on the site because of your huge album collection? You didn’t get this script from me OK. Nope. Not me. ;-)

param ([Parameter(Mandatory=$true)][string]$path)

$progressPreference = 'silentlyContinue'
pushd
cd $path
$artistFolders = ls -directory | where { $_.name -ne "Various artists"}

foreach ($artistFolder in $artistFolders)
{
    $artist = $artistFolder.name
    write-host "::: $artist :::" -foregroundcolor green

    cd -Literalpath $artistFolder
    $releaseFolders = ls -directory
    
    foreach ($releaseFolder in $releaseFolders)
    {
        $release = $releaseFolder.name
        write-host "$release" -foregroundcolor cyan
        cd -literalpath "$releaseFolder"

        if ((test-path *.png) -or (test-path *.jpg))
        {
            write-host "- Images found, skipping."
        }
        else
        {
            $url = "https://www.last.fm/music/$($artist)/$($release)"
            $r = $null

            try 
            {
                $r = invoke-webrequest $url -usebasicparsing
            }
            catch 
            {
                write-host "- Release not found, skipping: $artist - $release" -foregroundcolor red
            }

            if ($r -ne $null)
            {
                $s = $r.content -split "`n" | where { $_ -like "*`"og:image`"*"} 
                $img = ($s -split '"') | where { $_ -like "*https*.jpg*" }

                if ($img -ne $null)
                {
                    write-host "- Downloading image for $artist - $release from $url"
                    invoke-webrequest $img -outfile cover.jpg
                }
                else
                {
                    write-host "- No image for $artist - $release from $url" -foregroundcolor yellow
                }
            }
        }
        cd ..
    }
    cd ..
}

popd
$progressPreference = 'Continue'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.