topic title: Convert wav to flac
Posts: 11
Stripey
Joined: 31 Dec 2015
#1
Here is a simple script to convert wav files to flac using SoX or avconv (both standard with antiX).

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.


Posts: 119
wildstar84
Joined: 31 May 2014
#2
How 'bout just?:

sox [file].wav [file].flac
Posts: 11
Stripey
Joined: 31 Dec 2015
#3
In case it is not clear here are the files:

Directory: wav2flac or .wav2flac somewhere in user's files
Files: wav2flav the executable
readme.txt the help

Some users have found this script useful. You may like to give it a try.

Have fun and do it your way, always!
Posts: 1,445
skidoo
Joined: 09 Feb 2012
#4
Hey, thanks for sharing the script, and... why might one want to generate a"specro-thingie" for each converted audiofile?

I"looked it up", and although I'm familiar with using waveform view in audacity (to zoom in and mark exact splice endpoints)
not sure how I'd put an audio spectrogram to use by viewing it outside an editing context.
Hmm, by using an imageviewer, and scrolling through a directory of spectro- images...
can flesh out"this this and THIS needs to be cleaned up (normalized, or equalized)" ???

========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://manual.audacityteam.org/man/spectrogram_view.html"
linktext was:"http://manual.audacityteam.org/man/spec ... _view.html"
====================================


The Spectrogram View of an audio track provides a visual indication of how the energy in different frequency bands changes over time.
The Spectrogram can show sudden onset of a sound, so it can often be easier to see clicks and other glitches or to line up beats in this view
rather than in one of the waveform views.
Posts: 11
Stripey
Joined: 31 Dec 2015
#5
Thanks for finding a use for the images! There had to be more than reassuring"looks same" __{{emoticon}}__
Posts: 11
Stripey
Joined: 31 Dec 2015
#6
Got an interesting request to make the wav2flac script grab the folder name as it does file names.
"Sounds reasonable," I thought.

You can do this with the pwd command. No change in script required (but you could do that if you like).

With pwd we print the working directory to awk.
Awk parses the string into fields using the forward slash delimiter and prints the last field.

I'll do an echo test -- if you like open a terminal and try it for yourself.

Stripey@system:~
$ dest=/media/remDrv/Flac/"$(pwd | awk -F / '{ f=NF; print $f; }')"
Stripey@system:~
$ echo $dest
/media/remDrv/Flac/Stripey
Stripey@system:~
$ dest="$(pwd | awk -F / '{ f=NF; print $f; }')"
Stripey@system:~
$ echo $dest
Stripey
$


In the wavfile folder we wish to transcode we give"$dest" the path to our output directory.
We test with echo and call wav2flac when ready.

Code: Select all


# destination same name as current folder
dest="$(pwd | awk -F / '{ f=NF; print $f; }')"

# add path to removable drive, relative or absolute
dest=/media/remDrv/Flac/"$dest"

# etc...
dest=~/Music/"$dest - FLAC"

# test
echo" '$dest'"
# When it's good to go let 'er rip
wav2flac --directory"$dest"
# Tip: use tab-completion to avoid typos in directory path


The take away.