Skip to content

Instantly share code, notes, and snippets.

@nishantbadhautiya
Last active April 7, 2025 16:54
Show Gist options
  • Select an option

  • Save nishantbadhautiya/f6b6a9d5b79ed1934728a2eb110f1f0f to your computer and use it in GitHub Desktop.

Select an option

Save nishantbadhautiya/f6b6a9d5b79ed1934728a2eb110f1f0f to your computer and use it in GitHub Desktop.
YouTube Playlist Downloader Script
#!/bin/bash
# Ensure the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "❌ Please run this script as root (use sudo)."
exit 1
fi
echo "======================= βœ… STEP 1: Folder Setup ======================="
FOLDER="/others"
if [ -d "$FOLDER" ]; then
echo "πŸ“ The folder $FOLDER already exists."
else
mkdir "$FOLDER"
echo "βœ… Folder $FOLDER created successfully."
fi
echo "===================== βœ… STEP 2: ffmpeg Check/Install ==================="
if command -v ffmpeg >/dev/null 2>&1; then
echo "βœ… ffmpeg is already installed at $(which ffmpeg)"
ffmpeg -version
else
echo "πŸ” ffmpeg is not installed. Installing..."
apt update && apt install -y ffmpeg
if command -v ffmpeg >/dev/null 2>&1; then
echo "βœ… ffmpeg installed successfully!"
else
echo "❌ Installation failed. Please check your internet connection or sources."
read -p "❓ Do you want to continue anyway? (y/n): " user_choice
case "$user_choice" in
y|Y ) echo "➑️ Continuing..." ;;
* ) echo "πŸ‘‹ Exiting."; exit 1 ;;
esac
fi
fi
echo "==================== βœ… STEP 3: yt-dlp Check/Install ==================="
if command -v yt-dlp >/dev/null 2>&1; then
echo "βœ… yt-dlp is already installed at $(which yt-dlp)"
else
echo "πŸ” yt-dlp not installed. Installing..."
curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
chmod a+rx /usr/local/bin/yt-dlp
if command -v yt-dlp >/dev/null 2>&1; then
echo "βœ… yt-dlp installed successfully!"
else
echo "❌ Failed to install yt-dlp. Exiting script."
exit 1
fi
fi
echo "πŸ“¦ yt-dlp version: $(yt-dlp --version)"
echo "=================== βœ… STEP 4: Download Playlist ======================"
OUTPUT_DIR="/others/youtube"
mkdir -p "$OUTPUT_DIR"
read -p "πŸ“Ί Enter YouTube Playlist URL: " PLAYLIST_URL
# Video quality
echo "πŸŽ₯ Choose video quality (default: 720p):"
echo " 1) 144p"
echo " 2) 240p"
echo " 3) 360p"
echo " 4) 480p"
echo " 5) 720p"
echo " 6) 1080p"
echo " 7) 1440p"
echo " 8) 2160p"
read -p "πŸ‘‰ Enter number (1-8) for desired quality [default 720px]: " QUALITY_OPTION
QUALITY_OPTION=${QUALITY_OPTION:-5}
case $QUALITY_OPTION in
1) QUALITY=144 ;;
2) QUALITY=240 ;;
3) QUALITY=360 ;;
4) QUALITY=480 ;;
5) QUALITY=720 ;;
6) QUALITY=1080 ;;
7) QUALITY=1440 ;;
8) QUALITY=2160 ;;
*) echo "❌ Invalid option. Exiting."; exit 1 ;;
esac
# Playlist start index
read -p "πŸ”’ Playlist start (default: 1): " START_INDEX
START_INDEX=${START_INDEX:-1}
# Playlist end index (optional)
read -p "πŸ”’ Playlist end (default: last video): " END_INDEX
# Ask for subtitle
read -p "πŸ“ Do you want subtitles? (y/n) [default: n]: " SUB_CHOICE
SUB_CHOICE=${SUB_CHOICE:-n}
echo "πŸš€ Starting download..."
# Download logic with or without subtitles
if [[ "$SUB_CHOICE" =~ ^[Yy]$ ]]; then
yt-dlp -f "bestvideo[height<=${QUALITY}]+bestaudio" \
-o "${OUTPUT_DIR}/%(playlist_index)s-%(title)s.%(ext)s" \
--embed-subs --sub-lang "en" \
--playlist-start "$START_INDEX" \
${END_INDEX:+--playlist-end "$END_INDEX"} \
"$PLAYLIST_URL"
else
yt-dlp -f "bestvideo[height<=${QUALITY}]+bestaudio" \
-o "${OUTPUT_DIR}/%(playlist_index)s-%(title)s.%(ext)s" \
--playlist-start "$START_INDEX" \
${END_INDEX:+--playlist-end "$END_INDEX"} \
"$PLAYLIST_URL"
fi
echo "βœ… Playlist download complete!"
echo "======================= βœ… STEP 5: Zip & Upload ========================"
ZIP_NAME="youtube_download.zip"
ZIP_PATH="/others/$ZIP_NAME"
LINK_FILE="/others/youtube_download.txt"
echo "πŸ“¦ Creating ZIP archive..."
zip -r "$ZIP_PATH" "$OUTPUT_DIR" >/dev/null
# Check if ZIP was successfully created
if [ ! -f "$ZIP_PATH" ]; then
echo "❌ Failed to create ZIP file. Exiting."
exit 1
fi
# βœ… Print total size of the ZIP file (human-readable)
FILE_SIZE=$(du -h "$ZIP_PATH" | cut -f1)
echo "πŸ“ Total ZIP file size: $FILE_SIZE"
# Upload with progress bar
echo "🌐 Uploading ZIP using bashupload with progress bar..."
UPLOAD_OUTPUT=$(curl --progress-bar --upload-file "$ZIP_PATH" https://bashupload.com/"$ZIP_NAME")
# Extract download link from upload response
DOWNLOAD_LINK=$(echo "$UPLOAD_OUTPUT" | grep -Eo 'https://bashupload\.com/[a-zA-Z0-9._/-]+' | tail -n 1)
# Get human-readable file size
FILE_SIZE=$(du -h "$ZIP_PATH" | cut -f1)
# Save the link to a text file
echo "$DOWNLOAD_LINK" > "$LINK_FILE"
# Display download info box
if [[ -n "$DOWNLOAD_LINK" ]]; then
echo ""
echo "🟒 Your ZIP file is ready to download!"
echo ""
echo "╔════════════════════════════════════════════╗"
echo "β•‘ πŸ“₯ Download Link: β•‘"
echo "β•‘ β•‘"
printf "β•‘ %-42s β•‘\n" "$DOWNLOAD_LINK"
echo "β•‘ β•‘"
printf "β•‘ πŸ“¦ File Size: %-30s β•‘\n" "$FILE_SIZE"
echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
echo ""
echo ""
else
echo "❌ Failed to retrieve download link. Check your internet or try again."
fi
# Clean up
echo ""
echo "🧹 Cleaning up all files in /others/ ..."
rm -rf /others/*
echo "βœ… Cleanup complete."
echo "βœ… All steps completed successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment