113 lines
2.4 KiB
Bash
Executable File
113 lines
2.4 KiB
Bash
Executable File
#!/bin/sh
|
|
ending() {
|
|
echo "$1" |awk -F . '{print $NF}'
|
|
}
|
|
|
|
checkMetadata() {
|
|
ffprobe "$1" 2>&1 | grep -ie artist -e title
|
|
while true; do
|
|
printf "correct metadata? [Y] | [n]: "
|
|
read -r yn < /dev/tty
|
|
case "$yn" in
|
|
[Nn]* ) return 1;;
|
|
* ) return 0;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
download_dir="$(mktemp -d)"
|
|
temp_dir="$(mktemp -d)"
|
|
downloadSong() {
|
|
filepath="$(youtube-dl "ytsearch:$1 lyrics" \
|
|
-f bestaudio/best \
|
|
--output "$download_dir/%(title)s.%(ext)s" \
|
|
--metadata-from-title "%(artist)s - %(title)s" \
|
|
--add-metadata \
|
|
|awk '$1 == "[download]" && $2 == "Destination:" {print substr($0, index($0,$3))}')"
|
|
case "$filepath" in
|
|
*.webm)
|
|
new_filename="${filepath%.*}".opus
|
|
ffmpeg -i "$filepath" -codec copy "$new_filename" 2> /dev/null
|
|
filepath="$new_filename"
|
|
;;
|
|
*.m4a)
|
|
;;
|
|
*)
|
|
echo "unsupported file type" > /dev/stderr
|
|
exit 1
|
|
;;
|
|
esac
|
|
r128gain "$filepath" 2> /dev/null
|
|
echo "$filepath"
|
|
}
|
|
|
|
changeMetadata() {
|
|
filepath="$1"
|
|
artist="$2"
|
|
title="$3"
|
|
dir="$4"
|
|
case "$filepath" in
|
|
*.opus)
|
|
metadata_track=":s:a:0"
|
|
title_tag="TITLE"
|
|
artist_tag="ARTIST"
|
|
;;
|
|
*.m4a)
|
|
title_tag="title"
|
|
artist_tag="artist"
|
|
;;
|
|
esac
|
|
metadata="-metadata$metadata_track"
|
|
output_path="$dir/$artist-$title.$(ending "$filepath")"
|
|
ffmpeg -i "$filepath" -codec copy "$metadata" "$title_tag=$title" "$metadata" \
|
|
"$artist_tag=$artist" "$output_path" 2> /dev/null
|
|
echo "$output_path"
|
|
}
|
|
|
|
while getopts "a:t:mp" c; do
|
|
case $c in
|
|
a) artist="$OPTARG";;
|
|
t) title="$OPTARG";;
|
|
m)
|
|
dir="$MUSIC_DIR/$(date +%Y)"
|
|
mkdir -p "$dir"
|
|
;;
|
|
p) print_path=0;;
|
|
*) echo "invalid flag" > /dev/stderr;;
|
|
esac
|
|
done
|
|
shift $(("$OPTIND" - 1))
|
|
if [ ! "$dir" ]; then
|
|
dir="$(pwd)"
|
|
fi
|
|
if [ "$artist" ] && [ "$title" ]; then
|
|
echo 1
|
|
filepath="$(downloadSong "$artist" "$title")"
|
|
output_path="$(changeMetadata "$filepath" "$artist" "$title" "$dir")"
|
|
if [ "$print_path" ]; then
|
|
echo "$output_path"
|
|
fi
|
|
else
|
|
for song in "$@"; do
|
|
filepath="$(downloadSong "$song")"
|
|
filename="$(basename "$filepath")"
|
|
if checkMetadata "$filepath"; then
|
|
mv "$filepath" "$dir"
|
|
if [ "$print_path" ]; then
|
|
echo "$dir/$filename"
|
|
fi
|
|
else
|
|
printf "artist: "
|
|
read -r artist
|
|
printf "title: "
|
|
read -r title
|
|
output_path="$(changeMetadata "$filepath" "$artist" "$title" "$dir")"
|
|
if [ "$print_path" ]; then
|
|
echo "$output_path"
|
|
fi
|
|
fi
|
|
done
|
|
fi
|
|
rm -rf "$temp_dir" "$download_dir"
|
|
mpc update > /dev/null
|