Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save nishantbadhautiya/bbd2128120584a83a08f93d9c39daa5f to your computer and use it in GitHub Desktop.
Download youtube videos and upload them on GDrive using rclone
#!/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 to Google Drive using rclone
GDRIVE_REMOTE="gdrive" # Your rclone remote name
GDRIVE_PATH="YouTubeDownloads/$ZIP_NAME"
if [ ! -f /root/.config/rclone/rclone.conf ]; then
echo "❌ rclone config for root not found. Copying from user..."
mkdir -p /root/.config/rclone
cp /home/$SUDO_USER/.config/rclone/rclone.conf /root/.config/rclone/
echo "βœ… rclone config copied successfully."
fi
echo "☁️ Uploading to Google Drive using rclone..."
rclone copy "$ZIP_PATH" "$GDRIVE_REMOTE:$GDRIVE_PATH" --progress
# Check if upload succeeded
if rclone ls "$GDRIVE_REMOTE:$GDRIVE_PATH" >/dev/null 2>&1; then
echo ""
echo "🟒 Your ZIP file was uploaded successfully to Google Drive!"
echo ""
echo "╔═════════════════════════════════════════════════════════╗"
echo "β•‘ ☁️ Google Drive Path: β•‘"
echo "β•‘ β•‘"
printf "β•‘ %-51s β•‘\n" "$GDRIVE_PATH"
echo "β•‘ β•‘"
printf "β•‘ πŸ“¦ File Size: %-37s β•‘\n" "$FILE_SIZE"
echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
echo ""
# Save the upload path to a text file
echo "Google Drive Path: $GDRIVE_PATH" > "$LINK_FILE"
else
echo "❌ Upload failed. Please check your rclone configuration and try again."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment