RIP freedb.org -- Add notes/todos re migrating to MusicBrainz
[zcdrip] / zcdrip.in
index 768799b..6312876 100644 (file)
--- a/zcdrip.in
+++ b/zcdrip.in
@@ -1,11 +1,11 @@
 #!/bin/zsh
 
-## Copyright (C) 2006 - 2008 Steve Youngs
+## Copyright (C) 2006 - 2013 Steve Youngs
 
-## Author:        Steve Youngs <steve@sxemacs.org>
-## Maintainer:    Steve Youngs <steve@sxemacs.org>
+## Author:        Steve Youngs <steve@steveyoungs.com>
+## Maintainer:    Steve Youngs <steve@steveyoungs.com>
 ## Created:       <2006-08-08>
-## Time-stamp:    <Thursday Jun 12, 2008 12:29:31 steve>
+## Time-stamp:    <Saturday Feb 20, 2021 12:38:28 steve>
 
 ## Redistribution and use in source and binary forms, with or without
 ## modification, are permitted provided that the following conditions
@@ -45,7 +45,8 @@
 ##    o Partial album ripping.  Specify track numbers or a range of
 ##      track numbers to only rip/encode those tracks.
 ##
-##    o Ripping from music DVDs
+##    o Rewrite online metadata retrieval to use MusicBrainz instead
+##      of freedb.org as the latter no longer exists.
 ##
 
 ### Bugs:
@@ -62,7 +63,7 @@ ourname=${0##*/}
 
 # Version info.
 VERSION=@VERSION@
-COPYRIGHT="Copyright (C) 2006 - 2008 Steve Youngs <steve@sxemacs.org>"
+COPYRIGHT="Copyright (C) 2006 - 2021 Steve Youngs <steve@steveyoungs.com>"
 
 version_str="${ourname}: ${VERSION}
 ${COPYRIGHT}"
@@ -93,6 +94,7 @@ Synopsis:
         [ -a ALBUM ] [ --album=ALBUM ] [ -s ARTIST ] [ --sameartist=ARTIST ]
        [ -t ENCODING_TYPE ] [ --type=ENCODING_TYPE ] [ -f | --flac_copy ]
        [ -e FILE | --edit_rcfile=FILE ] [ -O | --online ] [ -d DIR | --dir=DIR ]
+        [ -p PLAYLISTFILE | --playlist=PLAYLISTFILE ]
   $ourname 
         [ -h | --help ]
   $ourname
@@ -103,7 +105,7 @@ Description:
 
   $ourname is a "frontend" to the process of ripping audio (cdda) CDs
   to a hard disk and encoding the audio tracks to either MP3 format or
-  Ogg Vorbis format.
+  Ogg/Vorbis format.
 
   File names are auto-generated from track titles, with illegal
   characters removed and spaces converted to underscores.
@@ -111,6 +113,10 @@ Description:
   Track title, artist, and album names are added to the ID3 tags or
   Vorbis comments header.
 
+  If the file: '\${PWD}/cover.jpg' exists it will be embedded into
+  each track file as "Front Cover" art.  This is only supported for
+  Ogg/Vorbis and FLAC formats.
+
 Options:
 -------
 
@@ -126,8 +132,8 @@ Options:
         Save encoded files to the directory, DIR.  If flac encoding
         is also requested, see '--flac_copy' option below, the flac
         files will be saved to DIR/flac.  If this option is omitted,
-        it defaults to 'PWD/ARTIST/ALBUM' the album name is first
-        converted to a sane filename.
+        it defaults to: '\${PWD}/ARTIST/ALBUM' (with sanitised
+        filenames).
 
   -e FILE
   --edit_rcfile=FILE
@@ -137,8 +143,8 @@ Options:
 
   -f
   --flac_copy
-        In addition to encoding the CD audio tracks to MP3 or Ogg
-        Vorbis, create a FLAC audio file for archival purposes.
+        In addition to encoding the CD audio tracks to Ogg/Vorbis or
+        MP3, create a FLAC audio file for archival purposes.
 
   -h
   --help
@@ -154,11 +160,22 @@ Options:
 
   -o
   --ogg
-        Specify Ogg Vorbis encoding.
+        Specify Ogg/Vorbis encoding.
 
   -O
   --online
         Attempt to grab the track info from freedb.freedb.org.
+        DO NOT USE.
+
+        freedb.org no longer exists.  The plan is for zcdrip to
+       use MusicBrainz instead of freedb.org but it is not yet
+       implemented.
+
+  -p PLAYLISTFILE
+  --playlist=PLAYLISTFILE
+        Write a playlist file to PLAYLISTFILE.  Argument PLAYLISTFILE
+        is _NOT_ optional, and should be given as the full path to the 
+        file.
 
   -r FILE
   --rcfile=FILE
@@ -168,7 +185,7 @@ Options:
            Artist Name|Track Title
 
         One "record" per line.  Whitespace and punctuation is permitted,
-        blank lines are not.  And there is no "comment" lines.
+        blank lines are not.  And there are no "comment" lines.
 
   -s ARTIST
   --sameartist=ARTIST
@@ -195,10 +212,10 @@ Requirements:
 
     lame        http://www.mp3dev.org/
                 For encoding to MP3 format. (optional if encoding to
-                Ogg Vorbis)
+                Ogg/Vorbis)
 
     oggenc      http://www.xiph.org/
-                For encoding to Ogg Vorbis format (optional if encoding
+                For encoding to Ogg/Vorbis format (optional if encoding
                 to MP3)
 
     flac        http://flac.sourceforge.net/
@@ -257,6 +274,11 @@ Environment Variables:
 
 Example Usage:
 -------------
+  ## Note:
+  Currently, online metadata retrieval is not possible because freedb.org
+  no longer exists.  We will be converting to MusicBrainz but that is yet
+  to be implemented.  In the meantime, the examples below that use the
+  '-O' option are presently not valid.
 
   Probably the most common usage would be to encode to Ogg/Vorbis
   format using track info obtained from freedb.freedb.org.  Like
@@ -304,6 +326,8 @@ ${COPYRIGHT}
 
 EOF
 }
+# Cover Art
+ARTWORK=$(pwd)/cover.jpg
 
 # Convert song title to something resembling a sane filename... no
 # whitespace, no punctuation, etc.  Also adds .ogg/.mp3 extension.
@@ -326,6 +350,7 @@ fnamefix ()
     done
 }
 
+### FIXME: freedb.org no longer exists. Convert to MusicBrainz.
 ### freedb magic
 _freedb_login ()
 {
@@ -353,6 +378,7 @@ _freedb_login ()
     return 0
 }
 
