Eases converting your wav collection to a smaller format for portable players etc...
Script converts wav files in current directory to flac in destination folder.
Normalization of volume is default but is configurable and optional.
Files are compared. Also a set of spectrographs can be generated.
A small help file is included.
Tested: works on Ubuntu, Mac (homebrew), and antiX.
Enjoy!
Code: Select all
Edited to include code as files as attachments not allowed :|
#!/bin/bash
# Convert WAV to FLAC -- normalize and prevent clipping, show progress
# Author: mo@snille.com Date: 08 Jan 2017 (Edited: 7)
test=$#; use_avconv=0; ghlp=0; nrm=0; s=0; outdir=FLAC
# Can add usr path to help: for fdir in ~/ /usr; do
# find $fdir -type f -regex '.*wav2flac\/readme.txt' -exec more '{}' \;; done
["$1" ="-h" -o"$1" ="--help" ] && { find ~/ -type f -iregex '.*wav2flac\/readme.txt' -exec more '{}' \; || echo"See readme.txt in wav2flac directory."; exit ; }
# No arguments passed
[ $test = 0 ] && { echo -e"*** No arguments passed. ***\n
\tUsage: [./]wav2flac [[\"]output dir[\"]]\n
Default output subdirectory FLAC will be used.\n"
read -p" OK? [y/N]" -n 1 svar;
["$svar" ="y" -o"$svar" ="Y" ] && echo || { echo -e"\n\tBailing out!\n"; exit; }
}
# Get arguments
while [[ $# -gt 1 ]]; do
case $1 in
-a | --avc*) use_avconv=$2;;
-d | --dir*) outdir=$2;;
-n | --nor*) nrm="$2";;
-s | --spe*) s="$2";;
esac
shift # past argument/value
done
# echo ghlp=$ghlp $outdir=$outdir nrm=$nrm avconv=$use_avconv s=$s
# exit
# Check for dest dir
[ -e"$outdir" ] && echo"
Found directory \"$outdir\": OK" || { mkdir -p"$outdir" && echo -e"\nI made \"$outdir\" directory.\n"; }
# Convert & compare -- with sox not ffmpeg -i $f -o ${f/.wav/.flac} nor avconv
[ -d /tmp/wav2flac ] || mkdir /tmp/wav2flac;
# find *wav and *WAV files in this directory
# Allow mix of wav & WAV names
# sl="" # list of temp symbolic links
# if both wav & WAV...
[ `ls *.WAV 2>/dev/null | wc -l` != 0 -a `ls *.WAV 2>/dev/null | wc -l` != 0 ] && {
for f in *.WAV; do
# [ -e"${f/.WAV/.wav}" ] && {
[ `ls"${f/.WAV/.wav}" | wc -l` != 0 ] && {
echo -e"\n*** Converting $f to ${f/.WAV/_W.flac} to avoid naming conflict. ***\n"
sl="${f/.WAV/_W.wav}"; } || sl="${f/.WAV/.wav}"
ln -s"$f""$sl" && echo $sl >> /tmp/wav2flac/tmplist
done; }
# exit
# Convert
echo -e"\nBegin converting.\n"
# [ -e *.wav ] && test=wav || test=WAV;
f=`ls *.wav | wc -l 2>/dev/null`; [ $f != 0 ] && test=wav || test=WAV;
for f in *.$test ; do
["$f" !="*.$test" ] && {
["$use_avconv" !="1" ] && { echo"SoX"
sox -S --temp"/tmp/wav2flac/" "$f""$outdir/${f/.$test/.flac}" --norm=$nrm
# -magic not compiled?
# If SoX fails or use_avconv forced
} || { echo avconv; avconv -i"$f""$outdir/${f/.$test/.flac}"; }
}
done
# Compare
echo -e"\nCompare:\n"
for f in *."$test" ; do
["$f" !="*.$test" ] && {
# echo -n"Sample count : `soxi -s"$f"`"
soxi"$f";
# echo -n"Sample count : `soxi -s"$f"`"
soxi"$outdir/${f/.$test/.flac}";
[ $s = 1 ] && { echo -e"Creating spectrographs...\n"
sox -S -G --temp=/tmp/wav2flac"$f" -n spectrogram -x 1024 -y 768\
-z 100 -t"$f" -c '' -o"$outdir/${f/.$test/_wav_.png}"
sox -S -G --temp=/tmp/wav2flac"$outdir/${f/.$test/.flac}" -n spectrogram\
-x 1024 -y 768 -z 100 -t"$outdir/${f/.$test/.flac}" -c '' -o"$outdir/${f/.$test/.png}"
} # end create spectrographs
} done
# Tidy any temp symlinks
touch /tmp/wav2flac/tmplist; sl="";
while read sl; do rm"$sl"; done < /tmp/wav2flac/tmplist
# unset slinks
# Tidy temp directory
rm -r /tmp/wav2flac
echo -e"\nBye!"
Refers to: wav2flac [Friday 13 january 2017]
Script converts wav files in current directory to flac in destination folder.
Normalization of volume is default but is configurable and optional.
Files are compared. Optionally a set of spectrographs generated.
Tips:
You may need to set the executable permission.
Example: chmod +x wav2flac
For ease of use make a symbolic link to the script you prefer.
Example: ln -s [/path/].wav2flac/wav2flac /usr/bin/wav2flac
Basic Usage: cd [directory with wav files to convert]
wav2flac
Advanced: wav2flac [-d ["][full path]output directory["]] [-a 0|1] [-n #] [-s 0|1]
Options:
-a (0|1) Transcode with avconv if set to 1. Default: 0
Uses Sound Exchange (SoX) by default.
Also --avconv [0|1]
-d Output directory.
Default is FLAC subdirectory.
Use escape characters or quote marks as required.
Directory will be created if not found.
Also --directory
-n (#) Normalize volume to prevent clipping. SoX only,
Default is 0 dB: -n 0. Max number is 0.
Use avconv (see above) if normalization not desired.
Also --normalize (#)
-s (0|1) Make spectrographs. Default: -s 0 no spectrographs.
Images placed in output directory.
Also --spectrograph [0|1]
More info:
Some advice on audio transcoding
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://www.learndigitalaudio.com/normalize-audio"
linktext was:"http://www.learndigitalaudio.com/normalize-audio"
====================================
Peak normalization
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"https://en.wikipedia.org/wiki/Audio_normalization#Peak_normalization"
linktext was:"https://en.wikipedia.org/wiki/Audio_nor ... malization"
====================================
man sox
man soxi
man avconv
Bug reports to:"Bugs bugs bugs!" <mo@snille.com>
Known issues:
1. an error message - no ramifications:
ls: cannot access *.wav: No such file or directory
When looking at directories with *.WAV only filenames.