Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active April 16, 2019 19:31
Show Gist options
  • Select an option

  • Save jfeilbach/410cd90e9131b955a6f5da897bb05ab1 to your computer and use it in GitHub Desktop.

Select an option

Save jfeilbach/410cd90e9131b955a6f5da897bb05ab1 to your computer and use it in GitHub Desktop.
add cover art to mp3 files (a reality sometimes)
#!/bin/bash
# This will add cover art to all *.mp3 in a directory without transcoding the input file
# requires ffmpeg
# JPEGs will be converted into PNGs
SECONDS=0
COVER=${1}
CMD='ffmpeg'
COUNT=$(ls *.mp3 | wc -l)
COUNTER='1'
NC='\e[0m'
WHITE='\e[1;37m'
YELLOW='\e[1;33m'
LIGHT_BLUE='\e[1;34m'
CYAN='\e[0;36m'
RED='\e[0;31m'
if [ "$1" == "" ]; then
echo -e "Please enter the path to the cover art. Exiting.\n"
exit 1
fi
if [[ -e "${COVER}" ]]; then
echo ""
echo -e "Cover art file found. Proceeding...\n"
else
echo -e "${RED}Cover art file ${WHITE}${COVER}${NC} not found. Exiting.${NC}\n"
exit 2
fi
if ! [ -x "$(command -v ${CMD})" ]; then
echo "${RED}Error: ${CMD} is not installed. Please install ${CMD}. Exiting.${NC}" >&2
exit 3
else
echo -e "${CMD} found. Proceeding...\n"
fi
if [ "$COUNT" == "0" ]; then
echo -e "${RED}No .mp3 files found. Exiting.${NC}\n"
exit 4
fi
displaytime () {
local T=$SECONDS
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
[[ $D > 0 ]] && printf '%d days ' $D
[[ $H > 0 ]] && printf '%d hours ' $H
[[ $M > 0 ]] && printf '%d minutes ' $M
[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and '
printf '%d seconds\n' $S
}
echo -e "Adding ${WHITE}${COVER}${NC} to ${WHITE}${COUNT}${NC} .mp3 audio files...\n"
for f in *.mp3 ; do
echo ""
echo -e "Updating file ${WHITE}\"${f}\"${NC} ... ${COUNTER}/${COUNT}\n"
ffmpeg -i "${f}" \
-i "${COVER}" \
-map 0:0 \
-map 1:0 \
-c copy \
-id3v2_version 3 \
-metadata:s:v title="Album cover" \
-metadata:s:v comment="Cover (front)" \
"new-${f}" \
-hide_banner
-loglevel panic # decrease stdout verbosity
(( COUNTER++ ))
done
#read -n 1 -s -r -p "Press any key to continue"
echo ""
echo -e "Deleting original files...\n"
for name in "./!(new-*.mp3)" ; do
echo -e "Deleting ${name}...\n"
[ -f "$name" ] && rm -v "$name"
done
echo -e "Renaming new files back to original file names...\n"
for n in new-*.mp3 ; do
echo ""
# echo -e "Renaming ${n}...\n"
mv -v "$n" "$(echo "$n" | sed s/new-//)"
done
TIME=$(displaytime)
echo ""
echo -e "Completed in ${YELLOW}${TIME}${NC}.\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment