Skip to content

Instantly share code, notes, and snippets.

@spirillen
Last active May 6, 2020 20:21
Show Gist options
  • Save spirillen/6333f73da3fa4fbf78a9cdd0a2750b0e to your computer and use it in GitHub Desktop.
Save spirillen/6333f73da3fa4fbf78a9cdd0a2750b0e to your computer and use it in GitHub Desktop.
Convert almost any media by extension to m4a
#!/usr/bin/env bash
# This script convert flac files into ALAC m4a
# Execute it by calling this script and append full path to dir as
# flac_mp4.sh "/media/joakim/VERBATIM/Avril Lavigne/"
# you can use "${PWD}" if you which to execute this script from
# current dir
# exit on any errors
set -e #-x
_ext=(flac) # e.g. flac mp4 webm opus mp3
_basedir="${1}"
# change to basedir
printf "You are converting from ${_basedir}\n"
cd "${_basedir}"
# Find any files in subfolders and add them to the array
readarray -d '' array < <(for i in "${_ext[@]}"; do find . -type f -iname "*.$i" -print0 ; done)
for i in "${array[@]}"
do printf "converting $i\n"
#ffmpeg -i "$i" -map 0:a:0 -y -v 0 -c:v copy -c:a alac -map_chapters -1 "${i%.*}".m4a && rm -f "$i"
ffmpeg -i "$i" -map 0:a:0 -y -v 0 -c:v copy -c:a alac -map_chapters -1 "${i%.*}".m4a
done
# Some sources had chapters which broke conversion
#mkdir -p "${subdir}/chap"
#ffmpeg -i "$i" -y -v 0 -c copy -map_chapters -1 "chap/${i}"
@spirillen
Copy link
Author

spirillen commented May 6, 2020

With this little script you can convert almost any media by extension to m4a with ffmpeg.

This script have been tested on mp3, flac, webm and mp4 files.

It works by you gives it a starting directory and then it recursively converts from _ext to m4a in same folder as the source media is located.

Be AWARE the default behavior is to delete source media after successful conversion, you can change this by comment L28 and outcomment #L29

You can execute it as is with wget

bash <(wget -qO- 'https://gist.githubusercontent.com/spirillen/6333f73da3fa4fbf78a9cdd0a2750b0e/raw/a45d349178aa927eda71b85f763f6eb2d9f410f7/convert_to_m4a.sh') "${PWD}"

@spirillen
Copy link
Author

Flipped default delete behavior....

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