#!/bin/zsh
export cachedir=~/.cache/ytplay
if [ -d $cachedir ];then
sleep .01
else
mkdir -p $cachedir
fi
if [ -z $1 ];then
echo "Enter Song Title"
read -r origterm
else
export origterm=$@
fi
export term=`echo $origterm lyrics`
if [ -f $cachedir/$origterm.ogg ];then
sleep .01
else
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
export attemptcounter=1
while true;do
if echo $url|grep -q http;then
break
else
unset term
export term=`echo $origterm`
unset url
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
fi
export attemptcounter=$(($attemptcounter+1))
if [ $attemptcounter -gt 5 ];then
echo "No results found or system error. "
exit 1
fi
done
fi
if echo $0|grep -qw noplay;then
export noplay=1
fi
if echo $0|grep -qw ytring;then
export ringcachedir=$cachedir/rings
if [ -d $ringcachedir ];then
sleep .01
else
mkdir -p $ringcachedir
fi
if [ -f $cachedir/$origterm.ogg ];then
sleep .01
else
ln -s `which $0` ./ytplay.noplay
./ytplay.noplay $origterm
rm ./ytplay.noplay
fi
while true;do
echo enter start time
read starttime
echo enter a number between 1 and 30. This will be the duration of your ringtone which starts from the start time entered in the previous prompt
read endtime
sox $cachedir/$origterm.ogg $ringcachedir/$origterm.wav trim $starttime 00:00:$endtime > /dev/null 2> /dev/null
echo convertion of $origterm to a ringtone lasting $endtime seconds from $starttime has been completed. This is how it will sound on your phone.
play -q $ringcachedir/$origterm.wav > /dev/null 2> /dev/null
echo is this ringtone ok\? enter y for yes or n for no.
read convertanswer
case "$convertanswer" in
y)
fdkaac $ringcachedir/$origterm.wav -o $ringcachedir/$origterm.m4a -m3 > /dev/null 2> /dev/null
mv $ringcachedir/$origterm.m4a $ringcachedir/ring-$origterm.m4r
rm $ringcachedir/$origterm.wav
exit 0
break
;;
n)
continue
;;
esac
done
fi
if echo $0|grep -qw stream;then
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
export attemptcounter=1
while true;do
if echo $url|grep -q http;then
break
else
unset term
export term=`echo $origterm`
unset url
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
fi
export attemptcounter=$(($attemptcounter+1))
if [ $attemptcounter -gt 5 ];then
echo "No results found or system error. "
exit 1
fi
done
while true;do
if curl -Ls $url|ffmpeg -y -i pipe:0 -f ogg pipe:1 2>/dev/null >/dev/null | play -t ogg -q -;then
break
else
unset url
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
curl -Ls $url|ffmpeg -y -i pipe:0 -f ogg pipe:1 2>/dev/null >/dev/null |play -t ogg -q -
fi
done
else
while true;do
if [ -f $cachedir/$origterm.ogg ];then
if [ -z $noplay ];then
play -q $cachedir/$origterm.ogg
else
sleep .01
fi
break
fi
if curl -Ls $url|ffmpeg -y -i pipe:0 -f ogg pipe:1 2>/dev/null >/dev/null | sox -t ogg -q - $cachedir/$origterm.ogg;then
if [ -z $noplay ];then
if [ -f $cachedir/$origterm.ogg ];then
play -q $cachedir/$origterm.ogg
fi
else
sleep .01
fi
break
else
unset url
export url=`yt-dlp --default-search ytsearch --restrict-filenames -g -x $term 2> /dev/null`
curl -Ls $url|ffmpeg -y -i pipe:0 -f ogg pipe:1 2>/dev/null >/dev/null | sox -t ogg -q - $cachedir/$origterm.ogg
if [ -z $noplay ];then
if [ -f $cachedir/$origterm.ogg ];then
play -q $cachedir/$origterm.ogg
fi
else
sleep .01
fi
break
fi
done
fi
exit 0