+### FIXME: freedb.org no longer exists. Convert to MusicBrainz.
 _parse_cddb ()
 {
     local various=yes
@@ -393,15 +419,27 @@ _parse_cddb ()
     fi
 
     # create an rcfile
+    :>$rcfile                  # make sure it is empty
+
+    for (( i=0; i<$tracks; i++ )); do
+       TRACK=""
+       for TITLE in $(grep TTITLE${i}= ${zlog}|cut -d= -f2); do
+           TRACK="${TRACK} ${TITLE}"
+       done
+       TRACK=${TRACK//[[:cntrl:]]/}
+       TRACK=${TRACK/[[:space:]]/}
+       echo ${TRACK} >> ${rcfile}
+    done
+
     if [[ $various == yes || -n $(grep TTITLE $zlog|grep $art) ]]; then
-       grep TTITLE $zlog|cut -d= -f2|sed 's@ [-/] @|@' > $rcfile
+       sed -i 's@ [-/] @|@g' $rcfile
     else
-       grep TTITLE $zlog|cut -d= -f2 > $rcfile
        sed -i "s@^@${art}|@g" $rcfile
     fi
     
 }
 
+### FIXME: freedb.org no longer exists. Convert to MusicBrainz.
 _freedb_read ()
 {
     if [[ -z $1 ]]; then
@@ -420,6 +458,7 @@ _freedb_read ()
     
 }
 
+### FIXME: freedb.org no longer exists. Convert to MusicBrainz.
 _freedb_query ()
 {
     if [[ -z $1 ]]; then
@@ -488,6 +527,7 @@ _freedb_query ()
     esac
 }
 
+### FIXME: freedb.org no longer exists. Convert to MusicBrainz.
 do_online ()
 {
     autoload -U tcp_open
@@ -535,7 +575,7 @@ cleanup ()
 _album_to_dir ()
 {
     local talbum=$album
-    local tart=$art
+    local tart=${ARTIST:-$art}
     local psub="[[:cntrl:],\`';/\\\\\"]"
     local prep=""
     local wsub="[[:space:][:blank:]]"
@@ -561,7 +601,11 @@ _album_to_dir ()
 
     dir=$(pwd)/${tart}/${talbum}
     [[ -d ${dir} ]] || mkdir -p ${dir}
-    [[ $keep_flac == yes ]] && fdir=${dir}/flac; mkdir -p ${fdir}
+
+    if [[ $keep_flac == yes ]]; then
+       fdir=${dir}/flac
+       mkdir -p ${fdir}
+    fi
 }
        
 
@@ -593,6 +637,7 @@ EOF
        $_zedit $rcfile
     fi
 
+    album=${ALBUM:-$album}
     if [[ -z $album ]]; then
        echo -n "Please enter the name of this album: "
        read album
@@ -608,12 +653,14 @@ EOF
        fi
     fi
 
+    year=${YEAR:-$year}
     if [[ -z $year ]]; then
        echo -n "Please enter the year this album was released: "
        read year
        echo
     fi
 
+    genre=${GENRE:-$genre}
     if [[ -z $genre ]]; then
        echo -n "Please enter the genre of this album: "
        read genre
@@ -662,7 +709,8 @@ flacenc ()
 {
     file=$1
     shift                      # pop off $1 (flacfile)
-    flac -8 --delete-input-file -o ${fdir}/${file} ${1}
+    [ -f ${ARTWORK} ] && PIC="--picture=${ARTWORK}" || PIC=""
+    flac -8 ${PIC} --delete-input-file -o ${fdir}/${file} ${1}
     shift                      # pop off next arg (wavfile)
     for tag in title artist album year genre tracknumber comment; do
        metaflac --set-tag="$tag=${1}" ${fdir}/${file}
@@ -676,13 +724,13 @@ flactomp3 ()
     mp3=$2
 
     # tags
-    title=$(metaflac --show-tag=title|cut -d= -f2)
-    artist=$(metaflac --show-tag=artist|cut -d= -f2)
-    album=$(metaflac --show-tag=album|cut -d= -f2)
-    year=$(metaflac --show-tag=year|cut -d= -f2)
-    genre=$(metaflac --show-tag=genre|cut -d= -f2)
-    track=$(metaflac --show-tag=tracknumber|cut -d= -f2)
-    comment=$(metaflac --show-tag=comment|cut -d= -f2)
+    title=$(metaflac --show-tag=title ${flac}|cut -d= -f2)
+    artist=$(metaflac --show-tag=artist ${flac}|cut -d= -f2)
+    album=$(metaflac --show-tag=album ${flac}|cut -d= -f2)
+    year=$(metaflac --show-tag=year ${flac}|cut -d= -f2)
+    genre=$(metaflac --show-tag=genre ${flac}|cut -d= -f2)
+    track=$(metaflac --show-tag=tracknumber ${flac}|cut -d= -f2)
+    comment=$(metaflac --show-tag=comment ${flac}|cut -d= -f2)
 
     flac -c -d $flac|lame -h --tt ${title} --ta ${artist} --tl ${album} \
        --ty $year --tg $genre --tn $track --tc ${comment} - $mp3
@@ -692,7 +740,7 @@ flactomp3 ()
 mp3rip ()
 {
     # Work around lame being too anal about genre
-    lame --genre-list|grep ${genre} 1>/dev/null || genre=Other
+    lame --genre-list 2>&1 | grep -q ${genre} || genre=Other
 
     for (( i=1; i<=$tracks; ++i )); do
        echo MP3-Ripping: $song[$i], by: $artist[$i], to: $filename[$i]
@@ -708,6 +756,7 @@ mp3rip ()
                    --ty $year --tg $genre --tc $zcomment $wavfile \
                    --tn $i ${dir}/$filename[$i]
            fi
+           [[ $write_playlist == yes ]] && echo ${dir}$filename[$i] >> $plfile
        fi
        [[ -f $wavfile ]] && rm -f $wavfile
     done
@@ -731,6 +780,8 @@ oggrip ()
                    -N $i $wavfile
                rm $wavfile
            fi
+           [ -f ${ARTWORK} ] && album_art ${dir}/$filename[$i]
+           [[ $write_playlist == yes ]] && echo ${dir}/$filename[$i] >> $plfile
        fi
     done
 }
@@ -738,7 +789,7 @@ oggrip ()
 # Make sure we have everything we need.
 # This might be a little over the top, but I don't see any point in
 # checking for `oggenc' if you are encoding to MP3, likewise, there's
-# no point in checking for `lame' if you are encoding to Ogg Vorbis.
+# no point in checking for `lame' if you are encoding to Ogg/Vorbis.
 chkreqs ()
 {
     reqs=(cdparanoia lame oggenc flac)
@@ -779,7 +830,7 @@ chkreqs ()
        
 
 # Command line parsing
-args=mofihVO-:r:a:s:t:e:d:
+args=mofihVO-:r:a:s:t:e:d:p:
 flavour=0
 rc=0
 interactive=no
@@ -805,6 +856,7 @@ while getopts $args opts; do
                (album?*) album=${OPTARG/album=/} ;;
                (sameartist?*) art=${OPTARG/sameartist=/} ;;
                (dir?*) dir=${OPTARG/dir=/} ;;
+               (playlist?*) write_playlist=yes; plfile=${OPTARG/playlist=/} ;;
                (version) _version; exit 0 ;;
                (help) cmd=usage; rv=0 ;;
                (*) 
@@ -824,6 +876,7 @@ while getopts $args opts; do
        (s) art=$OPTARG ;;
        (t) atype=$OPTARG; (( ++flavour )) ;;
        (d) dir=$OPTARG ;;
+       (p) write_playlist=yes; plfile=$OPTARG ;;
        (V) _version; exit 0 ;;
        (h) cmd=usage; rv=0 ;;
        (*) rv=2 ;;