I found this script on github, modified it slightly, also created version for fdk-aac.
Tested with fdkaac 0.6.3 and opusenc opus-tools 0.1.9 (using libopus 1.1.5)
* Keeps directory structure
* Copies metadata
* Detects changed files
* Uses parallel
flac2opus.sh
#!/bin/bash
export BITRATE=64
function usage
{
echo
echo "Usage:"
echo " $0 FLAC_DIR LOSSY_DIR"
}
function encode
{
if [ ! -f "$LOSSY_DIR"/"$(echo "$1" | sed 's/.flac/.opus/')" ]; then
ARTIST=`metaflac "$FLAC_DIR"/"$1" --show-tag=ARTIST | sed s/.*=//`
TITLE=`metaflac "$FLAC_DIR"/"$1" --show-tag=TITLE | sed s/.*=//g`
ALBUM=`metaflac "$FLAC_DIR"/"$1" --show-tag=ALBUM | sed s/.*=//g`
TRACKNUMBER=`metaflac "$FLAC_DIR"/"$1" --show-tag=TRACKNUMBER | sed s/.*=//g`
DISCNUMBER=`metaflac "$FLAC_DIR"/"$1" --show-tag=DISCNUMBER | sed s/.*=//g`
echo "encoding $1"
flac -s -c -d "$FLAC_DIR"/"$1" | opusenc --quiet --bitrate "$BITRATE" --artist "$ARTIST" --album "$ALBUM" \
--title "$TITLE" --comment "TRACKNUMBER=$TRACKNUMBER" --comment "DISCNUMBER=$DISCNUMBER" \
- "$LOSSY_DIR"/"$(echo "$1" | sed 's/\.flac$/\.opus/')"
fi
}
export -f encode
#If user didn't provide 2 arguments, quit.
if [ "$#" -ne 2 ]; then
usage
exit
fi
export FLAC_DIR="$1"
export LOSSY_DIR="$2"
#If first argument isn't a directory, quit.
if [ ! -d "$FLAC_DIR" ]; then
usage
exit
fi
#if LOSSY_DIR doesn't exist, create it
if [ ! -d "$LOSSY_DIR" ]; then
mkdir "$LOSSY_DIR"
fi
#recreate the folder structure in lossy
echo "Creating the folder structure."
diff --new-line-format="" --unchanged-line-format="" \
<(find "$FLAC_DIR" -mindepth 1 -maxdepth 3 -type d -printf '%P\n' | sort) \
<(find "$LOSSY_DIR" -mindepth 1 -maxdepth 3 -type d -printf '%P\n' | sort) |while read fname; do
mkdir -v -p "$LOSSY_DIR/$fname"
done
echo "Encoding missing songs."
diff --new-line-format="" --unchanged-line-format="" \
<(find "$FLAC_DIR" -mindepth 1 -name \*.flac -printf '%P\n' | sort) \
<(find "$LOSSY_DIR" -mindepth 1 -name \*.opus -printf '%P\n' | sed 's/\.opus$/\.flac/g' | sort) \
| parallel --no-notice encode
flac2aac.sh
#!/bin/bash
export FDKAAC_PROFILE=5
export FDKAAC_BITRATE_MODE=2
function usage
{
echo
echo "Usage:"
echo " $0 FLAC_DIR LOSSY_DIR"
}
function encode
{
if [ ! -f "$LOSSY_DIR"/"$(echo "$1" | sed 's/.flac/.m4a/')" ]; then
ARTIST=`metaflac "$FLAC_DIR"/"$1" --show-tag=ARTIST | sed s/.*=//`
TITLE=`metaflac "$FLAC_DIR"/"$1" --show-tag=TITLE | sed s/.*=//g`
ALBUM=`metaflac "$FLAC_DIR"/"$1" --show-tag=ALBUM | sed s/.*=//g`
TRACKNUMBER=`metaflac "$FLAC_DIR"/"$1" --show-tag=TRACKNUMBER | sed s/.*=//g`
DISCNUMBER=`metaflac "$FLAC_DIR"/"$1" --show-tag=DISCNUMBER | sed s/.*=//g`
echo "encoding $1"
flac -s -c -d "$FLAC_DIR"/"$1" | fdkaac -S -p "$FDKAAC_PROFILE" -m "$FDKAAC_BITRATE_MODE" --artist "$ARTIST" --album "$ALBUM" \
--title "$TITLE" --comment "TRACKNUMBER=$TRACKNUMBER" --comment "DISCNUMBER=$DISCNUMBER" \
- -o "$LOSSY_DIR"/"$(echo "$1" | sed 's/\.flac$/\.m4a/')"
fi
}
export -f encode
#If user didn't provide 2 arguments, quit.
if [ "$#" -ne 2 ]; then
usage
exit
fi
export FLAC_DIR="$1"
export LOSSY_DIR="$2"
#If first argument isn't a directory, quit.
if [ ! -d "$FLAC_DIR" ]; then
usage
exit
fi
#if LOSSY_DIR doesn't exist, create it
if [ ! -d "$LOSSY_DIR" ]; then
mkdir "$LOSSY_DIR"
fi
#recreate the folder structure in lossy
echo "Creating the folder structure."
diff --new-line-format="" --unchanged-line-format="" \
<(find "$FLAC_DIR" -mindepth 1 -maxdepth 3 -type d -printf '%P\n' | sort) \
<(find "$LOSSY_DIR" -mindepth 1 -maxdepth 3 -type d -printf '%P\n' | sort) |while read fname; do
mkdir -v -p "$LOSSY_DIR/$fname"
done
echo "Encoding missing songs."
diff --new-line-format="" --unchanged-line-format="" \
<(find "$FLAC_DIR" -mindepth 1 -name \*.flac -printf '%P\n' | sort) \
<(find "$LOSSY_DIR" -mindepth 1 -name \*.m4a -printf '%P\n' | sed 's/\.m4a$/\.flac/g' | sort) \
| parallel --no-notice encode