-
-
Save Bioblaze/e237c5c6fc6a2a76268a727fe57e5bbf to your computer and use it in GitHub Desktop.
A script to play a youtube video or its audio alone in mpv.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment