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.
"${x%.*}.wav"
? – Philippos May 24 '22 at 07:46${x:r}
and that in zsh just expanding$1
wouldn't by default take it as a glob. But you edited that now. – ilkkachu May 24 '22 at 07:52${x.r}
also has the same error, which I am guessing is because I wrap that in a string as the file names contain spaces"${x.r}"
. Perhaps I need to generate the output filename separately... – practicalli-john May 24 '22 at 08:05#!/usr/bin/zsh
? If not, it will be interpreted bysh
, notzsh
. In this case, the shell will complain about zsh-only syntax. – Bodo May 24 '22 at 08:16"${x%.*}.wav"
work? – Philippos May 24 '22 at 08:16Bad substitution
with the B capitalized looks like an error from Dash, Debian/Ubuntu's/bin/sh
. Zsh would givebad substitution
all in lowercase (and Bash would print the expansion in the error message too, and Busybox would also say "syntax error") – ilkkachu May 24 '22 at 08:17#!/usr/bin/zsh
was the part that was missing, thanks Bodo. Yes, Philippos, the posix approach works too. Thanks all. – practicalli-john May 24 '22 at 08:20