Skip to content

Instantly share code, notes, and snippets.

@pltb
Last active May 8, 2024 15:37
Show Gist options
  • Save pltb/244899d900a1b0305467f50e32133cc3 to your computer and use it in GitHub Desktop.
Save pltb/244899d900a1b0305467f50e32133cc3 to your computer and use it in GitHub Desktop.
FLAC -> ALAC convesion script with downsampling to 16bit/44100kHz
#!/usr/bin/env bash
set -e
f2a () {
if [ -z $1 ]; then
echo "usage: $0 dir"
return 1
fi
for file in "$1"/*.flac; do
echo -n "."
ffmpeg -i "$file" -sample_fmt s16p -map 0:a -ar 44100 -acodec alac "${file%.*}.m4a"
done
}
f2a $1
@pltb
Copy link
Author

pltb commented May 8, 2024

To make the usage more convenient, I've also made it available as a shell command (I use .zsh):

  1. created a directory for shell scripts which I want to make callable from the shell
  2. put the content of the script above in a file in that directory
  3. added these two lines to my .zshrc:
fpath=($ZSH_SCRIPTS_HOME $fpath);
autoload -U $fpath[1]/*(.:t)

Then I could call it from the shell like this: f2a <directory with flacs>

A brief explanation of the ffmpeg args:

  • -map 0:a – to only include audio streams (e.g. when there's a cover stream in the source flac)
  • -sample_fmt s16p -ar 44100 – output sample rate / discretisation level adjustment

@pltb
Copy link
Author

pltb commented May 8, 2024

Was first using this script, then changed it to only use afconvert, but then decided to simply use ffmpeg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment