Sample Header Ad - 728x90

ZSh script to batch process filenames containing spaces and substitute extension

0 votes
2 answers
883 views
Is there a zsh script that can convert a directory files in one music format and be converted into Ogg opus format. The *filenames have spaces in their names*. For example, a directory contains 10 files with *.wma extensions Files are converted into *.wav format using ffmpeg -i filename.wma filename.wav The *.wav files are converted to opus using opusenc --bitrate 160 filename.wav filename.opus *Update*: ffmpeg -i filename.wma -c:a libopus -b:a 128k filename.opus converts the file with one command A partially working script will process filenames in current directory from .wma to .wav, even with spaces. However, the .wav extension is added rather than replacing the *.wma file extension This script was added to a file called convert and that file made executable
IFS=$'\n'
for x in *.wma ; do
  echo $x
  ffmpeg -i "$x" $x.wav
done
Trying to use Zsh modifiers to substitute the filename extension with: ${x:e}".wav (and after a suggestion by ilkkachu I also tried ${x:r}".wav)
IFS=$'\n'
for x in *.wma ; do
  ffmpeg -i "$x" "${x:e}".wav
done
Calling this from a file called convert, the following error is returned
./convert: 3: Bad substitution
The same error happens with
IFS=$'\n'
for x in *.wma ; do
  ffmpeg -i "$x" "${x:r}".wav
done
I assume the syntax is not quite write or modifiers do not work when filenames have spaces. Or I still have a lot to learn about zsh :) Is there a correct way to substitute a filename extension in Zsh (when file names contain spaces) Thank you.
Asked by practicalli-johnny (101 rep)
May 24, 2022, 07:39 AM
Last activity: May 25, 2022, 05:17 AM