Log in

View Full Version : How to batch convert audio files in a portable way?


stax76
18th May 2019, 01:49
My long term goal is to learn how to do things the command line and portable way and maybe try Linux and MacOS in the future.

How would you batch convert audio files?

Here is my solution in PowerShell and only tested in Windows:

$inputFolder = "C:\Users\frank\Daten\Music Temp"
$outputFolder = "C:\Users\frank\Desktop\mp3 files"

Get-ChildItem $inputFolder |
Foreach-Object {
$targetpath = $outputFolder + "\" + $_.BaseName + ".mp3"
ffmpeg -i $_.FullName -b:a 320k $targetpath
}

GUI is fine as long as it is portable.

LoRd_MuldeR
18th May 2019, 11:41
Well, FFmpeg already is portable, so is this mostly a question about the scripting?

If so, I think shell script (e.g. Bash) is the way to go:

readonly input_folder="/home/johndoe/Music"
readonly output_folder="/home/johndoe/Desktop"

for source_path in "$input_folder/"*; do
target_path="$output_folder/$(basename -s .wav "$source_path").mp3"
ffmpeg -i "$source_path" -b:a 320k "$target_path"
done


This should work on any Unix and Unix-like OS (e.g. Linux or macOS ) out of the box. On Windows, you can use MSYS/MinGW (https://www.msys2.org/) or Cygwin, for example (I recommend giving MobaXterm (https://mobaxterm.mobatek.net/) a try to get started).

Could even use BusyBox for Win32 (https://frippery.org/busybox/) as a minimal Unix shell on Windows. And, of course, Microsoft's PowerShell is more or less "portable" now, as well...

qyot27
18th May 2019, 13:17
Another Bash example. This is the script I use to process my audio CD backups:
#!/bin/bash -x
set -e
cd FLAC
for n in *.wav ; do
flac --no-preserve-modtime -V -8 "$n"
done

cd ..
for n in *.wav ; do
qaac --abr 192 --no-smart-padding --rate keep -o "MP4/${n%.*}.m4a" "$n"
done

for n in *.wav ; do
opusenc --bitrate 192 --comp 10 --framesize 60 --ignorelength "$n" "Opus/${n%.*}.opus"
done

Not that I'm saying anything for/against using the format tools, it was more about the shell substitution and regex abilities ( ${n%.*} ).