It is currently Fri Jun 23, 2017 5:20 pm



Welcome
Welcome to antiX-forum.

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, join our community today!


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Convert wav to flac
PostPosted: Thu Feb 02, 2017 9:09 am 
Offline

Joined: Thu Dec 31, 2015 12:33 pm
Posts: 11
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:


Edited to include code as files as attachments not allowed  :|
#!/bin/bash

# Convert WAV to FLAC -- normalize and prevent clipping, show progress
#   Author: [email protected]   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!"


Quote:

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
http://www.learndigitalaudio.com/normalize-audio

Peak normalization
https://en.wikipedia.org/wiki/Audio_nor ... malization

man sox
man soxi
man avconv


Bug reports to: "Bugs bugs bugs!" <[email protected]>


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.





Top
 Profile  
 
 Post subject: Re: Convert wav to flac
PostPosted: Thu May 18, 2017 2:59 am 
Offline

Joined: Sat May 31, 2014 6:50 pm
Posts: 105
How 'bout just?:

sox [file].wav [file].flac


Top
 Profile  
 
 Post subject: Re: Convert wav to flac
PostPosted: Fri May 26, 2017 4:49 pm 
Offline

Joined: Thu Dec 31, 2015 12:33 pm
Posts: 11
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!


Top
 Profile  
 
 Post subject: Re: Convert wav to flac
PostPosted: Fri May 26, 2017 6:07 pm 
Offline

Joined: Thu Feb 09, 2012 7:29 am
Posts: 1177
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)" ???

Quote:
http://manual.audacityteam.org/man/spectrogram_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.


Top
 Profile  
 
 Post subject: Re: Convert wav to flac
PostPosted: Fri Jun 02, 2017 6:31 am 
Offline

Joined: Thu Dec 31, 2015 12:33 pm
Posts: 11
Thanks for finding a use for the images! There had to be more than reassuring "looks same" :)


Top
 Profile  
 
 Post subject: Re: Convert wav to flac
PostPosted: Sat Jun 17, 2017 3:05 pm 
Offline

Joined: Thu Dec 31, 2015 12:33 pm
Posts: 11
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.

Quote:

[email protected]:~
$ dest=/media/remDrv/Flac/"$(pwd | awk -F / '{ f=NF; print $f; }')"
[email protected]:~
$ echo $dest
/media/remDrv/Flac/Stripey
[email protected]:~
$ dest="$(pwd | awk -F / '{ f=NF; print $f; }')"
[email protected]:~
$ 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:

# 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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred