Ubuntu – How to fetch lyrics for all the music and store them within the files

lyricssoftware-recommendationtag

I have a large music library (most of them are .mp3 & .m4a) and none of them have lyrics. Is there any program or plug-in that automatically adds lyrics to all of my music by querying the internet?

Because I don't usually have an Internet connection I need a program which automatically adds all lyrics to music file tags so I can view them offline.

Best Answer

  • This is a semi answer as I don't know your environment and the code I provide is intended to serve as an example only - running it as is shouldn't do any harm but I can't give any guarantee.

    Tasks like this can be dealt with using simple bash scripts, e.g. in the following example I use eyeD3 to extract artist and title, then a free wikilyrics mirror to fetch the lyrics and then eyeD3 again to save the new tag. I encourage every one who will use this code to look up another lyrics API, as exploiting the same service over and over can be considered malicious.

    #!/bin/bash
    
    _TPL='http://makeitpersonal.co/lyrics?artist=<artist>&title=<title>'
    _SRY="Sorry, We don't have lyrics for this song yet."
    
    [ "$1" ] && _PATH="$1" || _PATH=$PWD
    
    cd $_PATH
    
    for _FILE in {*.mp3,*.m4a}; do
        if [[ -r $_FILE ]]; then
            _SONG=$(eyeD3 --no-color "$_FILE" | grep title)
    
            _ARTIST="${_SONG#*"artist: "}"
            _TITLE="${_SONG%"artist: "*}"
            _TITLE="${_TITLE#"title: "}"
    
            echo -n "$_ARTIST - $_TITLE"
    
            _ARTIST="${_ARTIST// /+}"
            _TITLE="${_TITLE// /+}"
            _URL="${_TPL//"<artist>"/$_ARTIST}"
            _URL="${_URL//"<title>"/$_TITLE}"
    
            _LYRICS=$(wget -qO- $_URL)
    
            if [ "$_LYRICS" != "$_SRY" ]; then
                eyeD3 --lyrics=eng:Lyrics:"$_LYRICS" "$_FILE" 1>/dev/null
            else
                echo "No lyrics found... skipping!"
            fi
        fi
    done
    
    cd $OLDPWD
    

    Instructions (run commands in terminal):

    1. install wget and eyeD3 by issuing sudo apt-get install wget eyed3
    2. save above code to a file, e.g. /home/user/lyrics_fetcher.sh
    3. add permission to run the file: chmod u+x /home/user/lyrics_fetcher.sh
    4. run the file (mind the quotes): /home/user/lyrics_fetcher.sh "path_to_my_album"
    5. you can stop script execution at any time by pressing ctrl+c

    I checked this code with "AM" album by Arctic Monkeys and it did sweetly.

    If you really want to fetch lyrics for all of your albums at once you can run the script in a loop for each directory, I guess. Still, I wouldn't use it as a final solution - wikilyrics and everyone who supports it by mirroring are good guys and this answer is here to promote thinking, not abuse.