Skip to content

Instantly share code, notes, and snippets.

@Hiradur
Created November 6, 2016 13:15
Show Gist options
  • Select an option

  • Save Hiradur/344f76ffe2bcc3454edc947cd1ebb088 to your computer and use it in GitHub Desktop.

Select an option

Save Hiradur/344f76ffe2bcc3454edc947cd1ebb088 to your computer and use it in GitHub Desktop.
Script which converts all flac files inside a folder to mp3, ogg vorbis or opus and puts them in another directory, maintaining the artist/album directory path scheme
#!/bin/sh
set -eu
# folder_enc.sh
# Original author: Hiradur
# License: CC0
# Description: Converts all flac files in a given folder to mp3, vorbis or opus
# files and put them in a different directory while maintaining the artist/album
# folder structure.
# select filetype and codec (use ogg for vorbis)
CONVERT_TO=mp3 # mp3, ogg or opus
# get the last two directory strings of pwd
# Example: pwd is "/Music/Vollenweider, Andreas/Down To The Moon"
# then we get "Vollenweider, Andreas/Down To The Moon"
albumpath=$(pwd | sed 's#.*[/]\([^/]*\)[/]\([^/]*\)$#\1/\2#')
# converted files will be put into
# ~/Music/<filetype>/Vollenweider, Andreas/Down To The Moon/
mkdir -p "$HOME/Music/$CONVERT_TO/$albumpath"
for file in *.flac
do
# path to output file
file2=${albumpath}/${file%.flac}
if [ $CONVERT_TO = mp3 ]; then
ffmpeg -i "$file" -codec:a libmp3lame -q:a 1 "$HOME/Music/mp3/$file2.mp3"
elif [ $CONVERT_TO = ogg ]; then
oggenc "$file" -q7 -o "$HOME/Music/ogg/$file2.ogg"
elif [ $CONVERT_TO = opus ]; then
opusenc --bitrate 224 "$file" "$HOME/Music/opus/$file2.opus"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment