Split and encode FLAC/CUE to mp3/ogg in one run.

flacsFew days ago i  needed to split and encode one flac file to mp3. I found few solutions, one of them only split flac file, other encode flac to mp3, but no one do not do it in one run.
So, i wrote script, you must specify flac and cue/toc files, after that script will convert flac file to group of mp3 or ogg files and will add tags.
To get this script work, you need to install next packages “cuetools”, “shntool”, “id3v2”, “vorbis-tools” and “lame”.
You will not found lame in standard repositories of squeeze, but you can install it from backports or from debian multimedia repository (all packages available in modern debian based distribution, at least I tested it in debian 8-10).
Usage: flac2mp3 -f /path/to/flac.flac -s /path/to/cue.cue
Also, you can choose between mp3 or ogg with swich -e mp3 or -e ogg (mp3 will be used by default).

Latest version of script available there: https://gist.github.com/IvanBayan/b8a1e7a1629de20b06ef6dc44843c9fe

#!/bin/bash
 
LAMEOPTS="-b 320 --quiet"
OGGOPTS="-b 320 --quiet"
 
while getopts ":s:f:e:" opt; do
  case $opt in
    f)
      FILE="$OPTARG"
      ;;
    s)
      SPLIT="$OPTARG"
      ;;
    e)
      FMT="$OPTARG"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done
 
# Set default format to mp3
case ${FMT:="mp3"} in
    mp3|MP3|ogg|OGG)
    ;;
    *)
        echo "Unknown format $FMT" >&2
        exit 1
    ;;
esac
 
# Check both files
if [ ! -f "$FILE" -o ! -f "$SPLIT" ]
then
    echo "You must specify correct flac file and CUE/TOC file." >&2
    exit 1
fi
 
#Get number of tracks
NUMTRACKS=`cueprint -d '%N' "${SPLIT}"`
 
for i in `seq 1 $NUMTRACKS`
do
    # Clear previous obtained variables 
    unset PERFORMER
    unset ALBUM
    unset TITLE
 
    # Set performer, album, track title
    PERFORMER=`cueprint -n $i -t '%p' "${SPLIT}"`
    ALBUM=`cueprint -d '%T' "${SPLIT}"`
    TITLE=`cueprint -n $i -t '%t' "${SPLIT}"`
 
    ## Check perfomer, album and track title
    if [ -z "$PERFORMER" ]
    then
        echo "Track $i: Can not obtain performer from cue, set it to 'Unknown Artist'" >&2
        PERFORMER="Unknown Artist"
    fi
 
    if [ -z "$ALBUM" ]
    then
        echo "Track $i: Can not obtain album from cue, set it to 'Unknown Album'" >&2
        ALBUM="Unknown Album"
    fi
 
    if [ -z "$TITLE" ]
    then
        echo "Track $i: Can not obtain track from cue, set it to 'Track $i'" >&2
        TITLE="Track $i"
    fi
 
    ## End
 
    # Split and encoding files
    echo "Encoding track $i/$NUMTRACKS."
    if [ "$FMT" = "mp3" -o "$FMT" = "MP3" ] 
    then        
        cuebreakpoints "$SPLIT"| shnsplit -q -o "cust ext=mp3 lame  $LAMEOPTS - %f" \
-x $i -O always -a "" -z " - $TITLE" "$FILE"
        OUTPUT=`printf "%.2d - ${TITLE}.mp3" $i`
        id3v2 -T $i -a "$PERFORMER" -A "$ALBUM" -t "$TITLE" "$OUTPUT"
    fi
 
    if [ "$FMT" = "ogg" -o "$FMT" = "OGG" ]
    then
        cuebreakpoints "$SPLIT"|shnsplit -q -o "cust ext=ogg oggenc $OGGOPTS -o %f -" \
-x $i -O always -a "" -z " - $TITLE" "$FILE"
        OUTPUT=`printf "%.2d - ${TITLE}.ogg" $i`
        echo -e "TRACKNUMBER=${i}\nARTIST=${PERFORMER}\nPERFORMER=${PERFORMER}" \
                             "\nALBUM=${ALBUM}\nTITLE=${TITLE}\n"|vorbiscomment "$OUTPUT"
    fi    
 
done

7 comments on “Split and encode FLAC/CUE to mp3/ogg in one run.

  • thanks, got a few problems with cuetools 1.4 and shnsplit 3.0.10 on ubuntu 18.04:
    – missing performer info with lower-case “%p” param, fixed with upper-case “%P” param, f.e.
    PERFORMER=`cueprint -n $i -t ‘%P’ “${SPLIT}”`

    – wrong split timings – multiple tracks with short random duration (<1min) and long/huge last one, fixed with additional "0" in INDEX lines in cue file, f.e.
    bad timing example
    INDEX 01 04:15:11
    fixed timing example
    INDEX 01 04:15:110

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>