Skip to content

Instantly share code, notes, and snippets.

@supershadoe
Created June 25, 2020 11:46
Show Gist options
  • Save supershadoe/2defd70d89b933ed89035979b2f0e773 to your computer and use it in GitHub Desktop.
Save supershadoe/2defd70d89b933ed89035979b2f0e773 to your computer and use it in GitHub Desktop.
A script to play a youtube video or its audio alone in mpv.
#!/bin/sh
# vim:sw=4:ts=4:et
################################################################################
# #
# A shell script which searches youtube for a video or audio #
# and plays it in mpv. #
# Created by Supershadoe #
# #
################################################################################
# Test for dependencies #
for cmd in {ffmpeg,youtube-dl,mpv}; do
if ! command -v $cmd > /dev/null 2>&1; then
echo "$cmd is not installed."
exit 1
fi
done
# copying argument to variable for use #
arg2=$2
# Functions for different actions #
usage() {
cat << EOF >&2
A shell script to search youtube for a video or audio and play it in mpv.
Created by Supershadoe <<https://github.com/supershadoe>>
SYNTAX: ytmp [-- | { -l|--link } | { -s | --search } | { -h | --help }]
[link|search term] [-nv]
USAGE:
-h, --help shows this help message and exits
-s, --search searches youtube for the string given in arguments
-l, --link opens youtube link in mpv
-nv no video(only audio mode)
Flags other than these throws an error and exits.
The no video flag is optional and by default video is played.
Extra arguments are also ignored.
If you want the script to detect the mode and run(instead of providing mode
manually), give -- before typing your link/search term.
EOF
}
link() {
title=$(youtube-dl --get-title "$arg2")
[ $? -eq 0 ] && {
echo "Playing..."
echo -e "\nFile name: $title"
mpv $no_video_arg --ytdl-raw-options=write-sub=,write-auto-sub=,sub-lang=en "$arg2"
unset title
unset arg2
echo "Bye!!"
}
}
search() {
numres=5
startres=1
ctl_sch="y"
while [ "$ctl_sch" = "y" -o -z "$ctl_sch" ]; do
youtube-dl --get-title --playlist-start $startres "ytsearch$numres:$arg2"
read -p "Which video?(1-5,0 for none) " choice
if [ $choice -lt 6 ] && [ $choice -gt 0 ]; then
itemnum=$(( choice + (numres - 5) ))
title=$(youtube-dl --get-title --playlist-item $itemnum "ytsearch$numres:$arg2")
ctl_sch="n"
elif [ $choice -eq 0 ]; then
read -p "Show more results? [Y/n]: " ctl_more
if [ "$ctl_more" = "n" ]; then
exit 1
fi
else
echo "Choice invalid..."
exit 1
fi
done
unset startres
unset ctl_sch
unset choice
unset ctl_more
echo "Playing..."
echo -e "\nFile name: $title"
mpv $no_video_arg --ytdl-raw-options=write-sub=,write-auto-sub=,sub-lang=en,playlist-item=$itemnum "ytdl://ytsearch$numres:$arg2"
unset title
unset itemnum
unset numres
unset arg2
echo "Bye!!"
}
unsetvariables() {
unset numres
unset startres
unset ctl_sch
unset choice
unset itemnum
unset ctl_more
}
# main #
[ "$3" = "-nv" ] && no_video_arg="--no-video --force-window=no"
case "$1" in
-l | --link)
link
[ $? -ne 0 ] && echo "Can't open link..." && exit 1;;
-s | --search)
search;;
-h | --help)
usage;;
--)
link 2> /dev/null || search
;;
"")
echo "No option given..."
usage
exit 1;;
*)
echo "Invalid choice..."
usage
exit 1;;
esac
@supershadoe
Copy link
Author

Dependencies: ffmpeg, mpv and youtube-dl
Save this script to some folder which is in your PATH variable (/usr/local/bin or some custom folder)
Refer this page to see how to add custom folders to PATH variable in zsh(I think it works for bash also except that you need to add those lines in .bashenv file instead of .zshenv)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment