HasteBin0

A CLI Bash multimedia downloader/player!

Jun 5th, 2024 (edited)
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.17 KB | Movies | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. help() {
  4.   cat << EOF
  5. Usage: ./ytd.sh [URL] [Format] [Folder] [Purge]
  6.  
  7. This script downloads a video from the provided URL using yt-dlp, plays it using mpv, and optionally deletes the downloaded files.
  8.  
  9. Arguments:
  10.   URL      The URL of the video to download. If not provided as an argument, you will be prompted to enter it.
  11.   Format   The format number to download. If not provided as an argument, you will be prompted to select from available formats.
  12.   Folder   The directory where the video will be downloaded. If not provided as an argument, you will be prompted to specify it.
  13.   Purge    A yes/no (default yes) on whether to purge the downloaded media's directory, delete the 'ytdv' subdirectory.
  14.  
  15. Behavior:
  16. 1. Prompt the user for the URL if it is not provided as an argument.
  17. 2. Use yt-dlp to fetch and display the list of available formats for the provided URL.
  18. 3. Prompt the user to select a format number if it is not provided as an argument.
  19. 4. Prompt the user for the download directory if it is not provided as an argument.
  20. 5. Create a subdirectory named 'ytdv' within the specified download directory and change into it.
  21. 6. Download the selected format using yt-dlp.
  22. 7. Play the downloaded files using mpv.
  23. 8. Prompt the user to remove the downloaded files after playback. If the user confirms, delete the 'ytdv' subdirectory.
  24.  
  25. Exit Codes:
  26.   0  Script executed successfully.
  27.   1  Failed to fetch formats for the provided URL.
  28.   2  The selected format is unavailable for the provided URL.
  29.   3  Invalid input for removing downloaded files (expected Y or N).
  30. EOF
  31. }
  32.  
  33. # Check if the first argument is a help flag and display help message if so
  34. if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "-?" ]]; then
  35.   help
  36.   exit 0
  37. fi
  38.  
  39. # Function to prompt the user and read input if not provided as an argument
  40. prompt_user() {
  41.   local prompt_message=$1
  42.   local input_variable_name=$2
  43.   local default_value=$3
  44.   local input_value
  45.  
  46.   if [[ -n "${!input_variable_name}" ]]; then
  47.     input_value="${!input_variable_name}"
  48.   else
  49.     read -p "$prompt_message" input_value
  50.     input_value=${input_value:-$default_value}
  51.   fi
  52.  
  53.   eval "$input_variable_name='$input_value'"
  54. }
  55.  
  56. # Get the URL from the argument or prompt the user
  57. prompt_user "URL?: " lnk "$1"
  58.  
  59. # Get the list of available formats
  60. /usr/bin/yt-dlp -F "$lnk"
  61. if [[ "$?" -ne 0 ]]; then
  62.   echo "Failed to fetch formats for the URL: $lnk"
  63.   exit 1
  64. fi
  65.  
  66. # Prompt the user for the format
  67. prompt_user "Format #: " fmt "$2"
  68.  
  69. # Prompt the user for the download directory
  70. prompt_user "Folder: " dr "$3"
  71.  
  72. # Create the directory and change into it
  73. mkdir -p "$dr/ytdv" && cd "$dr/ytdv"
  74.  
  75. # Download the selected format
  76. /usr/bin/yt-dlp -f "$fmt" "$lnk"
  77. if [[ "$?" -ne 0 ]]; then
  78.   echo "Format $fmt unavailable for \"$lnk\"."
  79.   exit 2
  80. fi
  81.  
  82. # Play the downloaded files with mpv
  83. /usr/bin/mpv ./*
  84.  
  85. # Prompt the user to remove the downloaded files
  86. prompt_user "Remove downloaded files [Y/n]? " rfm "$4"
  87. rfm=${rfm:-Y}
  88.  
  89. case "$rfm" in
  90.   [Yy]*)
  91.     cd "$dr"
  92.     rm -rf ./ytdv
  93.     ;;
  94.   [Nn]*)
  95.     ;;
  96.   *)
  97.     echo "ERROR: Invalid input, expected Y or N, but got $rfm!"
  98.     exit 3
  99.     ;;
  100. esac
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment