Skip to content

Instantly share code, notes, and snippets.

@fabacab
Created November 18, 2022 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabacab/7df138c362f45a1b625f1cb0eca5d076 to your computer and use it in GitHub Desktop.
Save fabacab/7df138c362f45a1b625f1cb0eca5d076 to your computer and use it in GitHub Desktop.
Read an M3U playlist file exported from Apple Music and relativize the paths, copying content from the original location to the relative location.
#!/bin/bash -
#
# Given a playlist, expected in M3U format, copies
# the songs listed in said playlist to the given
# output directory, creating a new playlist using
# relative path names. This new folder can then be
# shared more easily.
playlist="$1" # Path to an M3U file.
tmpfile="$(mktemp playlist.XXXXX)"
output_dir="$2" # Where to place the new playlist.
mkdir -p "${output_dir}"
file -k "${playlist}" | grep -q "CR line terminators"
[ $? -eq 0 ] && command -v dos2unix && \
dos2unix --convmode mac --newfile "${playlist}" "${tmpfile}" || \
echo <<EOF
CR line endings detected but no conversion utility found.
If this script fails, try again after installing 'dos2unix':
port install dos2unix
or
brew install dos2unix
EOF
while read line; do
cp "$line" "${output_dir}"
sed -e '/^\// s/.*\///' "${tmpfile}" > "${output_dir}/${output_dir}.m3u"
done < <(grep -E '^/' "${tmpfile}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment