Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Bash] sox convert for loop

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
sunrat
Administrator
Administrator
Posts: 6475
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

[Bash] sox convert for loop

#1 Post by sunrat »

I have a for loop to batch resample .wav files. convert to flac, normalize, and move to a new directory.
Command is:

Code: Select all

mkdir 16bit && for file in *.wav; do sox -SG "$file" -b 16 16bit/"$file".flac norm -1.0; done
This works fine except the resulting files have suffix ".wav.flac"
How can I avoid the superfluous ".wav"?
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1418
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 79 times
Been thanked: 191 times

Re: [Bash] sox convert for loop

#2 Post by steve_v »

I'm too lazy to test with your exact case, but I'd probably just use a simple bash string manipulation here, e.g. something like
Substring replacement:

Code: Select all

mkdir 16bit && for file in *.wav; do sox -SG "$file" -b 16 16bit/"${file/.wav/.flac}" norm -1.0; done
That will replace the first occurrence of ".wav" in $file so it'll be a little fragile, but close enough for government work assuming your input filenames aren't too funky.

Or better, substring removal:

Code: Select all

mkdir 16bit && for file in *.wav; do sox -SG "$file" -b 16 16bit/"${file%.wav}.flac" norm -1.0; done
That'll cut the shortest match for ".wav" off the end of $file, so it should be more resilient.

I find the ${var%string} construct (and it's front-cut counterpart ${var#string}) extremely handy for separating filenames from extensions in general.


Many ways to skin that one, how many cats do you have? :P
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

User avatar
sunrat
Administrator
Administrator
Posts: 6475
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: [Bash] sox convert for loop

#3 Post by sunrat »

The second one works a treat. I was thinking along those lines but wasn't sure of exact syntax. Thank you @steve_v !

My cat left for cat heaven several years ago but I'm sure she would have been happy about that. :)
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1418
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 79 times
Been thanked: 191 times

Re: [Bash] sox convert for loop

#4 Post by steve_v »

Bash sure is a screwey language, but it's surprising what it can do natively. :)

Aside, I find iterating over filenames in for loops to be a real PITA, particularly when they contain whitespace. In your case it's probably fine, but there are other, more space-tolerant and less variable-quoting sensitive ways to loop over arbitrary filenames.
Traditionally one might use a while | read loop or the -exec function of find, but now that bash has arrays... IMHO bash arrays are pretty mint for dealing with filenames.

Just a thought, if you too have the tendency to succumb to script-OCD ;)
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

User avatar
kent_dorfman766
Posts: 540
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 59 times
Been thanked: 70 times

Re: [Bash] sox convert for loop

#5 Post by kent_dorfman766 »

flacfile=${file/.wav/.flac}

Should do it...but I see steve beat me to the punch. You may be able to anchor the search to the right end of the string using "$", or that may just work in PERL. cannot remember. I usually use the above instead of the % form he mentioned.

User avatar
sunrat
Administrator
Administrator
Posts: 6475
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: [Bash] sox convert for loop

#6 Post by sunrat »

I've used a similar for loop to convert sample rate/bit depth many times but have never used it to convert formats before and this was a quest to find how to do it all in one. sox is great for lots of things and it makes it easy that the specified output file just needs to have the target format suffix and it converts. Spaces are handled, no problem. sox also dithers by default when bit depth is lowered which was a factor in using it rather than ffmpeg.
Achievement unlocked! Thanks again.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
kent_dorfman766
Posts: 540
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 59 times
Been thanked: 70 times

Re: [Bash] sox convert for loop

#7 Post by kent_dorfman766 »

sox kicks a$$! if you need a low level swiss army knife there is nothing better, IMHO...

I am very partial to ecasound because of the GUI to visualize the waveforms though, and I don't have to remember sox command line params.

User avatar
sunrat
Administrator
Administrator
Posts: 6475
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: [Bash] sox convert for loop

#8 Post by sunrat »

kent_dorfman766 wrote: 2023-04-02 22:56 sox kicks a$$! if you need a low level swiss army knife there is nothing better, IMHO...

I am very partial to ecasound because of the GUI to visualize the waveforms though, and I don't have to remember sox command line params.
ecasound is cli. What GUI do you use?
sox can do spectrograms:

Code: Select all

sox <file> -n spectrogram
I used to use Spek which was great except it was left behind in Bullseye with library upgrades but it's coming back for Bookworm.

Mostly for recording I use Harrison Mixbus 32C, Ardour, or Qtractor. For edits and vinyl rips mainly Audacity. But sox is best for samplerate conversions and batch processing.
Unfortunately Linux still doesn't have a comprehensive audio restoration program. Audacity can do a bit, but usually I use Izotope RX in Windows which still astounds me in quality particularly for the vinyl to digital conversion project I'm doing now.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
kent_dorfman766
Posts: 540
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 59 times
Been thanked: 70 times

Re: [Bash] sox convert for loop

#9 Post by kent_dorfman766 »

ecasound is cli. What GUI do you use?
brain says one thing and fingers say another. I meant audacity, but since I pull it from an X11 pulldown menu, I'm like "dangit...what's it called this week?"

Post Reply