How do I use GNU Parallel to convert audio files from .wav format to mp3?

I’m attempting to convert a complete folder of audio files in the .wav format to .mp3 by adjusting the bitrate. The issue arises because each audio file’s name contains multiple periods and concludes with .wav. I am utilizing gnu-parallel to modify the audio signals’ bitrate and save them as .mp3. My current command line is as follows:

ls wavs | rev | cut -d ‘.’ -f 2- | rev | parallel -I% ffmpeg -i wavs/%.wav -codec:a libmp3lame -qscale:a 2 wavs_2/%.mp3

The audio file names contain more than one period, which is the cause of the issue. ‘cut’ produces an error saying “No such file or directory” since it can only handle one delimiter. ‘rev’ is used to reverse the file name in the modified command, ‘cut’ extracts all characters except the final period, and’rev’ is used once more to restore the original order. This should fix the problem that the file names with multiple periods were causing.

Need guidance on how to resolve it.

1 Like

It appears that you are encountering a typical issue when working with filenames that contain several periods. It’s assumed that the ‘cut’ issue you’re experiencing is occurring since it can only handle one delimiter.
One solution to this is to use more flexible alternative tools with numerous delimiters, such as ‘awk’ or’sed’. Have you given these possibilities any thought?

I haven’t really delved into ‘awk’ or ‘sed’ much. How would I go about using them in this context?

Okay! Let us consider awk first. You could modify your command like this:

ls wavs | awk -F. '{print $1}' | parallel -I% ffmpeg -i wavs/%.wav -codec:a libmp3lame -qscale:a 2 wavs_2/%.mp3

Here, we use ‘awk’ to split the filename at periods and extract the first field. Alternatively, with ‘sed’:


ls wavs | sed 's/\(.*\)\..*/\1/' | parallel -I% ffmpeg -i wavs/%.wav -codec:a libmp3lame -qscale:a 2 wavs_2/%.mp3

This sed command substitutes everything after the last period with an empty string. Give these a try and let me know how it goes!