Last active
June 6, 2017 13:21
-
-
Save isamert/aa4d062c471f7282201f085270759107 to your computer and use it in GitHub Desktop.
Splitting *.flac files with *.cue and creating mp3/ogg files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script takes all flac files in a folder, splits them according to cue file. | |
# Then converts all splitted flac files to mp3 (or ogg) files. | |
# Tags from the cue file kept in mp3 files. | |
# You need to install these packages in order to use this script: | |
# - cuetools | |
# - shntool | |
# - ffmpeg | |
# Change these settings as you wish: | |
# #################### # | |
type=ogg | |
folder=out | |
quality=7 | |
filename="%track% - %title%" | |
remove_flac=true | |
backup=true | |
# #################### # | |
# ############################################################################ # | |
# type -- output type, options are: | |
# ogg | |
# mp3 | |
# folder -- folder that mp3/ogg files will be created in | |
# quality -- mp3 quality options are: | |
# 320k | |
# [0-9] 0 is best(~245 kbit/s), 9 is worst quality(~65 kbit/s) | |
# -- ogg quality options are: | |
# [0-10] 0 being lowest, 10 highest quality | |
# | |
# filename -- mp3 file name template. | |
# %artist%, %album%, %title%, %track% | |
# | |
# remove_flac -- deletes created flac files. | |
# (It will not delete the orginal flac file) | |
# backup -- creates a folder name "flac", moves the flac and cue file to | |
# that folder with .backup extension | |
# moves all mp3 files to root folder | |
# ############################################################################ # | |
flac_out=split-track | |
# Create folder | |
mkdir $folder | |
cd $folder | |
# Split flac files | |
cuebreakpoints ../*.cue | shnsplit -o flac ../*.flac | |
# Copy metadata from cue file | |
cuetag.sh ../*.cue $flac_out*.flac | |
# Create mp3 files | |
codec="" | |
if [ $type = mp3 ]; then | |
codec="libmp3lame" | |
else | |
codec="libvorbis" | |
fi | |
quality_fixed="" | |
if [ $quality = 320k ]; then | |
quality_fixed='-b:a 320k' | |
else | |
quality_fixed="-qscale:a $quality" | |
fi | |
for flac in $flac_out*.flac; do | |
ffmpeg -i $flac -codec:a $codec $quality_fixed `basename $flac .flac`.$type | |
done | |
# Remove generated flac files | |
if [ $remove_flac = true ]; then | |
rm *flac | |
fi | |
# Rename mp3 files | |
function getTag() { echo $(ffprobe -loglevel error -show_entries format_tags="$1" -of default=noprint_wrappers=1:nokey=1 "$2"); } | |
for file in *$type; do | |
# BTW, I'm not a bash pro | |
artist=$(getTag artist "$file") | |
album=$(getTag album "$file") | |
title=$(getTag title "$file") | |
track=$(getTag track "$file") | |
file_name=${filename/\%artist\%/$artist} | |
file_name=${file_name/\%album\%/$album} | |
file_name=${file_name/\%title\%/$title} | |
file_name=${file_name/\%track\%/$track} | |
mv "$file" "$file_name.$type" | |
done | |
if [ $backup = true ] ; then | |
cd .. | |
mkdir flac | |
mv *.flac flac | |
mv *.cue flac | |
rm *.log | |
mv $folder/* . | |
rm -rf $folder | |
cd flac | |
rename .flac .flac.backup *.flac | |
rename .cue .cue.backup *.cue | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment