Skip to content

Instantly share code, notes, and snippets.

@vega113
Last active October 10, 2024 14:15
Show Gist options
  • Save vega113/5265120d364494af8751ab00856e2edb to your computer and use it in GitHub Desktop.
Save vega113/5265120d364494af8751ab00856e2edb to your computer and use it in GitHub Desktop.
Script to download youtube video using command line
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for yt-dlp
if ! command_exists yt-dlp; then
echo "yt-dlp not found. Installing yt-dlp..."
curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o ~/.local/bin/yt-dlp
chmod a+rx ~/.local/bin/yt-dlp
export PATH=$PATH:~/.local/bin
fi
# Check for ffmpeg
if ! command_exists ffmpeg; then
echo "ffmpeg not found. Installing ffmpeg..."
brew install ffmpeg --quiet
fi
# Check for python3
if ! command_exists python3; then
echo "python3 not found. Please install python3."
exit 1
fi
# Check for virtual environment and activate it
if [ ! -d "venv" ]; then
echo "Creating virtual environment..."
python3 -m venv venv
fi
source venv/bin/activate
# Check for required Python packages
pip install --quiet browser-cookie3 boto3 requests
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <youtube_url>"
exit 1
fi
YOUTUBE_URL=$1
# Python script to extract YouTube cookies
extract_youtube_cookies_py=$(cat <<'EOF'
import browser_cookie3
import os
LOCAL_COOKIES_PATH = "/tmp/cookies.txt"
def extract_youtube_cookies():
try:
cookies = browser_cookie3.chrome(domain_name='youtube.com')
with open(LOCAL_COOKIES_PATH, 'w') as file:
file.write("# Netscape HTTP Cookie File\n")
file.write("# This file was generated by extract_youtube_cookies.py\n")
file.write("#\n")
for cookie in cookies:
file.write(
f"{cookie.domain}\t"
f"{'TRUE' if cookie.domain.startswith('.') else 'FALSE'}\t"
f"{cookie.path}\t"
f"{'TRUE' if cookie.secure else 'FALSE'}\t"
f"{int(cookie.expires) if cookie.expires else 0}\t"
f"{cookie.name}\t"
f"{cookie.value}\n"
)
print(f"Cookies saved to {LOCAL_COOKIES_PATH}")
except Exception as e:
print(f"Failed to extract cookies: {e}")
print("Consider using Firefox, or manually exporting cookies using a browser extension like EditThisCookie.")
extract_youtube_cookies()
EOF
)
# Generate cookies file
echo "Generating cookies file..."
python3 -c "$extract_youtube_cookies_py"
COOKIES_FILE="/tmp/cookies.txt"
# Fetch video information in JSON format
VIDEO_INFO=$(yt-dlp -J "$YOUTUBE_URL")
# Extract the video title
VIDEO_NAME=$(echo "$VIDEO_INFO" | jq -r '.title')
echo "Downloading video: $VIDEO_NAME"
# Sanitize the video name to remove any illegal characters
sanitize_filename() {
echo "$1" | sed 's/[\\/*?:"<>|]/_/g'
}
VIDEO_NAME=$(sanitize_filename "$VIDEO_NAME")
# Set the extension to mp4
EXT="mp4"
# Download the video using the sanitized video name and correct extension
yt-dlp --paths downloads -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4] -o "${VIDEO_NAME}.${EXT}" "${YOUTUBE_URL}" --cookies "$COOKIES_FILE"
# Clean up the cookies file
rm "$COOKIES_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment