Skip to content

Instantly share code, notes, and snippets.

@right-mouse
Last active April 14, 2021 01:58
Show Gist options
  • Save right-mouse/5af8c495ddd0f5c6e96d63c094ef4dae to your computer and use it in GitHub Desktop.
Save right-mouse/5af8c495ddd0f5c6e96d63c094ef4dae to your computer and use it in GitHub Desktop.
List all mp3s in a folder with title, artist, track and album metadata using ffmpeg
#!/usr/bin/env bash
set -e
dir="$(pwd)"
if [ ! -z "$1" ]; then
dir="$1"
fi
allSizes=()
allNames=()
allTitles=()
allArtists=()
allTracks=()
allAlbums=()
for file in "${dir}"/*.mp3; do
fileName=$(basename -- "$file")
ffProps=$(ffprobe "$file" 2>&1)
title=$(echo "$ffProps" | sed -E -n 's/^ *title *: (.*)/\1/p')
artist=$(echo "$ffProps" | sed -E -n 's/^ *artist *: (.*)/\1/p')
track=$(echo "$ffProps" | sed -E -n 's/^ *track *: (.*)/\1/p')
album=$(echo "$ffProps" | sed -E -n 's/^ *album *: (.*)/\1/p')
allSizes+=( $(stat -c%s "$file" | numfmt --to=iec) )
allNames+=( "${fileName}" )
allTitles+=( "${title}" )
allArtists+=( "${artist}" )
allTracks+=( "${track}" )
allAlbums+=( "${album}" )
done
colWidthSize=4
colWidthName=4
colWidthTitle=5
colWidthArtist=6
colWidthTrack=5
colWidthAlbum=5
for idx in ${!allNames[@]}; do
size="${allSizes[idx]}"
name="${allNames[idx]}"
title="${allTitles[idx]}"
artist="${allArtists[idx]}"
track="${allTracks[idx]}"
album="${allAlbums[idx]}"
if [ ${#size} -gt $colWidthSize ]; then
colWidthSize=${#size}
fi
if [ ${#name} -gt $colWidthName ]; then
colWidthName=${#name}
fi
if [ ${#title} -gt $colWidthTitle ]; then
colWidthTitle=${#title}
fi
if [ ${#artist} -gt $colWidthArtist ]; then
colWidthArtist=${#artist}
fi
if [ ${#track} -gt $colWidthTrack ]; then
colWidthTrack=${#track}
fi
if [ ${#album} -gt $colWidthAlbum ]; then
colWidthAlbum=${#album}
fi
done
printf "%${colWidthSize}s %-${colWidthName}s %-${colWidthTitle}s %-${colWidthArtist}s %-${colWidthTrack}s %-${colWidthAlbum}s\n" \
"Size" \
"Name" \
"Title" \
"Artist" \
"Track" \
"Album"
for idx in ${!allNames[@]}; do
printf "%${colWidthSize}s %-${colWidthName}s %-${colWidthTitle}s %-${colWidthArtist}s %-${colWidthTrack}s %-${colWidthAlbum}s\n" \
"${allSizes[idx]}" \
"${allNames[idx]}" \
"${allTitles[idx]}" \
"${allArtists[idx]}" \
"${allTracks[idx]}" \
"${allAlbums[idx]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment