I created a script to convert my FLAC music collection to MP3. I did this so that I can play the MP3 files in my car, as the integrated head unit only supports MP3 and WMA files. The script works on my Ubuntu system. Are there any opportunities to improve its efficiency?
I chose to use ffmpeg
to transcode and GNU parallel to utilize all CPU cores/threads.
Iteration 1
#!/bin/bash
## Converts flac to mp3
if ! dpkg-query -W -f='${Status}' ffmpeg | grep "ok installed"; then
apt-get update && apt-get install ffmpeg; fi
if ! dpkg-query -W -f='${Status}' parallel | grep "ok installed"; then
apt-get update && apt-get install parallel; fi
parallel -i -j$(nproc) \
ffmpeg -n -i {} -acodec libmp3lame -ab 320k -map_metadata 0 -id3v2_version 3 {.}.mp3 ::: */*/*/*.flac
Iteration 2
#!/bin/sh
## Converts flac to mp3
die() { echo "$@" >&2; exit 1; }
### Check the preconditions
if ! dpkg-query -W -f='${Status}' ffmpeg | grep "ok installed"; then
apt-get update && apt-get install ffmpeg libmp3lame parallel;
fi
####
flac_to_mp3() {
ffmpeg -n -i "$1" -acodec libmp3lame -ab 320k \
-map_metadata 0 -id3v2_version 3 "$2"
}
export -f flac_to_mp3
parallel flac_to_mp3 {} '{.}.mp3' ::: */*/*/*.flac
-i -j$(nproc)
is default and thus not needed. \$\endgroup\$