Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- help() {
- cat << EOF
- Usage: ./ytd.sh [URL] [Format] [Folder] [Purge]
- This script downloads a video from the provided URL using yt-dlp, plays it using mpv, and optionally deletes the downloaded files.
- Arguments:
- URL The URL of the video to download. If not provided as an argument, you will be prompted to enter it.
- Format The format number to download. If not provided as an argument, you will be prompted to select from available formats.
- Folder The directory where the video will be downloaded. If not provided as an argument, you will be prompted to specify it.
- Purge A yes/no (default yes) on whether to purge the downloaded media's directory, delete the 'ytdv' subdirectory.
- Behavior:
- 1. Prompt the user for the URL if it is not provided as an argument.
- 2. Use yt-dlp to fetch and display the list of available formats for the provided URL.
- 3. Prompt the user to select a format number if it is not provided as an argument.
- 4. Prompt the user for the download directory if it is not provided as an argument.
- 5. Create a subdirectory named 'ytdv' within the specified download directory and change into it.
- 6. Download the selected format using yt-dlp.
- 7. Play the downloaded files using mpv.
- 8. Prompt the user to remove the downloaded files after playback. If the user confirms, delete the 'ytdv' subdirectory.
- Exit Codes:
- 0 Script executed successfully.
- 1 Failed to fetch formats for the provided URL.
- 2 The selected format is unavailable for the provided URL.
- 3 Invalid input for removing downloaded files (expected Y or N).
- EOF
- }
- # Check if the first argument is a help flag and display help message if so
- if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "-?" ]]; then
- help
- exit 0
- fi
- # Function to prompt the user and read input if not provided as an argument
- prompt_user() {
- local prompt_message=$1
- local input_variable_name=$2
- local default_value=$3
- local input_value
- if [[ -n "${!input_variable_name}" ]]; then
- input_value="${!input_variable_name}"
- else
- read -p "$prompt_message" input_value
- input_value=${input_value:-$default_value}
- fi
- eval "$input_variable_name='$input_value'"
- }
- # Get the URL from the argument or prompt the user
- prompt_user "URL?: " lnk "$1"
- # Get the list of available formats
- /usr/bin/yt-dlp -F "$lnk"
- if [[ "$?" -ne 0 ]]; then
- echo "Failed to fetch formats for the URL: $lnk"
- exit 1
- fi
- # Prompt the user for the format
- prompt_user "Format #: " fmt "$2"
- # Prompt the user for the download directory
- prompt_user "Folder: " dr "$3"
- # Create the directory and change into it
- mkdir -p "$dr/ytdv" && cd "$dr/ytdv"
- # Download the selected format
- /usr/bin/yt-dlp -f "$fmt" "$lnk"
- if [[ "$?" -ne 0 ]]; then
- echo "Format $fmt unavailable for \"$lnk\"."
- exit 2
- fi
- # Play the downloaded files with mpv
- /usr/bin/mpv ./*
- # Prompt the user to remove the downloaded files
- prompt_user "Remove downloaded files [Y/n]? " rfm "$4"
- rfm=${rfm:-Y}
- case "$rfm" in
- [Yy]*)
- cd "$dr"
- rm -rf ./ytdv
- ;;
- [Nn]*)
- ;;
- *)
- echo "ERROR: Invalid input, expected Y or N, but got $rfm!"
- exit 3
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment