This commit is contained in:
2023-04-12 13:03:20 +02:00
parent 6294649b5a
commit 1a9a4dee6d
5 changed files with 110 additions and 63 deletions

108
md
View File

@ -1,50 +1,112 @@
#!/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
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
* ) return 0;;
esac
done
}
download_dir="$(mktemp -d)"
temp_dir="$(mktemp -d)"
for song in "$@"; do
file_name="$(youtube-dl "ytsearch:$song lyrics" \
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 "$(echo "$file_name" | awk -F . '{print $NF}')" in
"webm")
new_filename="${file_name%.*}".opus
mkvextract tracks "$file_name" "0:$new_filename" > /dev/null
file_name="$new_filename"
case "$filepath" in
*.webm)
new_filename="${filepath%.*}".opus
ffmpeg -i "$filepath" -codec copy "$new_filename" 2> /dev/null
filepath="$new_filename"
;;
"m4a")
*.m4a)
;;
*)
echo "unsupported file type"
echo "unsupported file type" > /dev/stderr
exit 1
;;
esac
r128gain "$file_name" 2> /dev/null
temp_file="$temp_dir/$(basename "$file_name")"
cp "$file_name" "$temp_file"
idntag "$temp_file"
temp_file=$(echo "$temp_dir"/*)
if checkMetadata "$temp_file"; then
mv "$temp_file" .
elif checkMetadata "$file_name"; then
mv "$file_name" .
else
echo "enter it manually"
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
done
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