Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #title :
- #description :Play youtube/twitch videos with mpv and yt-dlp from terminal.
- #author :lovelettersfromtheslum / a real noob
- #date :2024.10.23
- #
- #version :0.2-2
- #usage :Add "alias ytw="$HOME/MyApps/mpv_YTW.sh" to "$HOME/.bash_functions", then use "ytw [NUMBER]".
- # "ytw" without any user input will read link from clipboard. "ytw 0" read history with fzf.
- #notes :
- #
- #bash_version :4.4.19(1)-release
- #dependencies :mpv, awk, sed, fzf, yt-dlp, xclip
- #=======================================================================
- # PLAYER OPTIONS
- #=======================================================================
- # MPV OPTIONS
- #mpv_parameter=(--vf=eq=1.0:0.1:1.0:1.0 --autofit-larger=582 --autofit-smaller=500x500 --geometry=+0+0 --no-border)
- #
- mpv_parameter=(--vf=eq=1.0:0.1:1.0:1.0 --vo=x11 --autofit-larger=582 --autofit-smaller=500x500 --geometry=+0+0 --no-border)
- # YT-DLP - YOUTUBE WEBM/MP4 TWITCH
- # 360p30
- ytdl_parameter=('243+250/243+251/134+139/134+140/360p/360p30')
- # 480p30
- #ytdl_parameter=('244+251/135+140/480p/480p30')
- # 720p30
- #ytdl_parameter=('247+251/136+140/720p/720p30')
- # 720p60
- #ytdl_parameter=('302+251/298+140/720p60/720p')
- # 1080p60
- #ytdl_parameter=('303+251/299+140/1080p60/1080p')
- #=======================================================================
- # SET HISTORY FILE
- #=======================================================================
- path_to_history="$HOME/MyApps/rofi/twitch-tv_youtube-com/history.txt"
- #=======================================================================
- # COLORS FOR SOME OUTPUT TEXT
- #=======================================================================
- BLINKING='\033[1;5;31m'
- BLINKING_2='\033[1;5;32m'
- RED='\033[1;31m'
- WHITE='\033[1;37m'
- LBLUE='\033[1;32m'
- YELLOW='\033[1;33m'
- YELLOW_2='\033[0;33m'
- TEST_1='\033[1;34m'
- TEST_2='\033[1;35m'
- TEST_3='\033[1;36m'
- NC='\033[0m' # NO COLOR
- #=======================================================================
- IFS=$(echo -en "\n\b")
- #
- tput civis # HIDE CURSOR
- #=======================================================================
- # FUNCTION(S)
- #=======================================================================
- start_to_play () {
- mpv "${mpv_parameter[@]}" --ytdl-format="${ytdl_parameter[@]}" "$link"
- }
- #=======================================================================
- # GET THE LINK
- #=======================================================================
- if [[ -z "$1" ]]; then
- link_from_clipboard="$(xclip -selection c -o|head -1|sed 's/^[ \t]*//')"
- #-------- CHANGE VIEWTUBE.IO TO REDIRECT.INVIDIOUS.IO -----
- if [[ "$link_from_clipboard" == *"viewtube.io"* ]];then
- link="$(echo "$link_from_clipboard"|sed 's\viewtube.io\redirect.invidious.io\g')"
- else
- link="$link_from_clipboard"
- fi
- #-------- REGISTRY TIMESTAMP IN HISTORY -------------------
- right_now=$(date '+%y'-'%m'-'%d_%H'h'%M'm'%S's'')
- clear
- #-------- GETTING TITLE -----------------------------------
- if [[ "$link_from_clipboard" == "https://"* ]];then
- echo -en "${BLINKING_2}Trying to getting the title....just a moment.${NC}\n"
- link_title="$(yt-dlp --get-title "$link")"
- else
- :
- fi
- #
- elif [[ "$1" -eq 0 ]]; then
- OUT="$(grep -n -v "^#\|^[[:blank:]]*$" "$path_to_history"|sed 's/:/. /'|fzf)"
- link="$(echo -en "$OUT"|rev|awk -F"ptth :" '{print $1"ptth"}'|rev)"
- link_title="$(echo -en "$OUT"|awk -F": http://|: https://" '{print $1}')"
- elif [[ "$1" -ge 1 ]]; then
- line="$(sed "${1}q;d" "$path_to_history")"
- link="$(echo "$line"|rev|awk -F" :" '{print $1}'|rev)"
- link_title="$(echo "$line"|awk -F": http://|: https://" '{print $1}')"
- else
- echo "Error!!!"
- exit 1
- fi
- tput civis # HIDE CURSOR
- clear # CLEAR SCREEN
- #=======================================================================
- # PLAY AND / OR EXIT
- #=======================================================================
- if [[ "$link" == *"/watch?v="* || \
- "$link" == "https://youtu.be/"* || \
- "$link" == "https://www.twitch.tv/"* ]]; then
- #//////// WRITE OUT INFO //////////////////////////////////
- echo -en "${TEST_2}--------------------------------------------------------------------------------------------\n\
- ${NC}$link_title\n\
- ${LBLUE}$link\n\
- ${TEST_2}--------------------------------------------------------------------------------------------\n\
- ${NC}mpv parameter(s):\n${LBLUE}${mpv_parameter[@]}\n\
- ${NC}ytdl format(s):\n${LBLUE}$(echo "$ytdl_parameter"|cut -d"=" -f2)\n\
- ${TEST_2}--------------------------------------------------------------------------------------------\n${NC}"
- #//////// ADD LINK TO HISTORY /////////////////////////////
- if [[ "$1" =~ ^[0-9] ]];then
- :
- else
- sed -i "1 i$right_now | $link_title: $link" $path_to_history
- echo -en "${NC}link has been added to ${LBLUE}"$path_to_history"\n\
- ${TEST_2}--------------------------------------------------------------------------------------------\n${NC}"
- fi
- #//////// PLAY ////////////////////////////////////////////
- start_to_play
- tput cnorm # SHOW CURSOR
- exit 0
- #//////// IF LINK EMPTY ///////////////////////////////////
- elif [[ -z "$link" ]]; then
- echo -en "\n${TEST_2}--------------------------------------------------------\n\
- ${LBLUE} Nem lett link kiválasztva !!! \n\
- ${TEST_2}--------------------------------------------------------${NC}\n\n"
- tput cnorm
- exit 1
- #//////// IF NOT A LINK ///////////////////////////////////
- else
- echo -en "${NC}Not suitable for this script.\n\
- ${YELLOW}--------------------------------------------------------\n\
- ${RED} $link \n\
- ${YELLOW}--------------------------------------------------------${NC}\n"
- tput cnorm
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement