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 #+begin_src bash
function print_usage { 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 #+end_src
@ -61,9 +61,10 @@ This function prints the usage of the script.
At first we parse the arguments. At first we parse the arguments.
We have the following flags: We have the following flags:
- =-a*|--alternative= :: Alternative video (optional); You can parse any amount of alternatives (e.g. =-aaa=) - =-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 - =-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 - =-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. 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" format="flac"
interactive=false interactive=false
music=false music=false
save=false
help=false help=false
for arg in "$@" for arg in "$@"
@ -94,6 +96,10 @@ Additionally we have exacly one mandatory quoted string as query.
music=true music=true
shift shift
;; ;;
-s|--save)
save=true
shift
;;
-h|--help) -h|--help)
help=true help=true
shift shift
@ -237,10 +243,10 @@ If the interactive flag is present, show the first ten results and query for a v
fi fi
#+end_src #+end_src
*** Play video *** Play or save video
Finally the video is played via mpv. Finally the video is played via mpv or saved via youtube-dl.
If the =-m= flag is set, only the audio track is played. If the =-m= flag is set, only the audio track is played or saved.
In interaction mode, another video is queried to be played. In interaction mode, another video is queried to be played.
@ -249,35 +255,50 @@ 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" echo -ne "Playing: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ] if [ "$music" = true ]
then then
mpv --no-video $1 &> /dev/null mpv --no-video "$1" &> /dev/null
else else
mpv $1 &> /dev/null mpv "$1" &> /dev/null
fi 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 then
while : download "$url" "$title" "$uploader"
do else
echo -ne "\nSelect another or enter [q] to quit: " play "$url" "$title" "$uploader"
read selection
while [[ ! "${selections[@]}" =~ "${selection}" ]] if [ "$interactive" = true ]
then
while :
do do
echo -ne "Not valid, try again: " echo -ne "\nSelect another or enter [q] to quit: "
read selection read selection
while [[ ! "${selections[@]}" =~ "${selection}" ]]
do
echo -ne "Not valid, try again: "
read selection
done
if [ ! "$selection" = "q" ]
then
echo ""
url=${urls[$selection]}
title=${titles[$selection]}
uploader=${uploaders[$selection]}
play "$url" "$title" "$uploader"
else
exit
fi
done done
if [ ! "$selection" = "q" ] fi
then
echo ""
url=${urls[$selection]}
title=${titles[$selection]}
uploader=${uploaders[$selection]}
play "$url" "$title" "$uploader"
else
exit
fi
done
fi fi
#+end_src #+end_src

64
tyt
View file

@ -21,13 +21,14 @@ then
fi fi
function print_usage { 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 alternative=0
format="flac" format="flac"
interactive=false interactive=false
music=false music=false
save=false
help=false help=false
for arg in "$@" for arg in "$@"
@ -50,6 +51,10 @@ do
music=true music=true
shift shift
;; ;;
-s|--save)
save=true
shift
;;
-h|--help) -h|--help)
help=true help=true
shift shift
@ -174,34 +179,49 @@ function play {
echo -ne "Playing: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n" echo -ne "Playing: \e[32m\e[1m$2\e[0m (\e[33m\e[1m$3\e[0m)\n"
if [ "$music" = true ] if [ "$music" = true ]
then then
mpv --no-video $1 &> /dev/null mpv --no-video "$1" &> /dev/null
else else
mpv $1 &> /dev/null mpv "$1" &> /dev/null
fi 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 then
while : download "$url" "$title" "$uploader"
do else
echo -ne "\nSelect another or enter [q] to quit: " play "$url" "$title" "$uploader"
read selection
while [[ ! "${selections[@]}" =~ "${selection}" ]] if [ "$interactive" = true ]
then
while :
do do
echo -ne "Not valid, try again: " echo -ne "\nSelect another or enter [q] to quit: "
read selection read selection
while [[ ! "${selections[@]}" =~ "${selection}" ]]
do
echo -ne "Not valid, try again: "
read selection
done
if [ ! "$selection" = "q" ]
then
echo ""
url=${urls[$selection]}
title=${titles[$selection]}
uploader=${uploaders[$selection]}
play "$url" "$title" "$uploader"
else
exit
fi
done done
if [ ! "$selection" = "q" ] fi
then
echo ""
url=${urls[$selection]}
title=${titles[$selection]}
uploader=${uploaders[$selection]}
play "$url" "$title" "$uploader"
else
exit
fi
done
fi fi