Skip to content

Instantly share code, notes, and snippets.

@anthonyrussano
Created October 25, 2025 02:02
Show Gist options
  • Select an option

  • Save anthonyrussano/ac9199cd239450d065b5fa357addae98 to your computer and use it in GitHub Desktop.

Select an option

Save anthonyrussano/ac9199cd239450d065b5fa357addae98 to your computer and use it in GitHub Desktop.
#!/bin/bash
# yt-nas - Download videos/audio with yt-dlp and upload to NAS
# Usage: yt-nas <url> [video|audio] [custom-filename]
set -e
# Configuration
NAS_USER="anthony"
NAS_HOST="naspi5"
NAS_VIDEO_PATH="/mnt/raid5/Videos/"
NAS_AUDIO_PATH="/mnt/raid5/Music/"
# Default settings
DOWNLOAD_TYPE="video"
CUSTOM_FILENAME=""
# Parse arguments
if [ $# -lt 1 ]; then
echo "Usage: $0 <url> [video|audio] [custom-filename]"
echo ""
echo "Examples:"
echo " $0 https://youtube.com/watch?v=... video"
echo " $0 https://youtube.com/watch?v=... audio"
echo " $0 https://youtube.com/watch?v=... video my-custom-name"
echo ""
echo "Default: Downloads as video"
exit 1
fi
URL="$1"
if [ $# -ge 2 ]; then
DOWNLOAD_TYPE="$2"
fi
if [ $# -ge 3 ]; then
CUSTOM_FILENAME="$3"
fi
# Validate download type
if [ "$DOWNLOAD_TYPE" != "video" ] && [ "$DOWNLOAD_TYPE" != "audio" ]; then
echo "Error: Download type must be 'video' or 'audio'"
exit 1
fi
# Create temporary download directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
echo "Downloading $DOWNLOAD_TYPE from: $URL"
echo "Temporary directory: $TEMP_DIR"
echo ""
# Download based on type
if [ "$DOWNLOAD_TYPE" = "audio" ]; then
if [ -n "$CUSTOM_FILENAME" ]; then
yt-dlp -x --audio-format mp3 --audio-quality 0 -o "${CUSTOM_FILENAME}.%(ext)s" "$URL"
DOWNLOADED_FILE="${CUSTOM_FILENAME}.mp3"
else
yt-dlp -x --audio-format mp3 --audio-quality 0 -o "%(title)s.%(ext)s" "$URL"
DOWNLOADED_FILE=$(ls -t *.mp3 | head -n1)
fi
NAS_PATH="$NAS_AUDIO_PATH"
else
if [ -n "$CUSTOM_FILENAME" ]; then
yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --merge-output-format mp4 -o "${CUSTOM_FILENAME}.%(ext)s" "$URL"
# Check for both mp4 and webm as fallback
if [ -f "${CUSTOM_FILENAME}.mp4" ]; then
DOWNLOADED_FILE="${CUSTOM_FILENAME}.mp4"
else
DOWNLOADED_FILE=$(ls -t "${CUSTOM_FILENAME}".* | head -n1)
fi
else
yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --merge-output-format mp4 -o "%(title)s.%(ext)s" "$URL"
DOWNLOADED_FILE=$(ls -t * | head -n1)
fi
NAS_PATH="$NAS_VIDEO_PATH"
fi
# Check if download was successful
if [ ! -f "$DOWNLOADED_FILE" ]; then
echo "Error: Download failed or file not found"
cd /
rm -rf "$TEMP_DIR"
exit 1
fi
echo ""
echo "Downloaded: $DOWNLOADED_FILE"
echo "File size: $(du -h "$DOWNLOADED_FILE" | cut -f1)"
echo ""
echo "Uploading to NAS: ${NAS_USER}@${NAS_HOST}:${NAS_PATH}"
# Upload to NAS
scp "$DOWNLOADED_FILE" "${NAS_USER}@${NAS_HOST}:${NAS_PATH}"
if [ $? -eq 0 ]; then
echo ""
echo "✓ Successfully uploaded to NAS!"
echo "Location: ${NAS_USER}@${NAS_HOST}:${NAS_PATH}${DOWNLOADED_FILE}"
# Clean up
cd /
rm -rf "$TEMP_DIR"
echo "✓ Cleaned up temporary files"
else
echo ""
echo "✗ Upload failed. File remains in: $TEMP_DIR"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment