initial commit

This commit is contained in:
Christian Ulrich 2020-09-30 18:27:47 +02:00
commit 6d33892049
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 73 additions and 0 deletions

73
watchyt.sh Executable file
View File

@ -0,0 +1,73 @@
#!/bin/sh
max_results=${2:-10}
line_index=0
title=""
id=""
uploader=""
duration=""
video_titles=()
video_urls=()
mkdir -p "$HOME/.local/share/watchyt"
cd "$HOME/.local/share/watchyt"
mkfifo youtube_dl_output
youtube-dl --get-title --get-filename -o '%(id)s;%(duration)d;%(upload_date)s;%(uploader)s' ytsearch${max_results}:"$1" 2>/dev/null > youtube_dl_output &
while read line
do
case $line_index in
0)
title="$line"
;;
1)
id=$(echo "$line" | cut -d ';' -f 1)
duration=$(echo "$line" | cut -d ';' -f 2)
upload_date=$(echo "$line" | cut -d ';' -f 3)
uploader=$(echo "$line" | cut -d ';' -f 4)
video_titles+=( "$title" )
video_urls+=( "https://youtube.com/watch?v=$id" )
duration_minutes=$(( duration / 60 ))
duration_seconds=$(( duration % 60 ))
upload_date_formatted=$(date -d"$upload_date" +%d/%m/%Y)
entry_index=${#video_urls[@]}
echo "$(printf "%2d" $entry_index)) $title [$duration_minutes:$(printf "%02d" $duration_seconds)]"
echo " uploaded by \"$uploader\" on $upload_date_formatted"
echo ""
;;
esac
line_index=$(( line_index + 1))
line_index=$(( line_index % 2 ))
done < youtube_dl_output
rm youtube_dl_output
video_count="${#video_urls}"
if [ "$video_count" -eq "0" ]
then
echo "no videos found"
exit 0
fi
echo -n "please select video: "
read selection
case $selection in
(*[!0-9]*|'')
echo "invalid selection"
exit 1
;;
(*)
if [ "$selection" -gt $(( video_count )) ]
then
echo "invalid selection"
exit 2
fi
video_url="${video_urls[$(( selection - 1 ))]}"
youtube-dl --restrict-filenames -o '%(title)s.%(ext)s' "$video_url"
filename=$(youtube-dl -s --get-filename --restrict-filenames -o '%(title)s' "$video_url")
echo "filename: $filename"
vlc "$filename".* 2>/dev/null
;;
esac