install.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #!/bin/bash
  2. # YouTube Predownloader Installation Script
  3. # This script sets up the YouTube predownloader as a systemd service
  4. set -e # Exit on any error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Configuration
  12. SERVICE_NAME="youtube-predownloader"
  13. INSTALL_DIR="/opt/youtube-predownloader"
  14. SERVICE_USER="youtube-dl"
  15. VENV_DIR="$INSTALL_DIR/venv"
  16. LOG_DIR="/var/log/youtube-predownloader"
  17. echo -e "${BLUE}🎬 YouTube Predownloader Installation Script${NC}"
  18. echo "=============================================="
  19. # Check if running as root
  20. if [[ $EUID -ne 0 ]]; then
  21. echo -e "${RED}❌ This script must be run as root (use sudo)${NC}"
  22. exit 1
  23. fi
  24. echo -e "${YELLOW}📋 Installation Configuration:${NC}"
  25. echo " Service Name: $SERVICE_NAME"
  26. echo " Install Directory: $INSTALL_DIR"
  27. echo " Service User: $SERVICE_USER"
  28. echo " Virtual Environment: $VENV_DIR"
  29. echo " Log Directory: $LOG_DIR"
  30. echo
  31. # Prompt for confirmation
  32. read -p "Continue with installation? (y/N): " -n 1 -r
  33. echo
  34. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  35. echo -e "${YELLOW}⚠️ Installation cancelled${NC}"
  36. exit 0
  37. fi
  38. # Install system dependencies
  39. echo -e "${BLUE}📦 Installing system dependencies...${NC}"
  40. apt update
  41. apt install -y python3 python3-venv python3-pip ffmpeg git curl
  42. # Install Intel GPU acceleration dependencies
  43. echo -e "${BLUE}🚀 Installing Intel GPU acceleration dependencies...${NC}"
  44. apt install -y intel-media-va-driver vainfo mesa-va-drivers
  45. echo -e "${GREEN}✅ GPU acceleration dependencies installed${NC}"
  46. # Configure GPU permissions
  47. echo -e "${BLUE}🔧 Configuring GPU permissions...${NC}"
  48. usermod -a -G video $SERVICE_USER
  49. usermod -a -G render $SERVICE_USER
  50. echo -e "${GREEN}✅ GPU permissions configured${NC}"
  51. # Create service user
  52. echo -e "${BLUE}👤 Creating service user...${NC}"
  53. if ! id "$SERVICE_USER" &>/dev/null; then
  54. useradd --system --shell /bin/false --home-dir $INSTALL_DIR --create-home $SERVICE_USER
  55. echo -e "${GREEN}✅ Created user: $SERVICE_USER${NC}"
  56. else
  57. echo -e "${YELLOW}⚠️ User $SERVICE_USER already exists${NC}"
  58. fi
  59. # Create installation directory
  60. echo -e "${BLUE}📁 Setting up installation directory...${NC}"
  61. mkdir -p $INSTALL_DIR
  62. mkdir -p $LOG_DIR
  63. chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
  64. chown $SERVICE_USER:$SERVICE_USER $LOG_DIR
  65. # Copy files to installation directory
  66. echo -e "${BLUE}📋 Copying application files...${NC}"
  67. if [ -f "main.py" ]; then
  68. cp *.py $INSTALL_DIR/
  69. if [ -f "client_secret.json" ]; then
  70. cp client_secret.json $INSTALL_DIR/
  71. fi
  72. if [ -f "service_account.json" ]; then
  73. cp service_account.json $INSTALL_DIR/
  74. fi
  75. if [ -f "*.pickle" ]; then
  76. cp *.pickle $INSTALL_DIR/ 2>/dev/null || true
  77. fi
  78. chown -R $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
  79. echo -e "${GREEN}✅ Application files copied${NC}"
  80. else
  81. echo -e "${RED}❌ main.py not found in current directory${NC}"
  82. exit 1
  83. fi
  84. # Create virtual environment
  85. echo -e "${BLUE}🐍 Creating Python virtual environment...${NC}"
  86. sudo -u $SERVICE_USER python3 -m venv $VENV_DIR
  87. echo -e "${GREEN}✅ Virtual environment created${NC}"
  88. # Install Python dependencies
  89. echo -e "${BLUE}📦 Installing Python dependencies...${NC}"
  90. cat > $INSTALL_DIR/requirements.txt << 'EOF'
  91. google-auth-oauthlib>=1.2.0
  92. google-api-python-client>=2.100.0
  93. google-auth>=2.15.0
  94. yt-dlp>=2023.7.6
  95. paho-mqtt>=2.0.0
  96. psutil>=5.9.0
  97. EOF
  98. sudo -u $SERVICE_USER $VENV_DIR/bin/pip install --upgrade pip
  99. sudo -u $SERVICE_USER $VENV_DIR/bin/pip install -r $INSTALL_DIR/requirements.txt
  100. echo -e "${GREEN}✅ Python dependencies installed${NC}"
  101. # Create configuration file
  102. echo -e "${BLUE}⚙️ Creating configuration file...${NC}"
  103. cat > $INSTALL_DIR/config.py << 'EOF'
  104. # YouTube Predownloader Configuration
  105. # Output directory for downloaded videos
  106. OUTPUT_DIR = "/media/youtube" # Change this to your desired output directory
  107. # Schedule settings
  108. SCHEDULE_START_TIME = "00:00" # Start time (24-hour format)
  109. SCHEDULE_END_TIME = "07:00" # End time (24-hour format)
  110. RUN_ONCE_PER_DAY = True # Only run once per day
  111. # YouTube API settings
  112. DAYS_BACK = 5 # Videos published in the last N days
  113. DAYS_TO_KEEP = 10 # Delete videos older than N days
  114. POPULAR_VIDEOS_MAX_RESULTS = 10
  115. REGION_CODES = ["US", "GB", "LU", "LK"]
  116. # Conversion settings
  117. MAX_CONCURRENT_CONVERSIONS = 2
  118. CONVERSION_QUEUE_SIZE = 50
  119. # GPU acceleration settings
  120. GPU_ACCELERATION_ENABLED = True
  121. INTEL_VAAPI_DEVICE = "/dev/dri/renderD128"
  122. # Home Assistant MQTT settings (optional)
  123. HOMEASSISTANT_ENABLED = True
  124. MQTT_BROKER = "192.168.1.100" # Your Home Assistant IP
  125. MQTT_PORT = 1883
  126. MQTT_USERNAME = "" # Set if authentication required
  127. MQTT_PASSWORD = "" # Set if authentication required
  128. EOF
  129. chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR/config.py
  130. echo -e "${GREEN}✅ Configuration file created${NC}"
  131. # Create systemd service file
  132. echo -e "${BLUE}🔧 Creating systemd service...${NC}"
  133. cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
  134. [Unit]
  135. Description=YouTube Predownloader Service
  136. After=network.target network-online.target
  137. Wants=network-online.target
  138. [Service]
  139. Type=simple
  140. User=$SERVICE_USER
  141. Group=$SERVICE_USER
  142. WorkingDirectory=$INSTALL_DIR
  143. Environment=PATH=$VENV_DIR/bin
  144. ExecStart=$VENV_DIR/bin/python main.py
  145. Restart=on-failure
  146. RestartSec=60
  147. StandardOutput=append:$LOG_DIR/youtube-predownloader.log
  148. StandardError=append:$LOG_DIR/youtube-predownloader.log
  149. # Security settings
  150. NoNewPrivileges=true
  151. PrivateTmp=true
  152. ProtectSystem=strict
  153. ProtectHome=true
  154. ReadWritePaths=$INSTALL_DIR $LOG_DIR /media
  155. DeviceAllow=/dev/dri/renderD128 rw
  156. DeviceAllow=/dev/dri/card1 rw
  157. ProtectKernelTunables=true
  158. ProtectKernelModules=true
  159. ProtectControlGroups=true
  160. [Install]
  161. WantedBy=multi-user.target
  162. EOF
  163. echo -e "${GREEN}✅ Systemd service created${NC}"
  164. # Create log rotation
  165. echo -e "${BLUE}📜 Setting up log rotation...${NC}"
  166. cat > /etc/logrotate.d/$SERVICE_NAME << EOF
  167. $LOG_DIR/*.log {
  168. daily
  169. missingok
  170. rotate 30
  171. compress
  172. delaycompress
  173. notifempty
  174. create 644 $SERVICE_USER $SERVICE_USER
  175. postrotate
  176. systemctl reload $SERVICE_NAME 2>/dev/null || true
  177. endscript
  178. }
  179. EOF
  180. echo -e "${GREEN}✅ Log rotation configured${NC}"
  181. # Create output directory
  182. echo -e "${BLUE}📁 Creating output directory...${NC}"
  183. OUTPUT_DIR_DEFAULT="/media/youtube"
  184. mkdir -p $OUTPUT_DIR_DEFAULT
  185. chown $SERVICE_USER:$SERVICE_USER $OUTPUT_DIR_DEFAULT
  186. echo -e "${GREEN}✅ Output directory created: $OUTPUT_DIR_DEFAULT${NC}"
  187. # Set up systemd service
  188. echo -e "${BLUE}🔧 Enabling systemd service...${NC}"
  189. systemctl daemon-reload
  190. systemctl enable $SERVICE_NAME
  191. echo -e "${GREEN}✅ Service enabled${NC}"
  192. # Create management script
  193. echo -e "${BLUE}🛠️ Creating management script...${NC}"
  194. cat > /usr/local/bin/youtube-predownloader << 'EOF'
  195. #!/bin/bash
  196. # YouTube Predownloader Management Script
  197. SERVICE_NAME="youtube-predownloader"
  198. INSTALL_DIR="/opt/youtube-predownloader"
  199. LOG_DIR="/var/log/youtube-predownloader"
  200. case "$1" in
  201. start)
  202. echo "Starting YouTube Predownloader..."
  203. systemctl start $SERVICE_NAME
  204. ;;
  205. stop)
  206. echo "Stopping YouTube Predownloader..."
  207. systemctl stop $SERVICE_NAME
  208. ;;
  209. restart)
  210. echo "Restarting YouTube Predownloader..."
  211. systemctl restart $SERVICE_NAME
  212. ;;
  213. status)
  214. systemctl status $SERVICE_NAME
  215. ;;
  216. logs)
  217. journalctl -u $SERVICE_NAME -f
  218. ;;
  219. logs-file)
  220. tail -f $LOG_DIR/youtube-predownloader.log
  221. ;;
  222. config)
  223. nano $INSTALL_DIR/config.py
  224. echo "Configuration updated. Restart service to apply changes."
  225. ;;
  226. update)
  227. echo "Updating yt-dlp..."
  228. sudo -u youtube-dl $INSTALL_DIR/venv/bin/pip install --upgrade yt-dlp
  229. echo "Restarting service..."
  230. systemctl restart $SERVICE_NAME
  231. ;;
  232. gpu-test)
  233. echo "Testing GPU acceleration capabilities..."
  234. sudo -u youtube-dl $INSTALL_DIR/venv/bin/python -c "
  235. import subprocess
  236. try:
  237. result = subprocess.run(['vainfo', '--display', 'drm', '--device', '/dev/dri/renderD128'],
  238. capture_output=True, text=True, timeout=10)
  239. if result.returncode == 0:
  240. print('✅ Intel VAAPI GPU acceleration available')
  241. if 'VAProfileH264High' in result.stdout:
  242. print('✅ H.264 hardware decode/encode supported')
  243. if 'VAProfileVP9Profile0' in result.stdout:
  244. print('✅ VP9 hardware decode supported')
  245. if 'VAProfileHEVCMain' in result.stdout:
  246. print('✅ HEVC hardware decode supported')
  247. else:
  248. print('❌ GPU acceleration not available')
  249. except Exception as e:
  250. print(f'❌ Error testing GPU: {e}')
  251. "
  252. ;;
  253. performance)
  254. echo "Showing performance statistics..."
  255. sudo -u youtube-dl $INSTALL_DIR/venv/bin/python -c "
  256. import psutil
  257. import subprocess
  258. print(f'CPU Usage: {psutil.cpu_percent()}%')
  259. print(f'Memory Usage: {psutil.virtual_memory().percent}%')
  260. print(f'Disk Usage: {psutil.disk_usage(\"/media\").percent}%')
  261. try:
  262. result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
  263. if result.returncode == 0:
  264. print('NVIDIA GPU detected')
  265. else:
  266. print('Using Intel integrated graphics')
  267. except:
  268. print('Using Intel integrated graphics')
  269. "
  270. ;;
  271. *)
  272. echo "Usage: $0 {start|stop|restart|status|logs|logs-file|config|update|gpu-test|performance}"
  273. echo ""
  274. echo "Commands:"
  275. echo " start - Start the service"
  276. echo " stop - Stop the service"
  277. echo " restart - Restart the service"
  278. echo " status - Show service status"
  279. echo " logs - Show live system logs"
  280. echo " logs-file - Show live application logs"
  281. echo " config - Edit configuration"
  282. echo " update - Update yt-dlp"
  283. echo " gpu-test - Test GPU acceleration"
  284. echo " performance - Show system performance"
  285. exit 1
  286. ;;
  287. esac
  288. EOF
  289. chmod +x /usr/local/bin/youtube-predownloader
  290. echo -e "${GREEN}✅ Management script created: youtube-predownloader${NC}"
  291. echo
  292. echo -e "${GREEN}🎉 Installation completed successfully!${NC}"
  293. echo
  294. echo -e "${BLUE}📋 Next Steps:${NC}"
  295. echo "1. Edit configuration: youtube-predownloader config"
  296. echo "2. Add your YouTube credentials (client_secret.json or service_account.json)"
  297. echo "3. Start the service: youtube-predownloader start"
  298. echo "4. Check status: youtube-predownloader status"
  299. echo "5. View logs: youtube-predownloader logs"
  300. echo
  301. echo -e "${BLUE}🛠️ Management Commands:${NC}"
  302. echo " youtube-predownloader start - Start service"
  303. echo " youtube-predownloader stop - Stop service"
  304. echo " youtube-predownloader restart - Restart service"
  305. echo " youtube-predownloader status - Check status"
  306. echo " youtube-predownloader logs - View logs"
  307. echo " youtube-predownloader config - Edit settings"
  308. echo " youtube-predownloader update - Update yt-dlp"
  309. echo " youtube-predownloader gpu-test - Test GPU acceleration"
  310. echo " youtube-predownloader performance - Show performance stats"
  311. echo
  312. echo -e "${YELLOW}⚠️ Important:${NC}"
  313. echo "- Edit $INSTALL_DIR/config.py to customize settings"
  314. echo "- Place your YouTube API credentials in $INSTALL_DIR/"
  315. echo "- Service will run continuously and handle scheduling internally"
  316. echo "- Logs are available at $LOG_DIR/youtube-predownloader.log"
  317. echo "- GPU acceleration is enabled for Intel integrated graphics"
  318. echo "- Run 'youtube-predownloader gpu-test' to verify GPU support"
  319. echo