| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- #!/bin/bash
- # YouTube Predownloader Installation Script
- # This script sets up the YouTube predownloader as a systemd service
- set -e # Exit on any error
- # Colors for output
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m' # No Color
- # Configuration
- SERVICE_NAME="youtube-predownloader"
- INSTALL_DIR="/opt/youtube-predownloader"
- SERVICE_USER="youtube-dl"
- VENV_DIR="$INSTALL_DIR/venv"
- LOG_DIR="/var/log/youtube-predownloader"
- echo -e "${BLUE}🎬 YouTube Predownloader Installation Script${NC}"
- echo "=============================================="
- # Check if running as root
- if [[ $EUID -ne 0 ]]; then
- echo -e "${RED}❌ This script must be run as root (use sudo)${NC}"
- exit 1
- fi
- echo -e "${YELLOW}📋 Installation Configuration:${NC}"
- echo " Service Name: $SERVICE_NAME"
- echo " Install Directory: $INSTALL_DIR"
- echo " Service User: $SERVICE_USER"
- echo " Virtual Environment: $VENV_DIR"
- echo " Log Directory: $LOG_DIR"
- echo
- # Prompt for confirmation
- read -p "Continue with installation? (y/N): " -n 1 -r
- echo
- if [[ ! $REPLY =~ ^[Yy]$ ]]; then
- echo -e "${YELLOW}⚠️ Installation cancelled${NC}"
- exit 0
- fi
- # Install system dependencies
- echo -e "${BLUE}📦 Installing system dependencies...${NC}"
- apt update
- apt install -y python3 python3-venv python3-pip ffmpeg git curl
- # Install Intel GPU acceleration dependencies
- echo -e "${BLUE}🚀 Installing Intel GPU acceleration dependencies...${NC}"
- apt install -y intel-media-va-driver vainfo mesa-va-drivers
- echo -e "${GREEN}✅ GPU acceleration dependencies installed${NC}"
- # Configure GPU permissions
- echo -e "${BLUE}🔧 Configuring GPU permissions...${NC}"
- usermod -a -G video $SERVICE_USER
- usermod -a -G render $SERVICE_USER
- echo -e "${GREEN}✅ GPU permissions configured${NC}"
- # Create service user
- echo -e "${BLUE}👤 Creating service user...${NC}"
- if ! id "$SERVICE_USER" &>/dev/null; then
- useradd --system --shell /bin/false --home-dir $INSTALL_DIR --create-home $SERVICE_USER
- echo -e "${GREEN}✅ Created user: $SERVICE_USER${NC}"
- else
- echo -e "${YELLOW}⚠️ User $SERVICE_USER already exists${NC}"
- fi
- # Create installation directory
- echo -e "${BLUE}📁 Setting up installation directory...${NC}"
- mkdir -p $INSTALL_DIR
- mkdir -p $LOG_DIR
- chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
- chown $SERVICE_USER:$SERVICE_USER $LOG_DIR
- # Copy files to installation directory
- echo -e "${BLUE}📋 Copying application files...${NC}"
- if [ -f "main.py" ]; then
- cp *.py $INSTALL_DIR/
- if [ -f "client_secret.json" ]; then
- cp client_secret.json $INSTALL_DIR/
- fi
- if [ -f "service_account.json" ]; then
- cp service_account.json $INSTALL_DIR/
- fi
- if [ -f "*.pickle" ]; then
- cp *.pickle $INSTALL_DIR/ 2>/dev/null || true
- fi
- chown -R $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
- echo -e "${GREEN}✅ Application files copied${NC}"
- else
- echo -e "${RED}❌ main.py not found in current directory${NC}"
- exit 1
- fi
- # Create virtual environment
- echo -e "${BLUE}🐍 Creating Python virtual environment...${NC}"
- sudo -u $SERVICE_USER python3 -m venv $VENV_DIR
- echo -e "${GREEN}✅ Virtual environment created${NC}"
- # Install Python dependencies
- echo -e "${BLUE}📦 Installing Python dependencies...${NC}"
- cat > $INSTALL_DIR/requirements.txt << 'EOF'
- google-auth-oauthlib>=1.2.0
- google-api-python-client>=2.100.0
- google-auth>=2.15.0
- yt-dlp>=2023.7.6
- paho-mqtt>=2.0.0
- psutil>=5.9.0
- EOF
- sudo -u $SERVICE_USER $VENV_DIR/bin/pip install --upgrade pip
- sudo -u $SERVICE_USER $VENV_DIR/bin/pip install -r $INSTALL_DIR/requirements.txt
- echo -e "${GREEN}✅ Python dependencies installed${NC}"
- # Create configuration file
- echo -e "${BLUE}⚙️ Creating configuration file...${NC}"
- cat > $INSTALL_DIR/config.py << 'EOF'
- # YouTube Predownloader Configuration
- # Output directory for downloaded videos
- OUTPUT_DIR = "/media/youtube" # Change this to your desired output directory
- # Schedule settings
- SCHEDULE_START_TIME = "00:00" # Start time (24-hour format)
- SCHEDULE_END_TIME = "07:00" # End time (24-hour format)
- RUN_ONCE_PER_DAY = True # Only run once per day
- # YouTube API settings
- DAYS_BACK = 5 # Videos published in the last N days
- DAYS_TO_KEEP = 10 # Delete videos older than N days
- POPULAR_VIDEOS_MAX_RESULTS = 10
- REGION_CODES = ["US", "GB", "LU", "LK"]
- # Conversion settings
- MAX_CONCURRENT_CONVERSIONS = 2
- CONVERSION_QUEUE_SIZE = 50
- # GPU acceleration settings
- GPU_ACCELERATION_ENABLED = True
- INTEL_VAAPI_DEVICE = "/dev/dri/renderD128"
- # Home Assistant MQTT settings (optional)
- HOMEASSISTANT_ENABLED = True
- MQTT_BROKER = "192.168.1.100" # Your Home Assistant IP
- MQTT_PORT = 1883
- MQTT_USERNAME = "" # Set if authentication required
- MQTT_PASSWORD = "" # Set if authentication required
- EOF
- chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR/config.py
- echo -e "${GREEN}✅ Configuration file created${NC}"
- # Create systemd service file
- echo -e "${BLUE}🔧 Creating systemd service...${NC}"
- cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
- [Unit]
- Description=YouTube Predownloader Service
- After=network.target network-online.target
- Wants=network-online.target
- [Service]
- Type=simple
- User=$SERVICE_USER
- Group=$SERVICE_USER
- WorkingDirectory=$INSTALL_DIR
- Environment=PATH=$VENV_DIR/bin
- ExecStart=$VENV_DIR/bin/python main.py
- Restart=on-failure
- RestartSec=60
- StandardOutput=append:$LOG_DIR/youtube-predownloader.log
- StandardError=append:$LOG_DIR/youtube-predownloader.log
- # Security settings
- NoNewPrivileges=true
- PrivateTmp=true
- ProtectSystem=strict
- ProtectHome=true
- ReadWritePaths=$INSTALL_DIR $LOG_DIR /media
- DeviceAllow=/dev/dri/renderD128 rw
- DeviceAllow=/dev/dri/card1 rw
- ProtectKernelTunables=true
- ProtectKernelModules=true
- ProtectControlGroups=true
- [Install]
- WantedBy=multi-user.target
- EOF
- echo -e "${GREEN}✅ Systemd service created${NC}"
- # Create log rotation
- echo -e "${BLUE}📜 Setting up log rotation...${NC}"
- cat > /etc/logrotate.d/$SERVICE_NAME << EOF
- $LOG_DIR/*.log {
- daily
- missingok
- rotate 30
- compress
- delaycompress
- notifempty
- create 644 $SERVICE_USER $SERVICE_USER
- postrotate
- systemctl reload $SERVICE_NAME 2>/dev/null || true
- endscript
- }
- EOF
- echo -e "${GREEN}✅ Log rotation configured${NC}"
- # Create output directory
- echo -e "${BLUE}📁 Creating output directory...${NC}"
- OUTPUT_DIR_DEFAULT="/media/youtube"
- mkdir -p $OUTPUT_DIR_DEFAULT
- chown $SERVICE_USER:$SERVICE_USER $OUTPUT_DIR_DEFAULT
- echo -e "${GREEN}✅ Output directory created: $OUTPUT_DIR_DEFAULT${NC}"
- # Set up systemd service
- echo -e "${BLUE}🔧 Enabling systemd service...${NC}"
- systemctl daemon-reload
- systemctl enable $SERVICE_NAME
- echo -e "${GREEN}✅ Service enabled${NC}"
- # Create management script
- echo -e "${BLUE}🛠️ Creating management script...${NC}"
- cat > /usr/local/bin/youtube-predownloader << 'EOF'
- #!/bin/bash
- # YouTube Predownloader Management Script
- SERVICE_NAME="youtube-predownloader"
- INSTALL_DIR="/opt/youtube-predownloader"
- LOG_DIR="/var/log/youtube-predownloader"
- case "$1" in
- start)
- echo "Starting YouTube Predownloader..."
- systemctl start $SERVICE_NAME
- ;;
- stop)
- echo "Stopping YouTube Predownloader..."
- systemctl stop $SERVICE_NAME
- ;;
- restart)
- echo "Restarting YouTube Predownloader..."
- systemctl restart $SERVICE_NAME
- ;;
- status)
- systemctl status $SERVICE_NAME
- ;;
- logs)
- journalctl -u $SERVICE_NAME -f
- ;;
- logs-file)
- tail -f $LOG_DIR/youtube-predownloader.log
- ;;
- config)
- nano $INSTALL_DIR/config.py
- echo "Configuration updated. Restart service to apply changes."
- ;;
- update)
- echo "Updating yt-dlp..."
- sudo -u youtube-dl $INSTALL_DIR/venv/bin/pip install --upgrade yt-dlp
- echo "Restarting service..."
- systemctl restart $SERVICE_NAME
- ;;
- gpu-test)
- echo "Testing GPU acceleration capabilities..."
- sudo -u youtube-dl $INSTALL_DIR/venv/bin/python -c "
- import subprocess
- try:
- result = subprocess.run(['vainfo', '--display', 'drm', '--device', '/dev/dri/renderD128'],
- capture_output=True, text=True, timeout=10)
- if result.returncode == 0:
- print('✅ Intel VAAPI GPU acceleration available')
- if 'VAProfileH264High' in result.stdout:
- print('✅ H.264 hardware decode/encode supported')
- if 'VAProfileVP9Profile0' in result.stdout:
- print('✅ VP9 hardware decode supported')
- if 'VAProfileHEVCMain' in result.stdout:
- print('✅ HEVC hardware decode supported')
- else:
- print('❌ GPU acceleration not available')
- except Exception as e:
- print(f'❌ Error testing GPU: {e}')
- "
- ;;
- performance)
- echo "Showing performance statistics..."
- sudo -u youtube-dl $INSTALL_DIR/venv/bin/python -c "
- import psutil
- import subprocess
- print(f'CPU Usage: {psutil.cpu_percent()}%')
- print(f'Memory Usage: {psutil.virtual_memory().percent}%')
- print(f'Disk Usage: {psutil.disk_usage(\"/media\").percent}%')
- try:
- result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
- if result.returncode == 0:
- print('NVIDIA GPU detected')
- else:
- print('Using Intel integrated graphics')
- except:
- print('Using Intel integrated graphics')
- "
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status|logs|logs-file|config|update|gpu-test|performance}"
- echo ""
- echo "Commands:"
- echo " start - Start the service"
- echo " stop - Stop the service"
- echo " restart - Restart the service"
- echo " status - Show service status"
- echo " logs - Show live system logs"
- echo " logs-file - Show live application logs"
- echo " config - Edit configuration"
- echo " update - Update yt-dlp"
- echo " gpu-test - Test GPU acceleration"
- echo " performance - Show system performance"
- exit 1
- ;;
- esac
- EOF
- chmod +x /usr/local/bin/youtube-predownloader
- echo -e "${GREEN}✅ Management script created: youtube-predownloader${NC}"
- echo
- echo -e "${GREEN}🎉 Installation completed successfully!${NC}"
- echo
- echo -e "${BLUE}📋 Next Steps:${NC}"
- echo "1. Edit configuration: youtube-predownloader config"
- echo "2. Add your YouTube credentials (client_secret.json or service_account.json)"
- echo "3. Start the service: youtube-predownloader start"
- echo "4. Check status: youtube-predownloader status"
- echo "5. View logs: youtube-predownloader logs"
- echo
- echo -e "${BLUE}🛠️ Management Commands:${NC}"
- echo " youtube-predownloader start - Start service"
- echo " youtube-predownloader stop - Stop service"
- echo " youtube-predownloader restart - Restart service"
- echo " youtube-predownloader status - Check status"
- echo " youtube-predownloader logs - View logs"
- echo " youtube-predownloader config - Edit settings"
- echo " youtube-predownloader update - Update yt-dlp"
- echo " youtube-predownloader gpu-test - Test GPU acceleration"
- echo " youtube-predownloader performance - Show performance stats"
- echo
- echo -e "${YELLOW}⚠️ Important:${NC}"
- echo "- Edit $INSTALL_DIR/config.py to customize settings"
- echo "- Place your YouTube API credentials in $INSTALL_DIR/"
- echo "- Service will run continuously and handle scheduling internally"
- echo "- Logs are available at $LOG_DIR/youtube-predownloader.log"
- echo "- GPU acceleration is enabled for Intel integrated graphics"
- echo "- Run 'youtube-predownloader gpu-test' to verify GPU support"
- echo
|