Archive for March, 2013

Automatically create genre playlists of your music collection

Sunday, March 3rd, 2013 | Linux, Tech-savvy | No Comments

This script will create playlists based on the tags of your mp3 and flac files. Prerequisites: id3v2/metaflac command line tool (sudo apt-get install id3v2 metaflac) and tagged mp3/flac files.

Note: The id3v2 command only works up to version 2.3 of the id3v2 spec (which is pretty much the default), so this script will not work on v2.4 files. It seems a lot of people still use 2.3 as 2.4 does not give a lot of advantages and 2.3 is way more compliant with mp3 playback devices. I just came across this issue because I use EasyTag for tagging and this tool writes v2.4 by default. After setting EasyTag’s prefs to v2.3 I was good to go 🙂

#!/bin/bash
 
# set extraction command chain for each file type
EXTMP3="id3v2 -l \"_FILE\" | sed -n \"s/TCON (Content type): \(.*\) (.*)/\1/p\""
EXTFLAC="metaflac --show-tag=GENRE \"_FILE\" | sed -n \"s/GENRE=\(.*\)/\1/p\""
 
# delete old playlists
rm *.m3u
 
function createPlaylist {
  while IFS= read -r -d '' FILE; do
    echo Processing \"$FILE\"
    GENRE=`eval ${2/_FILE/$FILE}`
    if [ "$GENRE" ]
      then
        echo "$FILE" >> "$GENRE.m3u"
    fi
    echo "$FILE" >> "All.m3u"
  done < <(find . -name "*.$1" -print0 | sort -z)
}
 
createPlaylist mp3 "$EXTMP3"
createPlaylist flac "$EXTFLAC"

Basically we loop over all files and extract the genre’s nice name using a file type specific chain of commands. If a genre is found the file name is appended to the playlist that is named like the genre itself. Additionally an “All” playlist is created that holds every file found, even the ones that have no genre set.

Tags: , , , , , , , ,

Search

Categories