Created
June 2, 2025 05:01
-
-
Save nishantbadhautiya/bbd2128120584a83a08f93d9c39daa5f to your computer and use it in GitHub Desktop.
Download youtube videos and upload them on GDrive using rclone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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