add download feature

This commit is contained in:
Denis Lehmann 2021-04-13 22:44:18 +02:00
parent 539789955b
commit efde00d5c0
2 changed files with 91 additions and 50 deletions

View file

@ -52,7 +52,7 @@ This function prints the usage of the script.
#+begin_src bash
function print_usage {
echo "tyt [ (-a* | --alternative) | (-i | --interactive) | (-m | --music) ] \"QUERY\""
echo "tyt [ (-a* | --alternative) | (-i | --interactive) | (-m | --music) (-s | --save)] \"QUERY\""
}
#+end_src
@ -61,9 +61,10 @@ This function prints the usage of the script.
At first we parse the arguments.
We have the following flags:
- =-a*|--alternative= :: Alternative video (optional); You can parse any amount of alternatives (e.g. =-aaa=)
- =-i|--interactive= :: Interactive mode; Shows the first 10 results and queries for a selection; If this flag is set, =-a= is ignored
- =-m|--music= :: Play only the audio track of the video
- =-a* | --alternative= :: Alternative video (optional); You can parse any amount of alternatives (e.g. =-aaa=)
- =-i | --interactive= :: Interactive mode; Shows the first 10 results and queries for a selection; If this flag is set, =-a= is ignored
- =-m | --music= :: Play only the audio track of the video
- =-s | --save= :: Save the video (or audio if =-m= is set) to the current directory
Additionally we have exacly one mandatory quoted string as query.
@ -72,6 +73,7 @@ Additionally we have exacly one mandatory quoted string as query.
format="flac"
interactive=false
music=false
save=false
help=false
for arg in "$@"
@ -94,6 +96,10 @@ Additionally we have exacly one mandatory quoted string as query.
music=true
shift
;;
-s|--save)
save=true
shift
;;
-h|--help)
help=true
shift
@ -237,10 +243,10 @@ If the interactive flag is present, show the first ten results and query for a v
fi
#+end_src
*** Play video
*** Play or save video
Finally the video is played via mpv.
If the =-m= flag is set, only the audio track is played.
Finally the video is played via mpv or saved via youtube-dl.
If the =-m= flag is set, only the audio track is played or saved.
In interaction mode, another video is queried to be played.
@ -249,12 +255,26 @@ In interaction mode, another video is queried to be played.
echo -ne "Playing: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ]
then
mpv --no-video $1 &> /dev/null
mpv --no-video "$1" &> /dev/null
else
mpv $1 &> /dev/null
mpv "$1" &> /dev/null
fi
}
function download {
echo -ne "Downloading: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ]
then
youtube-dl -x -o "%(title)s.%(ext)s" "$1" &> /dev/null
else
youtube-dl -o "%(title)s.%(ext)s" "$1" &> /dev/null
fi
}
if [ "$save" = true ]
then
download "$url" "$title" "$uploader"
else
play "$url" "$title" "$uploader"
if [ "$interactive" = true ]
@ -280,4 +300,5 @@ In interaction mode, another video is queried to be played.
fi
done
fi
fi
#+end_src

30
tyt
View file

@ -21,13 +21,14 @@ then
fi
function print_usage {
echo "tyt [ (-a* | --alternative) | (-i | --interactive) | (-m | --music) ] \"QUERY\""
echo "tyt [ (-a* | --alternative) | (-i | --interactive) | (-m | --music) (-s | --save)] \"QUERY\""
}
alternative=0
format="flac"
interactive=false
music=false
save=false
help=false
for arg in "$@"
@ -50,6 +51,10 @@ do
music=true
shift
;;
-s|--save)
save=true
shift
;;
-h|--help)
help=true
shift
@ -174,16 +179,30 @@ function play {
echo -ne "Playing: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ]
then
mpv --no-video $1 &> /dev/null
mpv --no-video "$1" &> /dev/null
else
mpv $1 &> /dev/null
mpv "$1" &> /dev/null
fi
}
play "$url" "$title" "$uploader"
function download {
echo -ne "Downloading: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ]
then
youtube-dl -x -o "%(title)s.%(ext)s" "$1" &> /dev/null
else
youtube-dl -o "%(title)s.%(ext)s" "$1" &> /dev/null
fi
}
if [ "$interactive" = true ]
if [ "$save" = true ]
then
download "$url" "$title" "$uploader"
else
play "$url" "$title" "$uploader"
if [ "$interactive" = true ]
then
while :
do
echo -ne "\nSelect another or enter [q] to quit: "
@ -204,4 +223,5 @@ then
exit
fi
done
fi
fi