Skip to content

Instantly share code, notes, and snippets.

@xse
Last active March 3, 2019 04:07
Show Gist options
  • Save xse/a5ff3ec0ef4bccb454f2a6a9329e2653 to your computer and use it in GitHub Desktop.
Save xse/a5ff3ec0ef4bccb454f2a6a9329e2653 to your computer and use it in GitHub Desktop.
cygwin version flac2ygg
#!/usr/bin/env bash
# requirements: bc, openssl, ffprobe, curl, sox, imagemagick, mktorrent
#set -xe
#TODO:
# -stop using opennsl
# -check imgur status
# -error checking in general
# -worth using ? VA_details=$(IFS=", " ; echo "${TAGS_ARTIST[*]}")
ANNOUNCE_URL="http://server.yggtracker.net:8080/123456789/announce"
# ou mettre .bbcode, .nfo et .torrent
OUTPUT_DIR="/cygdrive/c/Users/xse/Desktop/output"
is_in_array () {
local array="$1[@]"
local seeking=$2
local in=1
for element in "${!array}"; do
if [[ $element == $seeking ]]; then
in=0
break
fi
done
return $in
}
get_tags () {
local tmp_ttpath=${1#"$TARGET"}
sox "$1" -n spectrogram -t "$tmp_ttpath" -o "$TMPDIR/sox/$(basename "$1").png"
while read -r line; do
case "$line" in
*0.sample_rate=* )
local sr=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_SAMPLE_RATE "$sr" || TAGS_SAMPLE_RATE+=("$sr")
;;
*0.channels=* )
local ch=${line##*=}
is_in_array TAGS_CHANNELS "$ch" || TAGS_CHANNELS+=("$ch")
;;
*0.channel_layout=* )
local chl=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_CHANNEL_LAYOUT "$chl" || TAGS_CHANNEL_LAYOUT+=("$chl")
;;
*0.bits_per_raw_sample=* )
local bprs=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_BITS_PER_SAMPLE "$bprs" || TAGS_BITS_PER_SAMPLE+=("$bprs")
;;
format.duration=* )
local dr=$(echo $line | cut -d '"' -f2)
local dhh=$(bc <<< "${dr}/3600")
local dmm=$(bc <<< "(${dr}%3600)/60")
local dss=$(bc <<< "${dr}%60")
# seconds truncated
dss=${dss%.*}
local str_t=""
[[ $dhh -eq 0 ]] || str_t+="$dhh:"
[[ $dmm -eq 0 ]] || str_t+="$dmm:"
str_t+="$dss"
tmp_ttpath+=" [$str_t]"
is_in_array TAGS_DURATIONS "$dr" || TAGS_DURATIONS+=("$dr")
;;
format.bit_rate=* )
local br=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_BITRATE "$br" || TAGS_BITRATE+=("$br")
;;
format.tags.ARTIST=* )
local a=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_ARTIST "$a" || TAGS_ARTIST+=("$a")
;;
format.tags.ALBUM=* )
local ab=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_ALBUM "$ab" || TAGS_ALBUM+=("$ab")
;;
format.tags.GENRE=* )
local gr=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_GENRE "$gr" || TAGS_GENRE+=("$gr")
;;
format.tags.DATE=* )
local y=$(echo $line | cut -d '"' -f2)
is_in_array TAGS_DATE "$y" || TAGS_DATE+=("$y")
;;
*)
;;
esac
done <<< $(ffprobe -v quiet -print_format flat -show_entries stream=sample_rate,bits_per_raw_sample,channels,channel_layout:format=format_name,duration,bit_rate:format_tags=title,ARTIST,ALBUM,genre,date,isrc,ALBUMARTIST "$WINPATH")
TRACKLIST+=("$tmp_ttpath")
}
[[ -d $1 ]] || { echo "$1 error: not a directory"; exit 1; }
TARGET="$1"
[[ "$TARGET" = /* ]] || TARGET=$(pwd)/$TARGET
TMPDIR=/tmp/$(echo "$1" | openssl dgst -sha1 -binary | xxd -p)
[ -d "$TMPDIR" ] && rm -rf $TMPDIR
mkdir $TMPDIR
mkdir $TMPDIR/img
mkdir $TMPDIR/sox
cd $TMPDIR
ARTIST=""
ALBUM=""
YEAR=""
NUMBER_OF_TRACKS=0
NUMBER_OF_FILES=$(find "$TARGET" -type f | wc -l)
SOURCE=
SIZE=$(du -m "$TARGET" | tail -1 | awk '{print $1;}')
CODEC="flac"
BITRATE=
SAMPLE_RATE=
BITS_PER_SAMPLE=
TRACKLIST=()
IMAGE=
SOX_MONTAGE=
SOX_THUMBNAIL=
TAGS_SAMPLE_RATE=()
TAGS_CHANNELS=()
TAGS_CHANNEL_LAYOUT=()
TAGS_BITS_PER_SAMPLE=()
TAGS_DURATIONS=()
TAGS_BITRATE=()
TAGS_ARTIST=()
TAGS_ALBUM=()
TAGS_GENRE=()
TAGS_DATE=()
while read -r stuff; do
WINPATH=$(cygpath -w "$stuff")
case "$stuff" in
*.flac )
NUMBER_OF_TRACKS=$((NUMBER_OF_TRACKS+1))
get_tags "$stuff"
;;
*.png|*.jpg|*.jpeg|*.gif|*.apng|*.tiff )
cp "$stuff" "$TMPDIR/img/$(basename "$stuff")"
;;
*)
;;
esac
done <<< $(find "$TARGET" | sort)
str_is_included() {
reqsubstr="$1"
shift
string="$@"
if [ -z "${string##*$reqsubstr*}" ] ;then
return 0
else
return 1
fi
}
function join_by { local IFS="$1"; shift; echo "$*"; }
do_artist() {
local status=
local ok_artist=()
local input=""
local j=0
# plusieurs artistes, est-ce que l'un d'entre eux est dans le nom du fichier ?
for i in "${TAGS_ARTIST[@]}"
do
str_is_included "$i" "$(basename "$TARGET")"
status=$?
if $(exit $status)
then
is_in_array ok_artist "$i" || ok_artist+=("$i")
is_in_array ok_artist "Various Artists" || ok_artist+=("Various Artists")
fi
done
# more than one artist in basename or none at all.
if [ ${#ok_artist[@]} -gt 1 ] || [ ${#ok_artist[@]} -eq 0 ]; then
ok_artist+=("Various Artists")
echo "Unsure about ARTIST:"
for i in "${ok_artist[@]}"; do
echo " $j: $i"
j=$((j+1))
done
read -p "Select or input artist/group name: (ENTER if only one correct choice) " input
if [[ "$input" =~ ^[0-9]+$ ]] && ((input >= 0 && input <= ${#ok_artist[@]})) ; then
ARTIST=${ok_artist[$input]}
elif [[ -z "$input" ]]; then
ARTIST="${ok_artist[0]}"
else
ARTIST="$input"
fi
else
ARTIST="${ok_artist[0]}"
fi
}
do_album() {
if [ ${#TAGS_ALBUM[@]} -eq 1 ]; then
ALBUM="${TAGS_ALBUM[0]}"
else
echo "Multiple album tags found:"
printf ' -%s\n' "${TAGS_ALBUM[@]}"
read -p "Enter album title: " ALBUM
fi
}
do_year() {
if [ ${#TAGS_DATE[@]} -eq 1 ]; then
YEAR="${TAGS_DATE[0]}"
else
echo "Multiple date tags found:"
printf ' -%s\n' "${TAGS_DATE[@]}"
read -p "Enter YEAR: " YEAR
fi
}
do_source() {
read -p "Source: " SOURCE
}
do_bitrate() {
local sum=0
IFS='+' sum=$(echo "scale=1;${TAGS_BITRATE[*]}" | bc)
BITRATE=$(echo "($sum / ${#TAGS_BITRATE[@]}) / 1000" | bc)
BITRATE+="kpbs"
}
do_sample_rate() {
if [ ${#TAGS_SAMPLE_RATE[@]} -eq 1 ]; then
SAMPLE_RATE="${TAGS_SAMPLE_RATE[0]}"
else
echo "Multiple sample_rate found:"
printf ' -%s\n' "${TAGS_SAMPLE_RATE[@]}"
read -p "Enter sample rate: " SAMPLE_RATE
fi
}
do_bits_per_sample() {
if [ ${#TAGS_BITS_PER_SAMPLE[@]} -eq 1 ]; then
BITS_PER_SAMPLE="${TAGS_BITS_PER_SAMPLE[0]}"
else
echo "Multiple bits_per_raw_sample tags found:"
printf ' -%s\n' "${TAGS_BITS_PER_SAMPLE[@]}"
read -p "Enter bits per sample: " BITS_PER_SAMPLE
fi
}
do_images() {
local imgurl
if find "$TMPDIR/img/" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
local nbrimg=$(ls -1q $TMPDIR/img/ | wc -l)
if [ $nbrimg -eq 1 ]; then
local fimg=$(find $TMPDIR/img/ -mindepth 1 -print -quit)
local filename=$(basename -- "$fimg")
local extension="${filename##*.}"
convert "$fimg" -resize 600x800 -quality 100 "$TMPDIR/prez.$extension"
result=$(curl -s -H "Authorization: Client-ID c9a6efb3d7932fd" -H "Expect: " -F "image=@prez.$extension" https://api.imgur.com/3/image.xml)
url="${result##*<link>}"
url="${url%%</link>*}"
IMAGE="$url"
else
montage -mode concatenate -background black -tile 2x "$TMPDIR/img/*" "$TMPDIR/prez.png"
convert "$TMPDIR/prez.png" -resize 600x800\> -quality 100 "$TMPDIR/prez.png"
result=$(curl -s -H "Authorization: Client-ID c9a6efb3d7932fd" -H "Expect: " -F "image=@prez.png" https://api.imgur.com/3/image.xml)
url="${result##*<link>}"
url="${url%%</link>*}"
IMAGE="$url"
fi
else
echo "'$TMPDIR/img/' is empty"
read -p "No images, enter image url: " imgurl
cd img
curl -O "$imgurl"
cd ..
local fimg=$(find $TMPDIR/img/ -mindepth 1 -print -quit)
local filename=$(basename -- "$fimg")
local extension="${filename##*.}"
convert "$fimg" -resize 600x800 -quality 100 "$TMPDIR/prez.$extension"
result=$(curl -s -H "Authorization: Client-ID c9a6efb3d7932fd" -H "Expect: " -F "image=@prez.$extension" https://api.imgur.com/3/image.xml)
url="${result##*<link>}"
url="${url%%</link>*}"
IMAGE="$url"
fi
montage -mode concatenate -background black -tile 4x "$TMPDIR/sox/*.png" "$TMPDIR/montage.png"
convert "$TMPDIR/montage.png" -resize 200x200\> -quality 100 "$TMPDIR/thumbnail.png"
result=$(curl -s -H "Authorization: Client-ID c9a6efb3d7932fd" -H "Expect: " -F "image=@montage.png" https://api.imgur.com/3/image.xml)
url="${result##*<link>}"
url="${url%%</link>*}"
SOX_MONTAGE="$url"
result=$(curl -s -H "Authorization: Client-ID c9a6efb3d7932fd" -H "Expect: " -F "image=@thumbnail.png" https://api.imgur.com/3/image.xml)
url="${result##*<link>}"
url="${url%%</link>*}"
SOX_THUMBNAIL="$url"
}
# NFO:
#Général : artiste, album, Année, Nombres de Piste, Source, Poids
#Audio : Codec Audio, Bitrate, Fréquence, Échantillonnage bits (FLAC/ALAC/WAV/WV seulement).
#Tracklist : Liste des pistes.
NFO() {
printf '♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫ GENERAL ♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫\n' > "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'ARTIST: %s\n' "$ARTIST" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'ALBUM: %s\n' "$ALBUM" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'YEAR: %s\n' "$YEAR" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'TRACKS: %s\n' "$NUMBER_OF_TRACKS" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'SOURCE: %s\n' "$SOURCE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'SIZE: %s MB\n\n' "$SIZE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf '♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪ AUDIO ♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'CODEC: %s\n' "$CODEC" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'BITRATE: %s\n' "$BITRATE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'SAMPLE_RATE: %s HZ\n' "$SAMPLE_RATE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf 'BITS_PER_SAMPLE: %s Bits\n\n' "$BITS_PER_SAMPLE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf '♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪ TRACKLIST ♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫♪♫\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
printf ' %s\n' "${TRACKLIST[@]}" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.nfo"
}
# PREZ
#Une image (Obligatoire) : Une couverture de l'album, ou de l'artiste.
#Les infos du sujet (Obligatoire) : Artiste / Groupe, nom d'album et année de sortie.
#Les infos de l'upload (Obligatoire) : Nombre de pistes, liste des pistes (ou liste des albums si discographie), le codec audio, le bitrate, l’échantillonnage et les bits pour les FLAC.
PREZ() {
printf '[center][size=150][b]%s - %s\n(%s-%s) [%s %sHZ / %sBITS][/b][/size]\n' "$ARTIST" "$ALBUM" "$YEAR" "$SOURCE" "$CODEC" "$SAMPLE_RATE" "$BITS_PER_SAMPLE" > "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[img]%s[/img]\n' "$IMAGE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '\n[b]Artiste:[/b] %s\n' "$ARTIST" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Album:[/b] %s\n' "$ALBUM" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Année:[/b] %s\n' "$YEAR" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Nombre de pistes:[/b] %s\n' "$NUMBER_OF_TRACKS" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Nombre de fichiers:[/b] %s\n' "$NUMBER_OF_FILES" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Poids Total:[/b] %s MB\n' "$SIZE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Source:[/b] %s\n' "$SOURCE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Codec:[/b] %s\n' "$CODEC" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Bitrate:[/b] %s\n' "$BITRATE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Sample rate:[/b] %s\n' "$SAMPLE_RATE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[b]Bits per sample:[/b] %s\n\n' "$BITS_PER_SAMPLE" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '\n[size=150][b]Tracklist:[/b][/size]\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '%s\n' "${TRACKLIST[@]}" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '\n[size=150][b]Spectrogrames:[/b][/size]\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[url=%s][img]%s[/img][/url]\n' "$SOX_MONTAGE" "$SOX_THUMBNAIL" >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
printf '[/center]\n' >> "$OUTPUT_DIR/$ARTIST-$ALBUM.bbcode"
}
TORRENT() {
if [ "$SIZE" -le 50 ] ; then
PIECE_SIZE=15
elif [ "$SIZE" -ge 50 ] && [ "$SIZE" -le 150 ]; then
PIECE_SIZE=16
elif [ "$SIZE" -ge 150 ] && [ "$SIZE" -le 350 ]; then
PIECE_SIZE=17
elif [ "$SIZE" -ge 350 ] && [ "$SIZE" -le 512 ]; then
PIECE_SIZE=18
elif [ "$SIZE" -ge 512 ] && [ "$SIZE" -le 1024 ]; then
PIECE_SIZE=19
elif [ "$SIZE" -ge 1024 ] && [ "$SIZE" -le 2048 ]; then
PIECE_SIZE=20
else
PIECE_SIZE=21
fi
/home/xse/mktorrent/mktorrent.exe -l "$PIECE_SIZE" -p -a "$ANNOUNCE_URL" "$TARGET" -o "$OUTPUT_DIR/$ARTIST-$ALBUM.torrent"
}
do_artist
do_album
do_year
do_source
do_bitrate
do_sample_rate
do_bits_per_sample
do_images
NFO
PREZ
TORRENT
rm -rf $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment