Advertisement
VghLaci

mpv_ytw.sh

Dec 12th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.83 KB | None | 0 0
  1. #!/bin/bash
  2. #title           :
  3. #description     :Play youtube/twitch videos with mpv and yt-dlp from terminal.
  4. #author          :lovelettersfromtheslum / a real noob
  5. #date            :2024.10.23
  6. #
  7. #version         :0.2-2
  8. #usage           :Add "alias ytw="$HOME/MyApps/mpv_YTW.sh" to "$HOME/.bash_functions", then use "ytw [NUMBER]".
  9. #                 "ytw" without any user input will read link from clipboard. "ytw 0" read history with fzf.
  10. #notes           :
  11. #
  12. #bash_version    :4.4.19(1)-release
  13. #dependencies    :mpv, awk, sed, fzf, yt-dlp, xclip
  14. #=======================================================================
  15. #              PLAYER OPTIONS
  16. #=======================================================================
  17. #          MPV OPTIONS
  18. #mpv_parameter=(--vf=eq=1.0:0.1:1.0:1.0 --autofit-larger=582 --autofit-smaller=500x500 --geometry=+0+0 --no-border)
  19. #
  20. 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)
  21. #      YT-DLP - YOUTUBE WEBM/MP4 TWITCH
  22. #  360p30
  23. ytdl_parameter=('243+250/243+251/134+139/134+140/360p/360p30')
  24. #  480p30
  25. #ytdl_parameter=('244+251/135+140/480p/480p30')
  26. #  720p30
  27. #ytdl_parameter=('247+251/136+140/720p/720p30')
  28. #  720p60
  29. #ytdl_parameter=('302+251/298+140/720p60/720p')
  30. #  1080p60
  31. #ytdl_parameter=('303+251/299+140/1080p60/1080p')
  32. #=======================================================================
  33. #              SET HISTORY FILE
  34. #=======================================================================
  35. path_to_history="$HOME/MyApps/rofi/twitch-tv_youtube-com/history.txt"
  36. #=======================================================================
  37. #              COLORS FOR SOME OUTPUT TEXT
  38. #=======================================================================
  39. BLINKING='\033[1;5;31m'
  40. BLINKING_2='\033[1;5;32m'
  41. RED='\033[1;31m'
  42. WHITE='\033[1;37m'
  43. LBLUE='\033[1;32m'
  44. YELLOW='\033[1;33m'
  45. YELLOW_2='\033[0;33m'
  46. TEST_1='\033[1;34m'
  47. TEST_2='\033[1;35m'
  48. TEST_3='\033[1;36m'
  49. NC='\033[0m' # NO COLOR
  50. #=======================================================================
  51. IFS=$(echo -en "\n\b")
  52. #
  53. tput civis # HIDE CURSOR
  54. #=======================================================================
  55. #              FUNCTION(S)
  56. #=======================================================================
  57. start_to_play () {
  58. mpv "${mpv_parameter[@]}" --ytdl-format="${ytdl_parameter[@]}" "$link"
  59. }
  60. #=======================================================================
  61. #              GET THE LINK
  62. #=======================================================================
  63. if [[ -z "$1" ]]; then
  64.    link_from_clipboard="$(xclip -selection c -o|head -1|sed 's/^[ \t]*//')"
  65. #-------- CHANGE VIEWTUBE.IO TO REDIRECT.INVIDIOUS.IO -----
  66.    if [[ "$link_from_clipboard" == *"viewtube.io"* ]];then
  67.      link="$(echo "$link_from_clipboard"|sed 's\viewtube.io\redirect.invidious.io\g')"
  68.    else
  69.      link="$link_from_clipboard"
  70.    fi
  71. #-------- REGISTRY TIMESTAMP IN HISTORY -------------------
  72.    right_now=$(date '+%y'-'%m'-'%d_%H'h'%M'm'%S's'')
  73.    clear
  74. #-------- GETTING TITLE -----------------------------------
  75.    if [[ "$link_from_clipboard" == "https://"* ]];then
  76.        echo -en "${BLINKING_2}Trying to getting the title....just a moment.${NC}\n"
  77.        link_title="$(yt-dlp --get-title "$link")"
  78.    else
  79.        :
  80.    fi
  81. #
  82. elif [[ "$1" -eq 0 ]]; then
  83.    OUT="$(grep -n -v "^#\|^[[:blank:]]*$" "$path_to_history"|sed 's/:/. /'|fzf)"
  84.    link="$(echo -en "$OUT"|rev|awk -F"ptth :" '{print $1"ptth"}'|rev)"
  85.    link_title="$(echo -en "$OUT"|awk -F": http://|: https://" '{print $1}')"
  86. elif [[ "$1" -ge 1 ]]; then
  87.    line="$(sed "${1}q;d" "$path_to_history")"
  88.    link="$(echo "$line"|rev|awk -F" :" '{print $1}'|rev)"
  89.    link_title="$(echo "$line"|awk -F": http://|: https://" '{print $1}')"
  90. else
  91.    echo "Error!!!"
  92.    exit 1
  93. fi
  94. tput civis # HIDE CURSOR
  95. clear # CLEAR SCREEN
  96. #=======================================================================
  97. #              PLAY AND / OR EXIT
  98. #=======================================================================
  99. if [[ "$link" == *"/watch?v="* || \
  100.    "$link" == "https://youtu.be/"* || \
  101.    "$link" == "https://www.twitch.tv/"* ]]; then
  102. #//////// WRITE OUT INFO //////////////////////////////////
  103.    echo -en "${TEST_2}--------------------------------------------------------------------------------------------\n\
  104. ${NC}$link_title\n\
  105. ${LBLUE}$link\n\
  106. ${TEST_2}--------------------------------------------------------------------------------------------\n\
  107. ${NC}mpv parameter(s):\n${LBLUE}${mpv_parameter[@]}\n\
  108. ${NC}ytdl format(s):\n${LBLUE}$(echo "$ytdl_parameter"|cut -d"=" -f2)\n\
  109. ${TEST_2}--------------------------------------------------------------------------------------------\n${NC}"
  110. #//////// ADD LINK TO HISTORY /////////////////////////////
  111.    if [[ "$1" =~ ^[0-9] ]];then
  112.        :
  113.    else
  114.        sed -i "1 i$right_now | $link_title: $link" $path_to_history
  115.        echo -en "${NC}link has been added to ${LBLUE}"$path_to_history"\n\
  116. ${TEST_2}--------------------------------------------------------------------------------------------\n${NC}"
  117.    fi
  118. #//////// PLAY ////////////////////////////////////////////
  119.    start_to_play
  120.    tput cnorm # SHOW CURSOR
  121.    exit 0
  122. #//////// IF LINK EMPTY ///////////////////////////////////
  123. elif [[ -z "$link" ]]; then
  124.    echo -en "\n${TEST_2}--------------------------------------------------------\n\
  125. ${LBLUE}             Nem lett link kiválasztva !!! \n\
  126. ${TEST_2}--------------------------------------------------------${NC}\n\n"
  127.    tput cnorm
  128.    exit 1
  129. #//////// IF NOT A LINK ///////////////////////////////////
  130. else
  131.    echo -en "${NC}Not suitable for this script.\n\
  132. ${YELLOW}--------------------------------------------------------\n\
  133. ${RED} $link \n\
  134. ${YELLOW}--------------------------------------------------------${NC}\n"
  135.    tput cnorm
  136.    exit 1
  137. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement